恢复为protoc-gen-go的早期版本

问题描述

我正在尝试将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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...