问题描述
我正在尝试将protobuf文件编译成golang。编译后,我在生成的pb.go中看到了这一点
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.23.0
// protoc v3.12.3
和
// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsversion4
但是,以前生成的pb.go具有
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsversion3 // please upgrade the proto package
使用proto软件包的第4版,代码会中断。如何进行编译,使其在版本3中具有ProtoPackage?
解决方法
问题似乎与protobuf
lib的旧版本有关。 ProtoPackageIsVersion3
适用于1.3。*,ProtoPackageIsVersion4
适用于1.4+,您可以在此处查看更新:https://github.com/golang/protobuf/releases/tag/v1.4.0
我建议您更新到较新的版本,例如googleapis也依赖于较新的版本。参见https://developers.google.com/protocol-buffers/docs/reference/go/faq#enforce-version-apiv1
如果由于某种原因,您仍然需要使用所有版本的较旧版本,则将需要protobuf
版本1.3.5,最有可能手动构建proto-gen-go
和protoc
以进行匹配。您可以通过以下步骤查看类似的问题:https://github.com/golang/protobuf/issues/1090