Go使用proto3的踩坑实战记录
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《Go使用proto3的踩坑实战记录》带大家来了解一下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.</filename></filename>
已经成功完成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的踩坑实战记录》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于golang的内容请关注golang学习网公众号!

- 上一篇
- MySQL关闭SSL的简单方法

- 下一篇
- Mysql查询优化之IN子查询优化方法详解
-
- Golang · Go教程 | 1分钟前 |
- Go语言rand.ExpFloat64未定义原因及解决方法
- 205浏览 收藏
-
- Golang · Go教程 | 1分钟前 |
- Golang空指针处理技巧分享
- 382浏览 收藏
-
- Golang · Go教程 | 9分钟前 |
- Golangcontext上下文控制详解
- 238浏览 收藏
-
- Golang · Go教程 | 10分钟前 |
- Golang操作Redis缓存技巧详解
- 308浏览 收藏
-
- Golang · Go教程 | 12分钟前 |
- Golang大文件写入优化技巧
- 475浏览 收藏
-
- Golang · Go教程 | 34分钟前 | golang 文本文件读取
- Golang逐行读取文本方法详解
- 249浏览 收藏
-
- Golang · Go教程 | 36分钟前 |
- Golangswitch中fallthrough的作用是什么?
- 453浏览 收藏
-
- Golang · Go教程 | 37分钟前 |
- Golang接口断言:值与指针区别详解
- 496浏览 收藏
-
- Golang · Go教程 | 42分钟前 |
- Golang指针逃逸分析与堆栈分配详解
- 347浏览 收藏
-
- Golang · Go教程 | 43分钟前 |
- Termux配置Golang开发环境教程
- 209浏览 收藏
-
- Golang · Go教程 | 1小时前 |
- Go语言接收二进制数据实战指南
- 317浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 514次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 499次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- AI Mermaid流程图
- SEO AI Mermaid 流程图工具:基于 Mermaid 语法,AI 辅助,自然语言生成流程图,提升可视化创作效率,适用于开发者、产品经理、教育工作者。
- 526次使用
-
- 搜获客【笔记生成器】
- 搜获客笔记生成器,国内首个聚焦小红书医美垂类的AI文案工具。1500万爆款文案库,行业专属算法,助您高效创作合规、引流的医美笔记,提升运营效率,引爆小红书流量!
- 519次使用
-
- iTerms
- iTerms是一款专业的一站式法律AI工作台,提供AI合同审查、AI合同起草及AI法律问答服务。通过智能问答、深度思考与联网检索,助您高效检索法律法规与司法判例,告别传统模板,实现合同一键起草与在线编辑,大幅提升法律事务处理效率。
- 545次使用
-
- TokenPony
- TokenPony是讯盟科技旗下的AI大模型聚合API平台。通过统一接口接入DeepSeek、Kimi、Qwen等主流模型,支持1024K超长上下文,实现零配置、免部署、极速响应与高性价比的AI应用开发,助力专业用户轻松构建智能服务。
- 597次使用
-
- 迅捷AIPPT
- 迅捷AIPPT是一款高效AI智能PPT生成软件,一键智能生成精美演示文稿。内置海量专业模板、多样风格,支持自定义大纲,助您轻松制作高质量PPT,大幅节省时间。
- 513次使用
-
- Go语言日志内聚复用及gjson踩坑记录分享
- 2023-01-01 118浏览
-
- 浅谈golang fasthttp踩坑经验
- 2022-12-28 324浏览
-
- 解决Go gorm踩过的坑
- 2023-02-16 233浏览
-
- MySQL8.0升级的踩坑历险记
- 2023-01-07 146浏览