[gRPC] Generate .pb.go from .proto
Generate .pb.go automatically using .proto file in Windows
For example, let's assume that we have following .proto written
syntax = "proto3";
package v1.k8s;
option go_package = "./nodes";
service Nodes {
  rpc GetNodes(GetNodesRequest) returns (GetNodesResponse);
}
message NodeInfoMessage {
  string name = 1;
  string version = 2;
  string address = 3;
  string osImage = 4;
  string ready = 5;
}
message GetNodesRequest {}
message GetNodesResponse {
  repeated NodeInfoMessage nodes = 1;
}In order for us to use this nodes.proto, we need to have nodes.pb.go file. We can achieve that by following instruction under Windows 10 environment:
I am assuming that you have your GOPATH and all PATH environment variables set correctly.
1. Install Protocol Buffers
- Download your Protocol Buffers from https://github.com/protocolbuffers/protobuf/releases. Look for Windows binary, for example it looks like - protoc-21.12-win64.zip.
- Extract - .zipand set- PATH.
2. Install Plugins
We need some things like protoc-gen-* to generate grpc stuff
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest3. Generate .pb.go
When you have set your go_package as following:
option go_package = "./nodes";This will generate your nodes.pb.go file under ./nodes directory. To do this, execute following command in the same directory where your nodes.proto resides in:
protoc -I . --go_out=. nodes.protoThis will generate the .pb.go file for gRPC
4. Generate _grpc.pb.go
Just like .pb.go, following command will generate nodes_grpc.pb.go under ./nodes directory. To do this, execute following command in the same directory where your nodes.proto resides in:
protoc -I . --protoc-gen_go=. nodes.proto 
Last updated