Skip to content
Santekno.com | Tech Tutorials and Trends
EN
📖 0%
11 Jun 2025 · 5 min read ·Article 3 / 110
Go

3 Installing gRPC in Go Step by Step

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

Introduction

As the demand for modern, scalable, and reliable applications such as microservices continues to grow, Remote Procedure Call (RPC) has become an increasingly popular method for inter-process communication. One of the most powerful RPC technologies, and one that has become the de facto industry standard, is gRPC . gRPC offers high performance, an explicit API contract through Protobuf, and a robust ecosystem.

If you are a Go engineer looking to start adopting gRPC, this article will walk you through the step-by-step installation of gRPC in Go, complete with code examples, a service simulation, and flow diagrams so you can try it out directly on your own machine.


What is gRPC?

gRPC is an open-source RPC framework from Google. It uses Protocol Buffers (Protobuf) as its Interface Definition Language (IDL) and HTTP/2 as its transport. Some of its key advantages include:

  • High performance: HTTP/2 provides multiplexing, streaming, and binary framing.
  • Contract consistency: Protobuf as the IDL ensures consistent data types.
  • Interoperability: Supports many programming languages.
  • Data streaming: Supports bidirectional data streaming.

Prerequisites

Before installing, make sure you have prepared the following:

  • Go 1.18 or newer (go version).
  • The Protobuf compiler (protoc).
  • Git and an internet connection.

To keep this tutorial concise, use a Linux/Mac operating system. For Windows, adjust the paths where necessary.


Step 1: Install the Protobuf Compiler (protoc)

The Protobuf compiler (protoc) is the tool that converts .proto files into Go source code.

For Linux/MacOS

bash
1# Download the latest release (replace X.Y.Z with the latest version)
2wget https://github.com/protocolbuffers/protobuf/releases/download/v24.0/protoc-24.0-linux-x86_64.zip
3unzip protoc-24.0-linux-x86_64.zip -d $HOME/.local
4export PATH="$PATH:$HOME/.local/bin"

Verify the installation:

bash
1protoc --version
2# Output: libprotoc 24.0 (or the latest version)

For Mac (brew)

An alternative via Homebrew (MacOS):

bash
1brew install protobuf

Step 2: Install the gRPC Go Plugins

gRPC in Go requires two plugins:

  • protoc-gen-go: Generates the Protobuf Go code
  • protoc-gen-go-grpc: Generates the gRPC Go stub code

Install both with go install:

bash
1go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
2go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
3
4# Make sure $GOPATH/bin is in your PATH
5export PATH="$PATH:$(go env GOPATH)/bin"

Verify:

bash
1protoc-gen-go --version
2protoc-gen-go-grpc --version

Step 3: Initialize the Go Module

Create a new project folder and initialize it:

bash
1mkdir grpc-hello
2cd grpc-hello
3go mod init github.com/username/grpc-hello

Step 4: Write the .proto File

Create a proto folder and a hello.proto file:

bash
1mkdir proto
2touch proto/hello.proto

Contents of hello.proto:

proto
 1syntax = "proto3";
 2
 3package hello;
 4
 5service Greeter {
 6  rpc SayHello (HelloRequest) returns (HelloReply) {}
 7}
 8
 9message HelloRequest {
10  string name = 1;
11}
12
13message HelloReply {
14  string message = 1;
15}

Step 5: Generate Go Code from Protobuf

Run the following command at the project root:

bash
1protoc --go_out=. --go-grpc_out=. --proto_path=./proto proto/hello.proto

This will generate two files:

  • proto/hello.pb.go - the data type code
  • proto/hello_grpc.pb.go - the service stub code

Step 6: Install the gRPC Go Dependencies

Install the gRPC library and the Protobuf Go module:

bash
1go get google.golang.org/grpc
2go get google.golang.org/protobuf

Step 7: Implement the gRPC Server

Create a server.go file:

go
 1package main
 2
 3import (
 4	"context"
 5	"log"
 6	"net"
 7
 8	pb "github.com/username/grpc-hello/proto"
 9	"google.golang.org/grpc"
10)
11
12type server struct {
13	pb.UnimplementedGreeterServer
14}
15
16func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
17	return &pb.HelloReply{Message: "Halo, " + req.Name + "!"}, nil
18}
19
20func main() {
21	lis, err := net.Listen("tcp", ":50051")
22	if err != nil {
23		log.Fatalf("failed to listen: %v", err)
24	}
25	s := grpc.NewServer()
26	pb.RegisterGreeterServer(s, &server{})
27
28	log.Println("Server listened on :50051")
29	if err := s.Serve(lis); err != nil {
30		log.Fatalf("failed to serve: %v", err)
31	}
32}

Step 8: Implement the gRPC Client

Create a client.go file:

go
 1package main
 2
 3import (
 4	"context"
 5	"log"
 6	"time"
 7
 8	pb "github.com/username/grpc-hello/proto"
 9	"google.golang.org/grpc"
10)
11
12func main() {
13	conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
14	if err != nil {
15		log.Fatalf("Could not connect: %v", err)
16	}
17	defer conn.Close()
18
19	client := pb.NewGreeterClient(conn)
20	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
21	defer cancel()
22
23	resp, err := client.SayHello(ctx, &pb.HelloRequest{Name: "gopher"})
24	if err != nil {
25		log.Fatalf("Error calling SayHello: %v", err)
26	}
27	log.Printf("Response: %s", resp.Message)
28}

Flow Diagram: The gRPC Communication Process

Let’s visualize the classic request-response flow between the gRPC client and server with the Mermaid diagram below:

MERMAID
sequenceDiagram
    participant Client
    participant Server
    Client->>Server: SayHello(name="gopher")
    Server-->>Client: HelloReply(message="Halo, gopher!")

Simulation: Trying It Out

  1. Run the server
    bash
    1go run server.go
    Output:
    text
    1Server listened on :50051
  2. Run the client in a new terminal
    bash
    1go run client.go
    Output:
    text
    1Response: Halo, gopher!

Table: Installation and Setup Checklist

StepCommand / FileOutput / Goal
1Install protocCheck protoc --version
2Install the Go gRPC pluginsCheck protoc-gen-go --version
3go mod initGo module ready
4Write hello.protoProtobuf service & message definitions
5Generate code via protocGo stub files auto-generated in proto
6go get google.golang.org/grpcgrpc dependency installed
7Implement the serverListening process on port 50051
8Implement the clientExecute the RPC request

Conclusion

By following the steps above, you now have a boilerplate gRPC installation in Go - complete, from environment setup and code generation to making a request end-to-end. This approach can be developed as the foundation for building performant, type-safe, and scalable microservices using Go.

As a next step, you can explore advanced gRPC features such as:

  • Authentication & Authorization,
  • Bidirectional streaming,
  • Interceptors & Middleware,
  • Load balancing & service discovery.

Hopefully this article serves as a practical and comprehensive reference for those of you who want to build modern microservices in Go using gRPC. If you have any questions or want to share your experience, feel free to leave a comment below!


Happy coding! 🚀

Related Articles

💬 Comments