Go使用proto3的踩坑实战记录

开发环境:windows10,golang1.18.2,goland2022.2

最近在写项目时,一些数据类的结构以protobuf文件给定。因此,需要将这些protobuf文件转换为golang代码。

首先,在下载解析protobuf的包的时候就碰到了第一个问题...

go get -u github.com/golang/protobuf/protoc-gen-go

在我用上述命令后,终端提示该包已弃用

go: module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead.

随后直接用给出的新地址替换,这一次终端没有给出任何提示,但在GOPATH的bin目录下并没有出现想要的protoc-gen-go.exe文件。

故再次使用go install

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

成功安装上述可执行文件。

再次尝试解析.proto文件,报错:

'protoc-gen-go' 不是内部或外部命令,也不是可运行的程序或批处理文件。

又是因为没有将GOPATH\bin目录添加至环境变量中。。添加后测试终端输入:

$:protoc
Usage: D:\Application\protoc-21.7-win64\bin\protoc.exe [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
                              If not found in any of the these directories,
                              the --descriptor_set_in descriptors will be
                              checked for required proto file.
  --version                   Show version info and exit.
  -h, --help                  Show this text and exit.
  --encode=MESSAGE_TYPE       Read a text-format message of the given type
                              from standard input and write it in binary
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --deterministic_output      When using --encode, ensure map fields are
                              deterministically ordered. Note that this order
                              is not canonical, and changes across builds or
                              releases of protoc.
  --decode=MESSAGE_TYPE       Read a binary message of the given type from
                              standard input and write it in text format
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode_raw                Read an arbitrary protocol message from
                              standard input and write the raw tag/value
                              pairs in text format to standard output.  No
                              PROTO_FILES should be given when using this
                              flag.
  --descriptor_set_in=FILES   Specifies a delimited list of FILES
                              each containing a FileDescriptorSet (a
                              protocol buffer defined in descriptor.proto).
                              The FileDescriptor for each of the PROTO_FILES
                              provided will be loaded from these
                              FileDescriptorSets. If a FileDescriptor
                              appears multiple times, the first occurrence
                              will be used.
  -oFILE,                     Writes a FileDescriptorSet (a protocol buffer,
    --descriptor_set_out=FILE defined in descriptor.proto) containing all of
                              the input files to FILE.
  --include_imports           When using --descriptor_set_out, also include
                              all dependencies of the input files in the
                              set, so that the set is self-contained.
  --include_source_info       When using --descriptor_set_out, do not strip
                              SourceCodeInfo from the FileDescriptorProto.
                              This results in vastly larger descriptors that
                              include information about the original
                              location of each decl in the source file as
                              well as surrounding comments.
  --dependency_out=FILE       Write a dependency output file in the format
                              expected by make. This writes the transitive
                              set of input file paths to FILE
  --error_format=FORMAT       Set the format in which to print errors.
                              FORMAT may be 'gcc' (the default) or 'msvs'
                              (Microsoft Visual Studio format).
  --fatal_warnings            Make warnings be fatal (similar to -Werr in
                              gcc). This flag will make protoc return
                              with a non-zero exit code if any warnings
                              are generated.
  --print_free_field_numbers  Print the free field numbers of the messages
                              defined in the given proto files. Groups share
                              the same field number space with the parent
                              message. Extension ranges are counted as
                              occupied fields numbers.
  --plugin=EXECUTABLE         Specifies a plugin executable to use.
                              Normally, protoc searches the PATH for
                              plugins, but you may specify additional
                              executables not in the path using this flag.
                              Additionally, EXECUTABLE may be of the form
                              NAME=PATH, in which case the given plugin name
                              is mapped to the given executable even if
                              the executable's own name differs.
  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --kotlin_out=OUT_DIR        Generate Kotlin file.
  --objc_out=OUT_DIR          Generate Objective-C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --pyi_out=OUT_DIR           Generate python pyi stub.
  --python_out=OUT_DIR        Generate Python source file.
  @<filename>                 Read options and filenames from file. If a
                              this argument file is searched. Content of
                              the file will be expanded in the position of
                              @<filename> as in the argument list. Note
                              that shell expansion is not applied to the
                              content of the file (i.e., you cannot use
                              quotes, wildcards, escapes, commands, etc.).
                              Each line corresponds to a single argument,
                              even if it contains spaces.

已经成功完成proto工具的安装。

紧接着,再一次尝试——

提示无法确定生成go文件的路径

protoc --go_out=. test.proto
报错信息:
protoc-gen-go: unable to determine Go import path for "test.proto"

Please specify either:
        • a "go_package" option in the .proto source file, or
        • a "M" argument on the command line.

test.proto文件

如下:

syntax="proto3"; //版本号
package main;  //包名

enum ClassName{   //枚举
  class1=0;  //标号 必须从 0开始
  class2=1;
  class3=2;
}
message Student{ //消息,对应于Go的结构体
  string name=1; //1:标号,唯一 即可(相当于数据库中的Id,不一定要从1 ,2的顺序依次排列。)
  int32 age=2;  //必须指定整型的范围,如int32,int64
  string address=3;
  ClassName cn=4;
}
message Students{
  repeated Student person=1;  // repeated 修饰,相当于Go中切片
  string school=2;
}
  • protoc-gen-go v1.27.1
  • protoc v3.12.3

原因是protoc-gen-go版本过高,对源proto文件需要添加包名。

还有一种解决办法就是把protoc-gen-go版本退回到1.3.2及以下也可以解决。

最终!!根据报错信息在文件中第二行后添加:(指定生成go文件的路径)

option go_package="/main"; //解决报错:unable to determine Go import path

总算成功在该目录下生成了test.pb.go文件!!!

总结

到此这篇关于Go使用proto3的踩坑实战记录的文章就介绍到这了,更多相关Go使用proto3踩坑内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • go micro微服务proto开发安装及使用规则

    目录 一 Protobuf介绍 二 安装Protobuf 三 Protobuf语法 1.1 基本规范 1.2 字段规则 1.3 service如何定义 1.4 Message如何定义 四 proto代码编写 五 生成.go文件 六 最后 一 Protobuf介绍 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 个 .proto 文件. 他们用于 RP

  • Go gRPC服务proto数据验证进阶教程

    前言 上篇介绍了go-grpc-middleware的grpc_zap.grpc_auth和grpc_recovery使用,本篇将介绍grpc_validator,它可以对gRPC数据的输入和输出进行验证. 创建proto文件,添加验证规则 这里使用第三方插件go-proto-validators自动生成验证规则. go get github.com/mwitkow/go-proto-validators 1.新建simple.proto文件 syntax = "proto3"; pa

  • Go使用proto3的踩坑实战记录

    开发环境:windows10,golang1.18.2,goland2022.2 最近在写项目时,一些数据类的结构以protobuf文件给定.因此,需要将这些protobuf文件转换为golang代码. 首先,在下载解析protobuf的包的时候就碰到了第一个问题... go get -u github.com/golang/protobuf/protoc-gen-go 在我用上述命令后,终端提示该包已弃用 go: module github.com/golang/protobuf is dep

  • 使用Pyinstaller的最新踩坑实战记录

    前言 将py编译成可执行文件需要使用PyInstaller,之前给大家介绍了关于利用PyInstaller将python程序.py转为.exe的方法,在开始本文之前推荐大家可以先看下这篇文章,本文主要给大家介绍了Pyinstaller最新踩坑实战记录,现在网上关于pyinstaller的问题充斥着各种copy过来copy过去的答案,这大概就是各种无脑博客爬虫站最让人讨厌的地方. 而且这方面的问题,stackoverflow也是回答的千奇百怪. 强烈推荐官方文档 http://pythonhost

  • nginx反向代理踩坑实战记录(容器方式)

    目录 一.简述 1.1 什么是反向代理? 1.2 看图理解 1.3 错误总结 二.正确案例 2.1 启动nginx 2.3 配置nginx 2.4 重启所有服务 2.5 测试 三.云服务器上跑的nginx怎么代理本地项目 总结 一.简述 1.1 什么是反向代理? 这很重要,反向代理就是代理服务器代理真实服务器.客户端以为代理服务器就是真实服务器,所以就会把要请求的==资源(URL)==发给代理服务器. 代理服务器一般是由nginx来充当,代理功能由配置文件来完成. 1.2 看图理解 画的仓促,大

  • 一次mysql迁移的方案与踩坑实战记录

    目录 背景 方案一:老数据备份 方案二:分表 方案三:迁移至tidb 重点说下同步老数据遇到的坑 最终同步脚本方案 总结 背景 由于历史业务数据采用mysql来存储的,其中有一张操作记录表video_log,每当用户创建.更新或者审核人员审核的时候,对应的video_log就会加一条日志,这个log表只有insert,可想而知,1个video对应多条log,一天10w video,平均统计一个video对应5条log,那么一天50w的log, 一个月50 * 30 = 1500w条记录, 一年就

  • springboot整合log4j的踩坑实战记录

    目录 1.依赖添加 1.1.添加依赖 1.2.剔除依赖 2.配置日志 2.1.日志打印记录 2.2.指定配置文件 补充:log4j调优和注意事项 总结 1.依赖添加 1.1.添加依赖 需要引入 log4j 的依赖支持,推荐自己确定使用的版本. <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <ve

  • vue3搭配pinia的踩坑实战记录

    目录 前言 An Object could not be cloned? VUE 3的toRaw PINIA与VUE 3可以混合搭配? 同样的操作在VUE 3下的结果 最后的解决方式 总结 前言 最近接手了一个新项目,用的是VUE3+pinia的组合.由于之前没有用过这2个库,只能现学现做.幸运的是这两者的上手难度都不大,项目可以正常开发.但这其中也遇到了一些坑,今天就来讲其中我印象最深的一个. An Object could not be cloned? 不知道有多少开发者遇到过这个报错——A

  • Android录音功能的实现以及踩坑实战记录

    目录 前言 前提 : 代码实现流程 : 踩坑 1.Android Q: 2.RuntimeException:setAudioSource failed 3.MediaRecorder: stop failed 总结 前言 最近接到个需求,不使用第三方SDK的情况下实现IM通讯,文字聊天已经通过MQTT实现,而语音功能目前想到的较好解决方案就是进行录音文件的上传下载.可能还有更好解决方案,但我目前没想到,有建议的小伙伴劳烦指导下. 前提 : 1.权限申请: 清单文件中加上: <uses-perm

  • Java切割字符串的踩坑实战记录

    目录 坑出现的环境 问题的解决 补充:java分割字符串常见语法 一.java.lang.String.split() 二.java.util.StringTokenizer() 总结 坑出现的环境 一般情况下切割字符串会使用split或者StringTokenizer,如下代码 String s = ",,o,,"; String[] split = s.split(","); 期望得到数组["","","o&qu

  • C++踩坑实战之构造和析构函数

    目录 前言 构造函数 通过构造函数实现的类型转换 派生类的构造函数 析构函数 继承中的析构函数 应用 总结 前言 我是练习时长一年的 C++ 个人练习生,喜欢野指针.模板报错和未定义行为(undefined behavior).之前在写设计模式的『工厂模式』时,一脚踩到了构造.继承和 new 组合起来的坑,现在也有时间来整理一下了. 构造函数 众所周知:在创建对象时,防止有些成员没有被初始化导致不必要的错误,在创建对象的时候自动调用构造函数(无声明类型),完成成员的初始化.即: Class c

  • Python 3.x踩坑实战汇总

    目录 纪要 处处有坑 1. 文件读取 open 2. 正则表达式 \S 与 \\S 3. 正则表达式匹配方法 match 4. 帮助文档 pydoc 5. 字符串 encode base64 编码 6. Python 调用 C# 动态链接库 总结 纪要 本文用于记录学习 Python 过程中遇到的一些小问题,如果遇到的是比较大的问题会单独开页面分析学习 处处有坑 1. 文件读取 open # 我们打开文件使用 open 方法 xml = open("demo.xml") # 使用 op

随机推荐