You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新版protoc-gen-go无go_package时生成Go代码报错如何解决

解决方案

你遇到的错误由两个配置遗漏导致:

  • M参数匹配规则错误:M参数后需要匹配的是proto文件的逻辑导入路径,而非本地绝对路径。你已经通过-I/shared/api指定了proto导入根目录,因此两个proto文件的逻辑路径应为antivirus/proto/v1/antivirus.proto和antivirus/proto/v1/structures.proto,你之前填写的绝对路径无法匹配到目标proto,导致配置不生效。
  • 缺少gRPC插件的包路径配置:新版protoc将Go gRPC代码的生成拆分到了protoc-gen-go-grpc插件,除了给protoc-gen-go传--go_opt参数,还需要给protoc-gen-go-grpc传完全相同的--go-grpc_opt参数,否则gRPC插件也无法识别proto对应的Go包路径。

正确的生成命令示例如下(如果需要和旧版行为一致,使用包名antivirus_proto_v1,可直接替换M参数后的Go包路径值):

protoc -I/usr/local/include -I/shared/api \
  --go_out=generated \
  --go_opt=Mantivirus/proto/v1/antivirus.proto=antivirus/proto/v1 \
  --go_opt=Mantivirus/proto/v1/structures.proto=antivirus/proto/v1 \
  --go-grpc_out=generated \
  --go-grpc_opt=Mantivirus/proto/v1/antivirus.proto=antivirus/proto/v1 \
  --go-grpc_opt=Mantivirus/proto/v1/structures.proto=antivirus/proto/v1 \
  antivirus/proto/v1/antivirus.proto antivirus/proto/v1/structures.proto

可选配置:如果你希望生成的Go文件直接输出到generated根目录,而非按Go包路径生成嵌套子目录,可以额外添加--go_opt=paths=source_relative和--go-grpc_opt=paths=source_relative参数。

内容的提问来源于stack exchange,提问作者DeYuro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 13:27:05