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

6 Ways to Install the protoc-gen-go and protoc-gen-go-grpc Plugins

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

In microservices-based development, communication between services becomes the key factor—and Protocol Buffers (protobuf) emerges as one of the fast and efficient serialization solutions. To combine Protocol Buffers with a Golang application, we need a few essential tools, in particular the protoc-gen-go and protoc-gen-go-grpc plugins.

In this article, I’ll walk you through the step-by-step installation process for both plugins, along with troubleshooting tips, best practices, a simple simulation, and an explanation of how these tools integrate into your development workflow.


Why protoc-gen-go and protoc-gen-go-grpc?

Before getting into the implementation, let’s first understand the role of each plugin:

PluginFunction
protoc-gen-goGenerates Go code from .proto files for data handling (messages, enums, etc.).
protoc-gen-go-grpcGenerates client and server stubs for gRPC using Go code.

So, if you want to build a Go application that communicates over gRPC and defines its API using .proto files, both plugins are absolutely vital.


Usage Flow (Workflow)

To make things clearer, here’s a simple diagram of the flow for using these plugins in the build workflow of a Go application based on protobuf and gRPC.

MERMAID
flowchart TD
    A[Menulis file .proto] --> B{Menjalankan
protoc compiler} B -->|--plugin=protoc-gen-go| C[Kode Go untuk data] B -->|--plugin=protoc-gen-go-grpc| D[Kode Go untuk gRPC Stub] C --> E[Integrasi ke dalam aplikasi Go] D --> E

1. Preparing the Environment

Before you start installing the plugins, make sure you meet the following prerequisites:

  • Go v1.18+
  • protoc (Protocol Buffer Compiler) v3.20+
  • GOPATH already set

Check Go and protoc:

bash
1go version
2protoc --version

If protoc isn’t installed yet, you can install it on Linux/macOS via Homebrew:

bash
1brew install protobuf

2. Installing the Plugins

a. Installing protoc-gen-go

This plugin is used to generate .pb.go code from protobuf files.

Run the following command:

bash
1go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

Note:
After this process, the protoc-gen-go binary will be located in $GOPATH/bin or $HOME/go/bin. Make sure this path is included in your $PATH environment variable.

b. Installing protoc-gen-go-grpc

This plugin specifically generates stub code for gRPC servers and clients.

bash
1go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

Once again, the binary will be in $GOPATH/bin or $HOME/go/bin.


3. Verifying the Installation

Check the installation results with:

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

If the output is a path to the binary, the installation was successful.


4. Usage Example

Let’s try a simple project simulation.

a. Project Structure

bash
1.
2├── proto/
3│   └── hello.proto
4└── go.mod

b. Sample hello.proto File

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}

c. Generate the Go Code

Open a terminal at the project root, then run:

bash
1protoc \
2  --go_out=. \
3  --go-grpc_out=. \
4  --proto_path=./proto \
5  proto/hello.proto

Once executed, new files will appear:

  • proto/hello.pb.go (the result of the protoc-gen-go plugin)
  • proto/hello_grpc.pb.go (the result of the protoc-gen-go-grpc plugin)

5. Simulating Integration in Go Code

You can use the generated code as the base for implementing a gRPC server or client.

Sample Server Implementation (Brief)

go
 1package main
 2
 3import (
 4    "context"
 5    "log"
 6    "net"
 7
 8    pb "path/to/proto/hello"
 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: "Hello " + req.Name}, nil
18}
19
20func main() {
21    lis, _ := net.Listen("tcp", ":50051")
22    grpcServer := grpc.NewServer()
23    pb.RegisterGreeterServer(grpcServer, &server{})
24    log.Println("gRPC Server listening on port 50051")
25    grpcServer.Serve(lis)
26}

6. Common Troubleshooting

ProblemSolution
protoc-gen-go: program not found or is not executableMake sure $GOPATH/bin or $HOME/go/bin is included in your $PATH environment variable.
Code is not generatedCheck the .proto file path and the --proto_path argument, and make sure the plugin is installed.
Incompatible versionCheck the versions of protoc, Go, and the plugins so they match the recommendations in the latest documentation.

7. Conclusion

By installing and configuring protoc-gen-go and protoc-gen-go-grpc, developing microservices applications based on Go and gRPC becomes much easier and more standardized. Be sure to always keep the plugins updated so you can enjoy the latest features and compatibility with protobuf and the gRPC-Go ecosystem.

Danger
Happy experimenting! If you have any questions or interesting experiences while setting up these plugins, feel free to share them in the comments.

References

Related Articles

💬 Comments