解决protoc-gen-go:通过添加“M”参数无法确定Go导入路径问题

问题描述 投票:0回答:3

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代码,我尝试使用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.
    
go protocol-buffers protoc grpc-go
3个回答
7
投票
我发现这可以使用

--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


2
投票
需要在命令中或者proto文件中添加路径

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=.
    

0
投票
对我来说,经过几个小时的挣扎,我发现

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


    

© www.soinside.com 2019 - 2024. All rights reserved.