protoc go_package 的正确格式?

Tim*_*Tim 22 protocol-buffers protoc grpc-go

我在 Go 中有一个现有项目,我正在使用协议缓冲区/gRPC。直到最近,该go_package选项还是可选的,并且生成的 Go 包名称将与 proto 包名称相同。

该文件位于项目根目录中。生成的代码文件 ( authenticator.pb.go) 位于同一位置。原型文件:

syntax = "proto3";

package authenticator;

service Authenticator {...}
Run Code Online (Sandbox Code Playgroud)

生成命令指定我要在同一目录中输出:

protoc --go_out=plugins=grpc:. authenticator.proto
Run Code Online (Sandbox Code Playgroud)

今天我拉取了新版本的协议缓冲区编译器和github.com/golang/protobuf/protoc-gen-go. 第一次运行时收到警告:

WARNING: Missing 'go_package' option in "authenticator.proto",
please specify it with the full Go package path as
a future release of protoc-gen-go will require this be specified.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
Run Code Online (Sandbox Code Playgroud)

建议的链接或多或少没用。但是教程更明确一点:

go_package 选项定义包的导入路径,该路径将包含此文件的所有生成代码。Go 包名称将是导入路径的最后一个路径组件。例如,我们的示例将使用包名称“tutorialpb”。

option go_package = "github.com/protocolbuffers/protobuf/examples/go/tutorialpb";
Run Code Online (Sandbox Code Playgroud)

将此选项添加到 proto 文件并重新运行命令后,输出最终位于此路径中,相对于项目根目录。就像是:

$GOPATH/src/github.com/<org>/authenticator/github.com/<org>/authenticator/authenticator.pb.go
Run Code Online (Sandbox Code Playgroud)

我已经尝试了以下替代go_package名称:

  • .
  • authenticator

生成发生在正确的位置,但我得到了警告:

WARNING: Deprecated use of 'go_package' option without a full import path...
Run Code Online (Sandbox Code Playgroud)

那么不破坏项目布局的正确方法是什么?

msc*_*ett 24

对于纯原始世代,您可以执行以下操作

protoc --go_out=paths=source_relative:./gen -I. authenticator.proto
Run Code Online (Sandbox Code Playgroud)

或者为 grpc 生成以下可以使用

protoc --go_out=plugins=grpc:./gen --go_opt=paths=source_relative authenticator.proto
Run Code Online (Sandbox Code Playgroud)

如果失败,您可能有更新版本的 protoc,需要使用以下命令。

protoc --go-grpc_out=./gen --go-grpc_opt=paths=source_relative authenticator.proto
Run Code Online (Sandbox Code Playgroud)

这只是 Google 再次就您应该如何编写和管理代码做出孤立的决定。但是,如果您深入研究了有类似问题的人的足够多的问题,我发现现在可以使用上述命令。除了添加其他答案中所示的完整导入路径

option go_package = "github.com/example/path/gen;gen";
Run Code Online (Sandbox Code Playgroud)

按照上述包和 protoc 命令,您将在项目根目录中拥有 proto 文件,模块名称为 github.com/example/path。然后,您的代码将放置在您负责创建的 gen 文件夹中。

您可能需要调整 source_relative 的输出位置以供您使用,但至少您不会以路径重复或不得不再次将代码置于 GOPATH 中而告终。

由于此票证中的其他更改,这可能会中断。不幸的是,二进制名称完全相同,因此如果您遇到问题,请阅读该票证并尝试切换您已安装的版本,直到新的 protoc-gen-go 准备好进行生产。

  • 看起来 `--go_out=plugins=grpc` 中的插件部分不再受支持。我能够运行以下命令以获得类似的结果:`protoc --go-grpc_out=./gen --go-grpc_opt=paths=source_relativeauthenticator.proto` (4认同)

小智 7

你只需要添加一个 Go 选项,它会告诉你添加相对于 proto 文件的文件。

protoc --go_out=. --go_opt=paths=source_relative your_file.proto 
Run Code Online (Sandbox Code Playgroud)

这将使 your_file.pb.go 与 your_file.proto 位于同一目录中

  • `--go_opt=paths=source_relative` 绝对是 Go 的方法。我排除了另一个答案,因为它包含有关该问题的更多背景信息。 (2认同)

Nel*_*ila 7

您好,我将通过阅读文档逐步解释我是如何生成它的

创建文件夹grpcgo

mkdir grpcgo && cd grpcgo
Run Code Online (Sandbox Code Playgroud)

创建项目

    go mod init projectxxx
    ## Download and install dependencies
    go get google.golang.org/protobuf/cmd/protoc-gen-go
    go get github.com/golang/protobuf

   sudo snap install protobuf or sudo apt  install protobuf-compiler
Run Code Online (Sandbox Code Playgroud)

创建文件夹 proto 和内部文件夹文件 .proto

愿望清单.proto

syntax = "proto3";

package grpcgo; #Folder

option go_package = "pb;gen"; <-- Add 

message Item {
    enum ItemPriority {
        LOW = 0;
        MID = 50;
        HIGH = 100;
    }

    enum ItemStatus {
        INACTIVE = 0;
        ACTIVE = 1;
    }

    string id = 1;
    string wishListId = 2;
    string name = 3; 
    string link = 4;
    double price = 5;

    ItemPriority priority = 6;
    ItemStatus status = 7;

}

message WishList {
    enum WishListStatus {
        INACTIVE = 0;
        ACTIVE = 1;
    }

    string id = 1;
    string name = 2;
    WishListStatus status = 3;

}
Run Code Online (Sandbox Code Playgroud)

编译原型

protoc -I=$PWD --go_out=$PWD $PWD/proto/*.proto
Run Code Online (Sandbox Code Playgroud)

文档:

https://developers.google.com/protocol-buffers/docs/gotutorial

https://blog.friendsofgo.tech/posts/introduccion-a-grpc/

版本:

github.com/golang/protobuf v1.5.2

google.golang.org/protobuf v1.26.0

go版本go1.16.5 linux/amd64

#编辑

$ protoc -I=$PWD --go_out=$PWD $PWD/proto/*.proto
protoc-gen-go: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go_out: protoc-gen-go: Plugin failed with status code 1.
Run Code Online (Sandbox Code Playgroud)

解决方案是安装 protoc-gen-go

mkdir grpcgo && cd grpcgo
Run Code Online (Sandbox Code Playgroud)

编辑2

在根项目中创建目录 pb

protoc -I=$PWD --go_out=$PWD/pb $PWD/protos/*.proto

syntax = "proto3";

package grpcgo;

option go_package = "../pb"; <--

message Item {
    enum ItemPriority {
        LOW = 0;
        MID = 50;
        HIGH = 100;
    }

    enum ItemStatus {
        INACTIVE = 0;
        ACTIVE = 1;
    }

    string id = 1;
    string wishListId = 2;
    string name = 3; 
    string link = 4;
    double price = 5;

    ItemPriority priority = 6;
    ItemStatus status = 7;

}

message WishList {
    enum WishListStatus {
        INACTIVE = 0;
        ACTIVE = 1;
    }

    string id = 1;
    string name = 2;
    WishListStatus status = 3;

}


Run Code Online (Sandbox Code Playgroud)

列出文件项目

    go mod init projectxxx
    ## Download and install dependencies
    go get google.golang.org/protobuf/cmd/protoc-gen-go
    go get github.com/golang/protobuf

   sudo snap install protobuf or sudo apt  install protobuf-compiler
Run Code Online (Sandbox Code Playgroud)

来自阿根廷的拥抱


sou*_*dit 6

如果您使用的是syntax = "proto3"; 。grpc 代码生成处理更改后必然会发生此问题。解决此问题

您可以按照文档https://protobuf.dev/reference/go/go- generated/#package

第 1 步:将此行粘贴到your_file_with.proto中

 option go_package = "pb;gen";
   
Run Code Online (Sandbox Code Playgroud)

步骤2:假设你想生成pb文件夹中的所有文件,用你合适的名称替换pb,然后在终端窗口中运行cmd。在我的例子中,我的所有proto文件都在项目目录的proto文件夹中

 protoc -I=proto --go_out=. proto/*.proto
Run Code Online (Sandbox Code Playgroud)


mon*_*rus 5

当我添加时,上述错误消失了

option go_package = "github.com/monkrus/grpc-from0;grpc_from0"; 如下:

聊天.proto

  • 这修复了错误,但仍然给您留下了现有项目的不理想的文件位置。 (2认同)

小智 5

我正在使用 Go,我想在与我所做的*.pb.go相同的文件夹中创建*.proto*.proto

option go_package = "./{Your folder name}";
Run Code Online (Sandbox Code Playgroud)

请注意,Your folder name将反映在生成的*.pb.go包中。然后运行:

protoc --go_out=.. *proto
Run Code Online (Sandbox Code Playgroud)