6 Ways to Install the protoc-gen-go and protoc-gen-go-grpc Plugins
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:
| Plugin | Function |
|---|---|
protoc-gen-go | Generates Go code from .proto files for data handling (messages, enums, etc.). |
protoc-gen-go-grpc | Generates 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.
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:
1go version
2protoc --versionIf protoc isn’t installed yet, you can install it on Linux/macOS via Homebrew:
1brew install protobuf2. Installing the Plugins
a. Installing protoc-gen-go
This plugin is used to generate .pb.go code from protobuf files.
Run the following command:
1go install google.golang.org/protobuf/cmd/protoc-gen-go@latestNote:
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.
1go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latestOnce again, the binary will be in $GOPATH/bin or $HOME/go/bin.
3. Verifying the Installation
Check the installation results with:
1which protoc-gen-go
2which protoc-gen-go-grpcIf 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
1.
2├── proto/
3│ └── hello.proto
4└── go.modb. Sample hello.proto File
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:
1protoc \
2 --go_out=. \
3 --go-grpc_out=. \
4 --proto_path=./proto \
5 proto/hello.protoOnce executed, new files will appear:
proto/hello.pb.go(the result of theprotoc-gen-goplugin)proto/hello_grpc.pb.go(the result of theprotoc-gen-go-grpcplugin)
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)
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
| Problem | Solution |
|---|---|
protoc-gen-go: program not found or is not executable | Make sure $GOPATH/bin or $HOME/go/bin is included in your $PATH environment variable. |
| Code is not generated | Check the .proto file path and the --proto_path argument, and make sure the plugin is installed. |
| Incompatible version | Check 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.