gRPC for Go (Golang): Libraries, Examples, and Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

gRPC for Go (Golang): Libraries and Examples



What is gRPC and Why Do You Need It in Go?



gRPC is a high-performance remote procedure call (RPC) framework developed by Google. It uses Protocol Buffers (protobuf) for data serialization and HTTP/2 for transport. In the Go ecosystem, gRPC has become the standard for building microservice architecture due to:



  • High performance (binary protocol, HTTP/2 multiplexing)
  • Strong typing (generated Go code)
  • Streaming support (client, server, and bidirectional)
  • Built-in authentication and load balancing


In this article, we will cover the main libraries, tools, and write a complete example of a server and client in Golang.



Required Libraries and Tools



To work with gRPC in Go, you will need:



  • google.golang.org/grpc — the main gRPC library for Go
  • google.golang.org/protobuf — working with Protocol Buffers
  • protoc — the .proto file compiler
  • protoc-gen-go — plugin for generating Go code from .proto
  • protoc-gen-go-grpc — plugin for generating gRPC server/client code


Installing Tools



# Install protoc (Linux/macOS)sudo apt install protobuf-compiler  # or brew install protobuf

# Install Go pluginsgo install google.golang.org/protobuf/cmd/protoc-gen-go@latestgo install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# Verifyprotoc --versionprotoc-gen-go --version


Creating a .proto File



Let's start by defining the service and messages in the hello.proto file:



syntax = "proto3";

package hello;

option go_package = "github.com/youruser/grpc-example/hello";

service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); rpc SayHelloStream (HelloRequest) returns (stream HelloReply);}

message HelloRequest { string name = 1;}

message HelloReply { string message = 1;}


Generating Go Code



protoc --go_out=. --go-grpc_out=. hello.proto


After execution, two files will appear:

  • hello.pb.go — message structures
  • hello_grpc.pb.go — server and client interfaces


Implementing a gRPC Server in Go



Create the file server.go:



package main

import ( "context" "log" "net"

"google.golang.org/grpc" pb "github.com/youruser/grpc-example/hello")

type server struct { pb.UnimplementedGreeterServer}

func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) { log.Printf("Received: %v", req.GetName()) return &pb.HelloReply{Message: "Hello, " + req.GetName()}, nil}

func (s *server) SayHelloStream(req *pb.HelloRequest, stream pb.Greeter_SayHelloStreamServer) error { for i := 0; i < 5; i++ { if err := stream.Send(&pb.HelloReply{ Message: "Hello " + req.GetName() + " - " + fmt.Sprint(i), }); err != nil { return err } } return nil}

func main() { lis, err := net.Listen("tcp", ":50051") if err != nil { log.Fatalf("failed to listen: %v", err) }

s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{})

log.Println("Server listening on :50051") if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) }}


Implementing a gRPC Client in Go



Create the file client.go:



package main

import ( "context" "log" "time"

"google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" pb "git

Recommendations