protoc在go源文件上生成导入路径

I have a pb file in $GOPATH/src/github.com/cs/p/a/a.proto, and another pb file in $GOPATH/src/github.com/cs/p/b/b.proto. And a.proto import b.proto like this:

import "b/b.proto"

Now I enter $GOPATH/src/github.com/cs/p and execute the following command:

protoc --go_out=plugins=grpc:. a/a.proto

Then I find a.pb.go is generated in $GOPATH/src/github.com/cs/p/a/, within which there's such statement: import "b".

When I build the project, it says it cannot find package b. What should I do ? Acutally I hope a.pb.go import b like this: import github.com/cs/p/b. Could it be done ?

Acutally I hope a.pb.go import b like this: import github.com/cs/p/b. Could it be done ?

Yes!

You need to include an import path during the protoc compilation step. So assuming your git source lives under the path $GOPATH/src, you would add the include flag "-I.:$GOPATH/src" like so:

protoc --go_out=plugins=grpc:. "-I.:$GOPATH/src" a/a.proto

Once imported, to reference any message types, use the scope path <package name>.<message name>.

For example, the (git hosted) definition:

// this file resides here: ${GOPATH}/src/github.com/bib/pb/person.proto
package tutorial;

message Person {
  string name = 1;
}

would be imported and referenced like so:

import "github.com/bib/pb/person.proto"

message AddressBook {
  repeated tutorial.Person people = 1;
}