脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Golang - Go使用proto3的踩坑实战记录

Go使用proto3的踩坑实战记录

2023-02-14 11:48多喝氧烷_ Golang

这篇文章主要给大家介绍了关于Go使用proto3的踩坑记录,文中通过实例代码介绍的非常详细,对大家学习或者会用Go语言具有一定的参考学习价值,需要的朋友可以参考下

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

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

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

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

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

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

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

故再次使用go install

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
$: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文件的路径

?
1
2
3
4
5
6
7
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文件

如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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文件的路径)

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

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

Go使用proto3的踩坑实战记录

总结

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

原文链接:https://blog.csdn.net/m0_61472414/article/details/127445617

延伸 · 阅读

精彩推荐
  • GolangGo 中 slice 的 In 功能实现探索

    Go 中 slice 的 In 功能实现探索

    这篇文章主要介绍了Go 中 slice 的 In 功能实现探索,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 ...

    波罗学 ·1572020-05-28
  • GolangGolang rabbitMQ生产者消费者实现示例

    Golang rabbitMQ生产者消费者实现示例

    这篇文章主要为大家介绍了Golang rabbitMQ生产者消费者实现的示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪...

    Jeff的技术栈5262022-09-22
  • GolangGolang Http 验证码示例实现

    Golang Http 验证码示例实现

    这篇文章主要介绍了Golang Http 验证码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小...

    xialeistudio4832020-09-06
  • Golanggo语言生成随机数和随机字符串的实现方法

    go语言生成随机数和随机字符串的实现方法

    随机数在很多时候都可以用到,尤其是登录时,本文就详细的介绍一下go语言生成随机数和随机字符串的实现方法,具有一定的参考价值,感兴趣的可以了...

    kuteng10332022-01-20
  • GolangGo语言入门之函数的定义与使用

    Go语言入门之函数的定义与使用

    函数是一段代码的片段,包含连续的执行语句,它可以将零个或多个输入参数映射到零个或多个参数输出。本文将通过示例和大家详细聊聊Go语言中函数的...

    宇宙之一粟9292022-11-03
  • GolangGo语言--切片(Slice)详解

    Go语言--切片(Slice)详解

    这篇文章主要介绍了Go语言--切片(Slice),Go 语言切片是对数组的抽象,下面文章小编将为大家详细介绍该内容,需要的朋友可以参考下,希望对你有所帮...

    归子莫10972021-11-19
  • GolangGo Excelize API源码阅读SetSheetViewOptions示例解析

    Go Excelize API源码阅读SetSheetViewOptions示例解析

    这篇文章主要为大家介绍了Go-Excelize API源码阅读SetSheetViewOptions示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加...

    丘山子11362022-08-17
  • Golang分析Go错误处理优化go recover机制缺陷

    分析Go错误处理优化go recover机制缺陷

    这篇文章主要为大家介绍了分析Go错误处理优化go recover机制缺陷示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职...

    煎鱼3812022-07-13