<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
開發環境: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' 不是內部或外部命令,也不是可執行的程式或批次檔。
又是因為沒有將GOPATHbin目錄新增至環境變數中。。新增後測試終端輸入:
$:protoc Usage: D:Applicationprotoc-21.7-win64binprotoc.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版本過高,對源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踩坑內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45