最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

開發(fā)環(huán)境:windows10,golang1.18.2,goland2022.2

最近在寫項目時,一些數(shù)據(jù)類的結(jié)構(gòu)以protobuf文件給定。因此,需要將這些protobuf文件轉(zhuǎn)換為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目錄下并沒有出現(xiàn)想要的protoc-gen-go.exe文件。

故再次使用go install

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

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

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

'protoc-gen-go' 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序或批處理文件。

又是因為沒有將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.

已經(jīng)成功完成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;  //標(biāo)號 必須從 0開始
  class2=1;
  class3=2;
}
message Student{ //消息,對應(yīng)于Go的結(jié)構(gòu)體
  string name=1; //1:標(biāo)號,唯一 即可(相當(dāng)于數(shù)據(jù)庫中的Id,不一定要從1 ,2的順序依次排列。)
  int32 age=2;  //必須指定整型的范圍,如int32,int64
  string address=3;
  ClassName cn=4;
}
message Students{
  repeated Student person=1;  // repeated 修飾,相當(dāng)于Go中切片
  string school=2;
}
  • protoc-gen-go v1.27.1
  • protoc v3.12.3

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

還有一種解決辦法就是把protoc-gen-go版本退回到1.3.2及以下也可以解決。

最終?。「鶕?jù)報錯信息在文件中第二行后添加:(指定生成go文件的路徑)

option go_package="/main"; //解決報錯:unable to determine Go import path

總算成功在該目錄下生成了test.pb.go文件?。?!

總結(jié)

到此這篇關(guān)于Go使用proto3的踩坑實戰(zhàn)記錄的文章就介紹到這了,更多相關(guān)Go使用proto3踩坑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • golang中配置?sql.DB獲得更好的性能

    golang中配置?sql.DB獲得更好的性能

    這篇文章主要介紹了golang中如何配置?sql.DB獲得更好的性能,在這篇文章中,我想準(zhǔn)確解釋這些設(shè)置的作用,并展示它們可能產(chǎn)生的(積極和消極)影響,需要的朋友可以參考下
    2023-10-10
  • Go語言中如何進(jìn)行包管理

    Go語言中如何進(jìn)行包管理

    在Go語言中,包(package)是函數(shù)和數(shù)據(jù)的集合,用于組織代碼,實現(xiàn)模塊化開發(fā),本文將結(jié)合實際案例,詳細(xì)講解Go語言包管理的用法,有需要的可以參考下
    2024-10-10
  • goland配置自動注釋的實現(xiàn)

    goland配置自動注釋的實現(xiàn)

    本文主要介紹了goland配置自動注釋的實現(xiàn),文中通過圖文示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • 一文詳解golang通過io包進(jìn)行文件讀寫

    一文詳解golang通過io包進(jìn)行文件讀寫

    這篇文章主要介紹了golang通過io包進(jìn)行文件讀寫文中有詳細(xì)的代碼示例。對學(xué)習(xí)或工資有很好的幫助,需要的小伙伴可以參考閱讀一下
    2023-04-04
  • 利用golang的字符串解決leetcode翻轉(zhuǎn)字符串里的單詞

    利用golang的字符串解決leetcode翻轉(zhuǎn)字符串里的單詞

    這篇文章主要介紹了利用golang的字符串解決leetcode翻轉(zhuǎn)字符串里的單詞,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • go語言實現(xiàn)同步操作項目示例

    go語言實現(xiàn)同步操作項目示例

    本文主要介紹了go語言實現(xiàn)同步操作項目示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 在Golang代碼中如何自動生成版本號的方法示例

    在Golang代碼中如何自動生成版本號的方法示例

    這篇文章主要給大家介紹了在Golang代碼中如何自動生成版本號的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-07-07
  • GO語言實現(xiàn)TCP服務(wù)器的示例代碼

    GO語言實現(xiàn)TCP服務(wù)器的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何通過GO語言實現(xiàn)TCP服務(wù)器,文中的示例代碼講解詳細(xì),對我們深入了解Go語言有一定的幫助,需要的可以參考一下
    2023-03-03
  • go語言實現(xiàn)兩個協(xié)程交替打印

    go語言實現(xiàn)兩個協(xié)程交替打印

    這篇文章主要介紹了go語言實現(xiàn)兩個協(xié)程交替打印,文章主要分享了兩種方法使用兩個channel和使用一個channel,內(nèi)容介紹詳細(xì)具有一定的參考價值,需要的小伙伴可以參考一下
    2022-03-03
  • Go語言實現(xiàn)百萬級WebSocket連接架構(gòu)設(shè)計及服務(wù)優(yōu)化

    Go語言實現(xiàn)百萬級WebSocket連接架構(gòu)設(shè)計及服務(wù)優(yōu)化

    本文將詳細(xì)介紹如何在Go中構(gòu)建一個能夠支持百萬級WebSocket連接的服務(wù),包括系統(tǒng)架構(gòu)設(shè)計、性能優(yōu)化策略以及具體的實現(xiàn)步驟和代碼示例
    2024-01-01

最新評論

韶关市| 盐城市| 湘潭市| 紫阳县| 吴堡县| 佛冈县| 广水市| 新乡市| 吉首市| 泗阳县| 万山特区| 宣恩县| 宿迁市| 安塞县| 南岸区| 津市市| 堆龙德庆县| 许昌市| 溧阳市| 上杭县| 沅陵县| 乌鲁木齐县| 剑阁县| 湟源县| 阿巴嘎旗| 景洪市| 雷山县| 惠来县| 石渠县| 防城港市| 左贡县| 雅江县| 柘城县| 卢湾区| 堆龙德庆县| 永昌县| 寿阳县| 鹤壁市| 吉林市| 南岸区| 和顺县|