How am I supposed to use protoc-gen-go-grpc?

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

How am I supposed to use protoc-gen-go-grpc?

问题

我正在尝试为一些协议缓冲区生成Go代码以及一个gRPC服务。

过去我使用过 https://github.com/golang/protobuf,生成命令看起来像这样:

//go:generate protoc --proto_path=. --go-out=. --go_opt=paths=source_relative <file>.proto
//go:generate protoc --proto_path=. --go_grpc_out=. --go_grpc_opt=paths=source_relative <file>.proto

这种方法效果很好,但是该存储库已被 google.golang.org/protobufgoogle.golang.org/grpc 取代。

根据我所了解,这是一个有意的分离,以分开协议缓冲区和gRPC项目的发布周期。

使用新的存储库,我像这样安装了工具:

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

两个工具都已安装,并且在路径上可见。我遇到的问题是 protocprotoc-gen-go 正在寻找旧的工具 - protoc-gen-go_grpc。请注意下划线。

我已经阅读了所有我能找到的文档和Github问题,但还没有弄清楚我应该如何使用新的 protoc-gen-go-grpc。请注意破折号。

$ echo $GOPATH
/Users/<username>/dev/go

$ echo $GOBIN
/Users/<username>/dev/go/bin

$ which protoc
/opt/homebrew/bin/protoc

$ which protoc-gen-go
/Users/<username>/dev/go/bin/protoc-gen-go

$ which protoc-gen-go_grpc
protoc-gen-go_grpc not found

$ which protoc-gen-go-grpc
/Users/<username>/dev/go/bin/protoc-gen-go-grpc
英文:

I am trying to generate Go code for some Protocol Buffers as well as a gRPC service.

In the past I have used https://github.com/golang/protobuf with a generate command that looked something like this:

//go:generate protoc --proto_path=. --go-out=. --go_opt=paths=source_relative &lt;file&gt;.proto
//go:generate protoc --proto_path=. --go_grpc_out=. --go_grpc_opt=paths=source_relative &lt;file&gt;.proto

This method worked just fine, but the repo has since been superceded by google.golang.org/protobuf and google.golang.org/grpc.

From what I've read, this was an intentional split to separate the protobuf and gRPC project release cycles.

Using the new repositories, I installed the tools like this:

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

Both tools are installed, and are visible on the path. The problem that I'm encountering is that protoc or protoc-gen-go is looking for the older tool - protoc-gen-go_grpc. Note the underscore.

I have been reading all the documentation and Github issues that I can find, and haven't yet been able to figure out how I am meant to use the new protoc-gen-go-grpc. Note the dash.

$ echo $GOPATH
/Users/&lt;username&gt;/dev/go

$ echo $GOBIN
/Users/&lt;username&gt;/dev/go/bin

$ which protoc
/opt/homebrew/bin/protoc

$ which protoc-gen-go
/Users/&lt;username&gt;/dev/go/bin/protoc-gen-go

$ which protoc-gen-go_grpc
protoc-gen-go_grpc not found

$ which protoc-gen-go-grpc
/Users/&lt;username&gt;/dev/go/bin/protoc-gen-go-grpc

答案1

得分: 8

我的问题非常简单,但是找起来真是让人烦恼。

当运行 protoc 时,使用 --go_grpc_out 标志会寻找 protoc-gen-go_grpc

当运行 protoc 时,使用 --go-grpc_out 标志会寻找 protoc-gen-go-grpc

看起来,protoc 没有定义明确的、有文档支持的标志,并且在提供了错误的标志时不会报错,而是使用这些标志来确定使用哪个插件。如果你拼写错误了标志,它会寻找错误的插件。

英文:

My issue was so simple to fix, but darn annoying to find.

When running protoc, using the --go_grpc_out flag looks for protoc-gen-go_grpc.

When running protoc, using --go-grpc_out flag looks for protoc-gen-go-grpc.

It seems that instead of protoc having well defined, documented flags, and erroring when an incorrect flag is provided they're using the flags to determine which plugin to use. If you misspell the flag, it looks for the wrong plugin.

huangapple
  • 本文由 发表于 2022年1月10日 00:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/70643183.html
匿名

发表评论

匿名网友

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

确定