protoc-gen-go: 无法确定 “simple.proto” 的 Go 导入路径。

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

protoc-gen-go: unable to determine Go import path for "simple.proto"

问题

我有一个包含以下内容的简单proto文件。

syntax="proto3";

package main;

message Person {
      string name = 1;
      int32 age = 2; 
}

我正在尝试使用protoc生成go代码。我运行了以下命令:

protoc --go_out=. simple.proto

我收到以下错误:

protoc-gen-go: 无法确定“simple.proto”的Go导入路径

请指定以下之一:
        • 在.proto源文件中使用“go_package”选项,或
        • 在命令行上使用“M”参数。

main.gogo.modsimple.proto位于同一个文件夹中。protocprotoc-gen-go都在PATH环境变量中定义。

英文:

I have simple proto file with following content.

syntax="proto3";

package main;

message Person {
      string name = 1;
      int32 age = 2; 
}

I am trying to generate go code for it using protoc. I run:

protoc --go_out=. simple.proto

I receive following error:

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.

main.go, go.mod and simple.proto is in the same folder. Both protoc and protoc-gen-go are defined in PATH enviroement.

答案1

得分: 15

你忘记了通过添加以下代码将文件与链表链接起来:

option go_package = "./";

你需要先将其链接起来才能使其正常工作。这个问题在这里也有相同的解决方法。

英文:

You forgot to linkedlist the file with it by adding:

option go_package = "./";

You need to linkedlist it first to make it work. It was same issues here

答案2

得分: 13

Protoc要求指定包名,解决方法是添加以下代码:

option go_package = "./your-package-name";

使你的文件看起来像这样:

syntax="proto3";

package main;

option go_package = "./your-package-name";

message Person {
      string name = 1;
      int32 age = 2; 
}

然后你可以运行以下命令:

protoc -I src/ --go_out=src/ src/simple/simple.proto

其中 --go_out=src/ 指定了生成文件的位置,然后是相对于你的proto文件的路径。

注意: 不要忘记在 option go_package 前加上 ./

英文:

Protoc requires that the package be specified, then the solution is to add

option go_package = "./your-package-name";

to make your file looks like the following:

syntax="proto3";

package main;

option go_package = "./your-package-name";

message Person {
      string name = 1;
      int32 age = 2; 
}

then you can run the command e.g:

protoc -I src/ --go_out=src/ src/simple/simple.proto

where --go_out=src/ specifies where your file will be generated then the relative path to your proto file.

Note: Don't forget to prefix the option go_package with ./

答案3

得分: 9

你缺少了option go_package

你将给option go_package指定的名称将成为由protoc生成的包的名称。通过这样做,你可以导入并访问消息字段。

英文:

You are missing option go_package.

The name you will give to option go_package will be the name of the package that will be generated by the protoc. By doing so, you can import thus access message fields.

答案4

得分: 0

我有一个类似的问题。

我以为协议缓冲区应该是语言中立的。如果我们在proto文件中添加go_package,那么如果我们尝试将这些proto文件编译成其他语言,我们将不得不对文件进行更改。

我看到的解决方案只适用于生成go文件。

英文:

I have a similar problem.

I thought protocol buffers were supposed to be language neutral. If we are adding go_package to the proto files, then if we try to compile these proto files to a different language, we will have to make changes to the files.

The solutions I have seen works if you are looking at generating only go files.

答案5

得分: -2

首先确保你正确安装了编译器

sudo apt install protobuf-compiler
sudo apt install golang-goprotobuf-dev

使用以下命令

protoc -I=src/ --go_out=src/ src/simple.proto

-I = IPATH - 指定导入文件的搜索目录 <br/>
--go_out= 输出目录

英文:

first make sure you installed compiler correctly

sudo apt install protobuf-compiler
sudo apt install golang-goprotobuf-dev

use this command

protoc -I=src/ --go_out=src/ src/simple.proto

-I = IPATH -Specify the directory in which to search for imports <br/>
--go_out= output directory

huangapple
  • 本文由 发表于 2022年1月5日 08:22:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/70586511.html
匿名

发表评论

匿名网友

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

确定