Closed Kidsunbo closed 9 months ago
The go_package
option should contain the full import path of the Go package. So, for your example:
option go_package = "<my_project_package_name>/pb/base";
The
go_package
option should contain the full import path of the Go package. So, for your example:option go_package = "<my_project_package_name>/pb/base";
But what if the other project wants to use the proto
file? Change the name manually everytime?
The simplest approach is to generate the Go package once and make it a dependency of whatever other packages require it.
If you do need to generate code in various Go packages from the same .proto
source file (not recommended), you can set the Go package on the protoc
command line with something like --go_opt=Mbase/base.proto=<my_project_package_name>/pb/base
. See https://protobuf.dev/reference/go/go-generated/#package for details.
The simplest approach is to generate the Go package once and make it a dependency of whatever other packages require it.
If you do need to generate code in various Go packages from the same
.proto
source file (not recommended), you can set the Go package on theprotoc
command line with something like--go_opt=Mbase/base.proto=<my_project_package_name>/pb/base
. See https://protobuf.dev/reference/go/go-generated/#package for details.
Thanks, that's helpful.
Hi, I would like to reuse some of the
proto
files among different Golang projects. For a simple example, I have a project with such structure:The
base.proto
defines some basic message used by all the other folders. So I useimport "base.base.proto"
in otherproto
files. I was told to addgo_package
option to give the package name of the generated Golang source files. But if I use./user
foruser.proto
andservice.proto
and./base
forbase.proto
. The generateduser.pb.go
will import./base
which can't be resolved by compiler. I use the following command to generate the proto files.So the question is, how could I make the generated source file import the right package name. For example, importing
<my_project_package_name>/pb/base
rather than./base
inuser.pb.go
.