An example repository that uses “fixed protoc versions” like protoc, protoc-gen-go can be found at https://github.com/MelleKoning/goproto. This ensures stability of the use of these code generation tools.
Introduction
When programming with #GoLang we can define interfaces to interact with other applications using Protocol Buffers. There are different versions of generating golang code for protoc, protoc-gen-go-grpc tools from protocol buffers, and when programming on different devices, potentially having different versions of these generators and/or validators, we need a way to pinpoint to a certain version to harmonize development over multiple devices (or developers).
This repository is aimed to do just that.
By providing a docker image having a particular version of the protocol buffer generator (and potentially other accompanying tools) we ensure that we use the same versions across different devices.
Example addressbook.proto
The addressbook.proto
example is copied from protobuf buffers basics:GO tutorial
You can (re-)generate the addressbook.pb.go
and addressbook_grpc.pb.go
files by running the following commands:
make docker
-> creates the docker image with the protocol buffers golang tooling
make generate
-> uses the generated docker image with the go protobuffers tooling
The above tutorial also shows the way the generated golang structs can then be used to Marshal and Unmarshal data for writing to files.
Make generate
The actual line in the makefile that does the trick:
docker run --user $(USERID):$(USERGROUP) --rm -it -v $(PWD):/app -w /app MelleKoning/goproto protoc --go_out=$(PROTOFOLDER) --go-grpc_out=. --go-grpc_opt=paths=source_relative $(PROTOFOLDER)/*.proto
It’s important to understand that the current user:group credentials are provided to the docker command so that the created files are also created with the same credentials, otherwise the generated *.pb.go
files could only be accessible to the root:root user, as that’s usually the user the docker service is running with.
Also, the installed protoc tooling of the used docker image “MelleKoning/goproto” are being used with this docker run
command, which is ensuring that the versions of protoc and protoc-gen-go-grpc from the docker image are used, instead of any (potentially outdated or new and not yet stable) versions of those tools that might happen to be installed on the developers machine! This is important for consistency among a group of developers, so that a conscious choice can be made before upgrading these kind of tools.
Eventually, the generated files will contain the version information of the protoc-tooling that was installed in the used docker image
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.5.1
// - protoc v5.29.2
// source: api/addressbook/v1/addressbook.proto
package addressbook
import (
Whenever you re-generate the files, using the Docker image ensures that each developer will not accidentally downgrade the used tooling versions.
Happy coding!
Leave a Reply