douyun4524 2019-06-18 10:11
浏览 831

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 ?

  • 写回答

1条回答 默认 最新

  • duansha7025 2019-06-18 14:47
    关注

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了