英文:
protobuf golang import .proto and .pb.proto from different directories
问题
我有一个名为myProtos的库,它的结构如下:
.
|-- proto
|---- hello.proto
|
|-- generated
└---- hello.pb.go
我有一个位于外部的.proto文件,名为example.proto
,它应该导入hello.proto
。
所以文件的开头应该是这样的:
syntax = "proto3";
package example;
import "path/to/myProtos/proto/hello.proto"
现在,当我编译example.proto
时,我在example.pb.go
上得到一个导入错误,因为它有这样的导入行:import "path/to/myProtos/proto/hello.pb.go"。
我尝试添加两个导入路径,但是我得到一个"import but not used error"。我还尝试了相对导入,并将两个目录作为标志传递给protoc,这样可以工作,但是我需要go文件中的导入路径是绝对路径。
我如何告诉protoc在go文件中路径不同?
在这种情况下,有更好的"最佳实践"吗?
英文:
I have a library called myProtos which looks like this
.
|-- proto
|---- hello.proto
|
|-- generated
└---- hello.pb.go
I have a .proto file outside called example.proto
that should import hello.proto
So the top of the file looks like this:
syntax = "proto3";
package example;
import "path/to/myProtos/proto/hello.proto"
Now, when I compile example.proto
I get an import error on example.pb.go
because it has the import line import "path/to/myProtos/proto/hello.pb.go"
I tried adding both import paths, but I get an 'import but not used error'. I also tried doing relative imports and passing both directories as flags to protoc, which worked, but I need the import path in the go file to be absolute.
How can I tell protoc that on the go file the path is different?
Is there a better 'best practice' in this case?
答案1
得分: 2
对我来说有效的方法是定义option go_package = "github.com/<your-account>/<your-cool-project>/<sub-dirs>"
。
假设你有如下的文件夹结构:
.
|-- proto
|---- hello.proto
|
|-- generated
└---- hello.pb.go
在你的情况下,你需要在hello.proto
中添加option go_package = "<path>/<in>/<GOPATH>/generated"
。
重要的是,你需要运行以下命令:
protoc -I. --go_out=$GOPATH ./*.proto
通常情况下,我会将生成的Go文件与proto文件放在一起,以保持proto文件和Go文件的导入路径相同。但这可能是个人喜好的问题。在这种情况下,你只需将option go_package = "<path>/<in>/<GOPATH>/proto"
设置为hello.proto
。
无论哪种情况,.proto
文件的相对导入现在应该能正确解析为生成的Go代码中的正确导入,并且.pb.go
文件也应该被放置在正确的Go包文件夹中。
英文:
What worked for me is to define option go_package = "github.com/<your-account>/<your-cool-project>/<sub-dirs>
Let's assume you have the folder structure your stated:
.
|-- proto
|---- hello.proto
|
|-- generated
└---- hello.pb.go
In your case you would add option go_package = "<path>/<in>/<GOPATH>/generated"
to hello.proto
.
What is important is that you then have to run
protoc -I. --go_out=$GOPATH ./*.proto
Generally, I would generate the go files alongside the proto files to keep the import paths the same for proto and go files. But this might be a matter of taste. In that case you would then simply set option go_package = "<path>/<in>/<GOPATH>/proto"
to hello.proto
.
In both cases a relative import of the .proto
file should now resolve to the proper import in the generated Go code and the .pb.go
files should also be put into the proper Go package folders.
答案2
得分: -1
在你的hello.proto
文件中使用package generated;
。
然后,使用protoc -I proto/ proto/*.proto --go_out=generated
命令将根据generated
包名在generated
文件夹中生成一个hello.pb.go
文件。
proto文件中的包声明告诉protobuf生成器在生成的文件中使用哪个包。
英文:
Use package generated;
inside your hello.proto
file.
Then, protoc -I proto/ proto/*.proto --go_out=generated
will generate a hello.pb.go
inside generated
folder by the package name of generated
.
The package inside the proto files tells the protobuf generator which package to use inside the generated file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论