Use go get
to download the following packages:
$ go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
$ go get google.golang.org/protobuf/cmd/protoc-gen-go
$ go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
This installs the protoc
generator plugins we need to generate the stubs. Make sure to add $GOPATH/bin
to your $PATH
so that executables installed via go get
are available on your $PATH
.
Here's an example of what a protoc
command might look like to generate Go stubs, assuming that you're at the root of your repository and you have your proto files in a directory called proto
:
$ protoc -I ./proto \
--go_out ./proto --go_opt paths=source_relative \
--go-grpc_out ./proto --go-grpc_opt paths=source_relative \
./proto/helloworld/hello_world.proto
We use the go
and go-grpc
plugins to generate Go types and gRPC service definitions. We're outputting the generated files relative to the proto
folder, and we're using the paths=source_relative
option, which means that the generated files will appear in the same directory as the source .proto
file.
You should check out the tutorial series on gRPC-Gateway, i.e., https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/. Also, you can refer to my simple hello world program, which uses gRPC-Gateway, i.e., https://github.com/iamrajiv/helloworld-grpc-gateway.