tl;dr A repo formerly configured to use GOPATH
is now configured for Modules. All's good and better. However, protoc correctly (!) generates Golang code for protobufs defined within the repo in a github.com/path/to/repo/protos
structure when I'd now prefer these to be generated in my sources, outside of GOPATH
. I'm moving them to resolve this. Is there a better solution?
I have a GitHub repo. For the sake of discussion, let's call it github.com/acme/toolbox
. In a subdirectory, I have protobuf files that include:
package acme.toolbox.v1;
option go_package = "github.com/acme/toolbox/protos";
When I was GOPATH
'ing, all was well and protoc
would generate Golang bindings in $GOPATH/src/github.com/acme/toolbox/protos
and my code, importing pb "github.com/acme/toolbox/protos"
, would work.
Moving to Go Modules hasn't been pain-free but, the benefits outweigh the cost and I'm future-proofing myself and the code.
My issue is that I don't see how I can get protoc
to generate the Golang bindings into my arbitrarily and outside of GOPATH
located clone.
I'm moving the files after they're generated but this feels... inelegant:
cd ${TOOLBOX}
protoc \
--proto_path=./protos \
--go_out=plugins=grpc:/go/src
./protos/*.proto
mv ${GOPATH}/src/github.com/acme/toolbox/protos/*.go ${TOOLBOX}/protos
Is there a better solution?
github.com/acme/toolbox
even though the files are stored in some arbitrary directory. This is becausego.mod
includes amodule
prefix that references that github path. If -- and perhaps this is my mistake --protos
is a subdirectory of the project, Go files in my project should refer to its files using the module prefix. – Interlard