protoc可以从两个属于不同包的proto文件生成Golang代码。

huangapple go评论79阅读模式
英文:

protoc generate golang code from two proto files which belong to different package

问题

我有两个proto源文件,位于同一个文件夹中,假设为:

第一个是 foo.proto

syntax = "proto3";
package foo;

第二个是 bar.proto,需要导入 foo.proto

syntax = "proto3";
import "foo.proto";
package bar;

你可以看到它们有不同的包名。当我使用protoc逐个生成它们时(当然先生成foo.proto),我得到了两个生成的golang文件,但我必须将它们放入两个目录中(你不能将不同包名的文件放在同一个目录中,golang不支持):

foo/foo.pb.go
bar/bar.pb.go

但是在 bar.pb.go 中,导入路径使用的是本地导入路径,类似于:

import foo ".";

我尝试了一些protoc提供的选项,但没有使其工作,有什么建议吗?

英文:

I have two proto src file, in same folder, let's say:

the 1st one is foo.proto

syntax = "proto3";
package foo;

the second is bar.proto, which need import foo.proto

syntax = "proto3";
import "foo.proto";
package bar;

you can see they have different package name, when I use protoc to generate them one by one, (foo.proto as the 1st of cause), I have two golang file generated, but I have to put them into 2 directory (you can't put different package name file inside same directory, golang )

foo/foo.pb.go
bar/bar.pb.go

but inside bar.pb.go the import are using local import path, which is like

import foo "."

I'm tweeking several options which protoc provided, but didn't make this work, any suggestion?

答案1

得分: 1

我相信添加option go_package应该是正确的解决方案。

英文:

I believe add option go_package should be the correct solution

huangapple
  • 本文由 发表于 2017年6月22日 11:26:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/44689628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定