我可以肯定这是操作员错误,并且我当时的想法还不清楚。

这是设置:

$GOPATH/src/github.com/<company>/<service a>/proto/a.proto
$GOPATH/src/github.com/<company>/<service b>/proto/b.proto

等等

a.protoimport "github.com///b.proto"

我可能有两个不同的问题。

go:generate protoca.pb.go$GOPATH/src/github.com///proto/
Using option go_package = "github.com///proto".proto

go生成多种变体;

go:generate protoc --proto_path=.:$GOPATH/src --go_out=$GOPATH/src a.proto
go:generate protoc --proto_path=.:$GOPATH/src --go_out=. a.proto 
go:generate protoc --go_out=import_prefix=github.com/<company>/:. api.proto

I clearly have a poor understanding on how protoc looks at import paths and file outputs. Anyone point me in the direction of what I am doing wrong?

Thanks!

Update #1 In a.proto;

option go_package = "github.com/<company>/<service a>/proto";

import "github.com/<company>/<service b>/proto/b.proto";

and the go generate;

//go:generate protoc --proto_path=$GOPATH/src --go_out=$GOPATH/src/github.com/<company>/<service a>/proto a.proto

Which is called from a go file in the proto directory with the a.proto.

I received the error; a.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path ch encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).

I have confirmed $GOPATH is to the expected location.

Solution

感谢Shivam Jindal向我指出正确的方向。导入与他的解决方案完全相同。输出是我滥用--go_out和选项go_package的问题。我使用go_package来指定输出位置,并使用--go_out来指定类似于--proto_path的根目录。现在一切正常。

option go_package = "github.com/<company>/<service a>/proto";

//go:generate protoc --proto_path=$GOPATH/src/ --go_out=$GOPATH/src/ $GOPATH/src/github.com/<company>/<service a>/proto/a.proto

谢谢!