亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Go使用proto3的踩坑實戰(zhàn)記錄

 更新時間:2023年02月13日 11:33:11   作者:多喝氧烷_  
這篇文章主要給大家介紹了關于Go使用proto3的踩坑記錄,文中通過實例代碼介紹的非常詳細,對大家學習或者會用Go語言具有一定的參考學習價值,需要的朋友可以參考下

開發(fā)環(huán)境: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 

成功安裝上述可執(zhí)行文件。

再次嘗試解析.proto文件,報錯:

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

又是因為沒有將GOPATH\bin目錄添加至環(huán)境變量中。。添加后測試終端輸入:

$: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的踩坑實戰(zhàn)記錄的文章就介紹到這了,更多相關Go使用proto3踩坑內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 基于go-cqhttp與Flask搭建定制機器人項目實戰(zhàn)示例

    基于go-cqhttp與Flask搭建定制機器人項目實戰(zhàn)示例

    這篇文章主要為大家介紹了基于go-cqhttp與Flask搭建定制機器人項目實戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • 詳解Go channel管道的運行原理

    詳解Go channel管道的運行原理

    Go推薦通過通信來共享內存,而channel就實現了這一理念。那channel是怎么運行的呢?本文將帶你搞懂Go channel管道的運行原理,感興趣的同學可以參考一下
    2023-05-05
  • Golang設計模式之外觀模式的實現

    Golang設計模式之外觀模式的實現

    這篇文章主要介紹了Golang設計模式之外觀模式的實現,外觀模式是一種常用的設計模式之一,是一種結構型設計模式,它提供了一個簡單的接口來訪問復雜系統(tǒng)的各種功能,從而降低了系統(tǒng)的復雜度,需要詳細了解可以參考下文
    2023-05-05
  • Go語言開發(fā)k8s之ConfigMap操作解析

    Go語言開發(fā)k8s之ConfigMap操作解析

    這篇文章主要為大家介紹了Go語言開發(fā)k8s之ConfigMap操作示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Go語言學習otns示例分析

    Go語言學習otns示例分析

    這篇文章主要為大家介紹了Go語言學習otns示例分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • GO的鎖和原子操作的示例詳解

    GO的鎖和原子操作的示例詳解

    這篇文章主要為大家詳細介紹了Go語言中鎖和原子操作的相關資料,文中的示例代碼講解詳細,對我們學習Go語言有一定的幫助,需要的可以參考一下
    2023-02-02
  • 基于Golang實現Excel表格的導入導出功能

    基于Golang實現Excel表格的導入導出功能

    最近項目開發(fā)中有涉及到Excel的導入與導出功能,特別是導出表格時需要特定的格式,所以本文給大家介紹了基于Golang實現Excel表格的導入導出功能,文中通過代碼示例和圖文介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • Go Excelize API源碼解讀GetSheetViewOptions與SetPageLayout

    Go Excelize API源碼解讀GetSheetViewOptions與SetPageLayo

    這篇文章主要為大家介紹了Go Excelize API源碼解讀GetSheetViewOptions與SetPageLayout方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • GO語言并發(fā)之好用的sync包詳解

    GO語言并發(fā)之好用的sync包詳解

    標準庫中的sync包在我們的日常開發(fā)中用的頗為廣泛,那么大家對sync包的用法知道多少呢,這篇文章就大致講一下sync包和它的使用,感興趣的可以學習一下
    2022-12-12
  • Go與C語言的互操作實現

    Go與C語言的互操作實現

    在Go與C語言互操作方面,Go更是提供了強大的支持。尤其是在Go中使用C,你甚至可以直接在Go源文件中編寫C代碼,本文就詳細的介紹一下如何使用,感興趣的可以了解一下
    2021-12-12

最新評論