Skip to content

gRPC in Go: define a service, generate code, and write a server and client

gRPC is an RPC framework: you describe a service and its messages in a .proto file, generate client and server code for your language, and call remote methods as if they were local functions. Messages are serialised with Protocol Buffers and sent over HTTP/2, which gives you a compact binary format, a typed contract between services, and streaming in both directions.

This post builds a one-method ExampleService in Go, with a server and a client.

You need Go, the Protocol Buffers compiler protoc (download it from the protobuf releases page or your package manager), and the two Go code generators:

Terminal window
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

protoc finds plugins on your PATH, so make sure Go’s bin directory is on it:

Terminal window
export PATH="$PATH:$(go env GOPATH)/bin"
grpc-demo/
├── go.mod
├── example/
│ └── example.proto
├── server/
│ └── main.go
└── client/
└── main.go

Create the module:

Terminal window
mkdir grpc-demo && cd grpc-demo
go mod init example.com/grpc-demo

The server and client each get their own directory because both are package main; two main functions in one directory won’t build together.

example/example.proto:

syntax = "proto3";
package example;
option go_package = "example.com/grpc-demo/example";
service ExampleService {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}

go_package tells the generator the Go import path of the generated code. Current protoc-gen-go requires it.

Generate the code from the project root:

Terminal window
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
example/example.proto

This writes two files next to the .proto:

  • example/example.pb.go: the message types (HelloRequest, HelloResponse).
  • example/example_grpc.pb.go: the ExampleServiceServer interface, RegisterExampleServiceServer, and the ExampleServiceClient.

Then pull in the runtime dependencies:

Terminal window
go mod tidy

server/main.go:

package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "example.com/grpc-demo/example"
)
type server struct {
pb.UnimplementedExampleServiceServer
}
func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloResponse, error) {
return &pb.HelloResponse{Message: "Hello, " + req.GetName()}, nil
}
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterExampleServiceServer(s, &server{})
log.Printf("gRPC server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}

Embedding pb.UnimplementedExampleServiceServer is required by the generated code. It makes any method you haven’t implemented return an Unimplemented error, so adding a new RPC to the .proto doesn’t break the build of existing servers.

Use the generated getters like req.GetName() rather than reading fields directly: they are nil-safe.

client/main.go:

package main
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "example.com/grpc-demo/example"
)
func main() {
conn, err := grpc.NewClient("localhost:50051",
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
defer conn.Close()
client := pb.NewExampleServiceClient(conn)
name := "World"
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := client.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("SayHello failed: %v", err)
}
log.Printf("Response: %s", resp.GetMessage())
}

grpc.NewClient does not connect immediately. The connection is made on the first RPC, so a server that isn’t running shows up as an error from SayHello, not from NewClient.

insecure.NewCredentials() disables TLS, which is fine for local testing. In production, use credentials.NewTLS or your platform’s credentials instead.

In one terminal:

Terminal window
go run ./server

In another:

Terminal window
go run ./client Alice

The client logs (after the timestamp log adds):

Response: Hello, Alice
  • Deadlines. Always set a timeout on the client context, as above. Without one, a hung server can block the caller indefinitely.
  • Errors. Return errors from handlers with status.Error(codes.NotFound, "...") from google.golang.org/grpc/status and google.golang.org/grpc/codes, so clients get a proper gRPC status code instead of Unknown.
  • Debugging with grpcurl. Register server reflection with reflection.Register(s) from google.golang.org/grpc/reflection, then tools like grpcurl can list and call your methods without the .proto file.
  • Browsers. Browsers can’t speak native gRPC directly. Use gRPC-Web or Connect if a web front end needs to call the service.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)