环境: protobuf2 ,golang

项目test下 存放proto的目录结构

---test // 项目根
------ cs 
      --- cs.proto
------ ss
     --- ss.proto

cs.proto

syntax = "proto2";
package cs;

message Test{
    optional string name = 1;
}

ss.proto

syntax = "proto2";
package ss;
import "test/cs/cs.proto";

message Test{
    optional cs.Test cstest = 1;
}

ss.proto 文件引用 cs/cs.proto

Backslashes, consecutive slashes, ".", or ".." are not allowed in the virtual pathtest/cs/cs.proto
protoc -I ../../ -I ./ --go_out=. *.proto // 此条命令在 ss 目录下运行,可根据自己习惯在不同地址运行,但是-I 参数需注意
protoc -I ../ -I ./ --go_out=. *.proto // 此条命令在 ss 目录下运行

ss.proto
import "cs/cs.proto";

组合后 “../cs/cs.proto" 。看似相对路径组合后没毛病。protoc 编译也能通过。
但是 生成的对应 cs.pb.go 文件,里的import 路径就是 "cs" 。这个go工程是编译不过的。go项目引用需要从gopath 路径过来。即 “test/cs “。

总结:

  1. proto 文件的 import 需要从 gopath 路径过来(即项目名开始的绝对路径)。
  2. protoc -I 路径需要到 gopath。