【问题标题】:Const initializer 'protoimpl.EnforceVersion(20 - protoimpl.MinVersion)' is not a constant常量初始化器 'protoimpl.EnforceVersion(20 - protoimpl.MinVersion)' 不是常量
【发布时间】:2020-07-07 11:02:02
【问题描述】:

我是 gRPC 的新手,尝试从 protos 生成 go 代码并在生成的文件中出现此错误

生成的代码有问题:

//  protoc-gen-go v1.25.0
//  protoc        v3.12.3
const (
    // Verify that this generated code is sufficiently up-to-date.
    _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
    // Verify that runtime/protoimpl is sufficiently up-to-date.
    _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)

错误

Const initializer 'protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)' is not a constant

版本:

$ sw_vers                                                                                                                                                                                                 (sadc1c/opsvisibility)
ProductName:    Mac OS X
ProductVersion: 10.15.3
BuildVersion:   19D76

$ go version
go version go1.14 darwin/amd64

$ protoc --version                                                                                                                                                                                        (sadc1c/opsvisibility)
libprotoc 3.12.3

【问题讨论】:

  • 这能回答你的问题吗? Why doesn't Golang allow const maps?
  • 链接副本解释了可以用作常量的内容。函数不能(除了一些内置函数)。请改用var 节。
  • 不是我写的代码,是protoc工具生成的
  • 啊,公平点。重复的标志已撤消。

标签: go protocol-buffers protoc grpc-go


【解决方案1】:

原因是包google.golang.org/protobuf的错误版本 解决办法是获取最新版本

go get -u google.golang.org/protobuf/proto

来源:https://developers.google.com/protocol-buffers/docs/reference/go/faq#enforce-version

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多