Using gRPC / grpc-gateway and golang

James Kozianski
Aug 26, 2017 · 2 min read

If you’re using gRPC with grpc-gateway to generate a REST API for your vendored golang project, you will likely run into versioning problems. The new dep tool generates a dependency on grpc-gateway v1.2.2, which is not what is checked out when you run go get. This means that protoc --grpc_out=... will generate code that works with grpc-gateway@master, but go build will use grpc-gateway@v1.2.2, which leads to compile errors.

This leaves you with two options: change the binaries to match your project dependency, or change your project dependency to match your binaries.

Change your binaries

To force a particular version of a go binary installed by go get, you can simply checkout the appropriate version in git and then reinstall the binaries:

cd $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway
git checkout v1.2.2
(cd protoc-gen-grpc-gateway && go install)
(cd protoc-gen-swagger && go install)

The advantage of this approach is that changes to grpc-gateway won’t break your build if you re-run go get or dep ensure, as you are bound to a specific version.

Change your dependencies

dep allows you to depend on a specific branch of a dependency, even though it will try and depend on a named version if it can. This means we can simply edit the grpc-ecosystem section of Gopkg.toml and specify that we want it to track master. The relevant section should look like this:

[[constraint]]
name = "github.com/grpc-ecosystem/grpc-gateway"
branch = "master"

Now whenever you run dep ensure the latest changes from grpc-gateway will be pulled in.

Bazel

Using bazel to generate your grpc-gateway proxy may well be the best approach in the end, but Bazel’s BUILD rules for golang are still in alpha, and there’s no support for generating a grpc-gateway proxy from them, though it seems like it might not be too hard to add (see the existing go_proto_library rule).

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade