Hi,
We are defining our data model/api in proto files. We'd like to organize them into multiple proto files.
I run protoc for each .proto file to generate the corresponding .pb.go file.
However, if b.proto is dependent on a.proto file, I found that in b.pb.go, it seems not able to reference the objet in a.pb.go properly.
Is there any code sample on organizing multiple proto files?
Yeah, the example in the protobuf repo uses only a single proto file -- https://github.com/google/protobuf/tree/master/examples
To make sure that protoc sees all the proto import dependencies, you want to set -I or --proto_path as mentioned in this doc -- https://developers.google.com/protocol-buffers/docs/proto#importing-definitions
The --proto_path value is where you have all the .proto files, possibly under different directories. The import statements inside the proto file needs to match this proto path value to corresponding .proto files being imported.
When generating the Go proto sources, the proto package name is generally used as the Go package (with some translation) unless you have specified a go_package option or if you passed parameters to --go_out as mentioned in https://github.com/golang/protobuf#parameters (this latter case overrides the other ones).
Example using go_package...
src/foo/a.proto
package a;
option go_package = "qux/foo";
src/bar/b.proto
import "foo/a.proto";
package b;
option go_package = "qux/bar";
Under src, generate the pb.go files using ...
$ protoc --proto_path=. --go_out=. foo/a.proto
This will generate src/qux/foo/a.pb.go with Go package qux/foo
$ protoc --proto_path=. --go_out=. bar/b.proto
This will generate src/qux/bar/b.pb.go with Go package qux/bar. Generated code will have an import "qux/foo"
Hope that helps.
Most helpful comment
Yeah, the example in the protobuf repo uses only a single proto file -- https://github.com/google/protobuf/tree/master/examples
To make sure that protoc sees all the proto import dependencies, you want to set -I or --proto_path as mentioned in this doc -- https://developers.google.com/protocol-buffers/docs/proto#importing-definitions
The --proto_path value is where you have all the .proto files, possibly under different directories. The import statements inside the proto file needs to match this proto path value to corresponding .proto files being imported.
When generating the Go proto sources, the proto package name is generally used as the Go package (with some translation) unless you have specified a go_package option or if you passed parameters to --go_out as mentioned in https://github.com/golang/protobuf#parameters (this latter case overrides the other ones).
Example using go_package...
Under src, generate the pb.go files using ...
This will generate src/qux/foo/a.pb.go with Go package qux/foo
This will generate src/qux/bar/b.pb.go with Go package qux/bar. Generated code will have an import "qux/foo"
Hope that helps.