与 protoc-gen-go 相同的症状:无法确定“simple.proto”的 Go 导入路径
对于具有以下内容的简单原型文件。
syntax="proto3"; package main; message Person { string name = 1; int32 age = 2; }
我正在尝试使用 protoc 为其生成 go 代码。我跑:
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative simple.proto
我收到以下错误:
protoc-gen-go: unable to determine Go import path for "simple.proto" Please specify either: • a "go_package" option in the .proto source file, or • a "M" argument on the command line.
那里的所有答案都集中在 first 选项上——添加
a "go_package" option in the .proto source file
,但我正在寻找命令行上 second 选项 ”a “M”参数 的答案”。
与https://stackoverflow.com/a/62540631/2125837下的评论相同
我正在寻找通过protoc
go_opt=module
但它不起作用与
source_relative
。有什么方法可以通过添加
“在命令行上添加“M”参数”来使其工作,而不是在 .proto 源文件中添加“go_package
”选项吗?具体来说,对于以下文件
https://github.com/mmcc007/go/blob/master/examples/helloworld/helloworld/helloworld.proto
这是我失败的尝试:
$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --proto_path=examples/helloworld/helloworld helloworld.proto
protoc-gen-go: unable to determine Go import path for "helloworld.proto"
. . .
--go_out: protoc-gen-go: Plugin failed with status code 1.
$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --proto_path=examples/helloworld/helloworld --go_opt=Mhelloworld.proto=github.com/mmcc007/go/blob/master/examples/helloworld/helloworld helloworld.proto
protoc-gen-go-grpc: unable to determine Go import path for "helloworld.proto"
. . .
--go_out: protoc-gen-go: Plugin failed with status code 1.
--proto_path
轻松参考“THE M FLAG”:-)
protoc --proto_path=./proto \
--go_out=./go \
--go_opt=Mhelloworld.proto=example.com/project/protos/fizz \
./proto/helloworld.proto
注意:helloworld.proto 之前的 M
syntax = "proto3";
option go_package = "pkg/api";
这对于 java 和 python 来说都是一样的。
option java_package = "pkg/api";
option py_generic_services = "pkg/api";
至于命令生成文件
PROJ_PATH=${CURDIR}
.PHONY: proto
proto: ## Generate protobuf code
# Compile proto files inside the project.
protoc api.proto --proto_path=${PROJ_PATH}/proto --go_out=. --go-grpc_out=.
M 参数只有在将其同时添加为 go_opt
和
go-grpc_opt
时才有效。例如。对于位于
proto/src/another/folder/helloworld.proto
:的原始文件
protoc --proto_path=proto/src \
--go_out=. \
--go-grpc_out=. \
--go_opt=Manother/folder/helloworld.proto=example.com/project/protos/fizz \
--go-grpc_opt=Manother/folder/helloworld.proto=example.com/project/protos/fizz \
proto/src/another/folder/helloworld.proto
在这种情况下,文件位于 proto/src/another/folder/helloworld.proto