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.
Install the tools
Section titled “Install the tools”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:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latestgo install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latestprotoc finds plugins on your PATH, so make sure Go’s bin directory is on it:
export PATH="$PATH:$(go env GOPATH)/bin"Project layout
Section titled “Project layout”grpc-demo/├── go.mod├── example/│ └── example.proto├── server/│ └── main.go└── client/ └── main.goCreate the module:
mkdir grpc-demo && cd grpc-demogo mod init example.com/grpc-demoThe server and client each get their own directory because both are package main; two main functions in one directory won’t build together.
Define the service
Section titled “Define the service”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:
protoc --go_out=. --go_opt=paths=source_relative \ --go-grpc_out=. --go-grpc_opt=paths=source_relative \ example/example.protoThis writes two files next to the .proto:
example/example.pb.go: the message types (HelloRequest,HelloResponse).example/example_grpc.pb.go: theExampleServiceServerinterface,RegisterExampleServiceServer, and theExampleServiceClient.
Then pull in the runtime dependencies:
go mod tidyWrite the server
Section titled “Write the server”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.
Write the client
Section titled “Write the client”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.
Run it
Section titled “Run it”In one terminal:
go run ./serverIn another:
go run ./client AliceThe client logs (after the timestamp log adds):
Response: Hello, AliceNext steps and gotchas
Section titled “Next steps and gotchas”- 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, "...")fromgoogle.golang.org/grpc/statusandgoogle.golang.org/grpc/codes, so clients get a proper gRPC status code instead ofUnknown. - Debugging with grpcurl. Register server reflection with
reflection.Register(s)fromgoogle.golang.org/grpc/reflection, then tools like grpcurl can list and call your methods without the.protofile. - Browsers. Browsers can’t speak native gRPC directly. Use gRPC-Web or Connect if a web front end needs to call the service.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)