<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 語法 ### 格式的定義 `[ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"` 或 `type fieldName "=" fieldNumber` ### 版本定義 ``` syntax = "proto3"; // or syntax = "proto2"; ``` ### package 定義包名 ``` package foo.bar; ``` ### repeated 允許重復 .proto ``` message Person { string name = 1; int32 id = 2; repeated string email = 3; } ``` demo.go ``` p := &Person{ Name: "smallnest", Id: 9527, Email: []string{"test@example.com"}, } ``` ### option option可以用在proto的scope中,或者message、enum、service的定義中。 可以是Protobuf定義的option,或者自定義的option。 option的定義格式是`"option" optionName "=" constant ";"` 比如: `option java_package = "com.example.foo"; ` 一些Protobuf定義的option: ``` java_package java_multiple_files java_outer_classname optimize_for cc_enable_arenas objc_class_prefix deprecated go_package ``` ### **普通字段** > [官網類型對應其他語言](https://developers.google.com/protocol-buffers/docs/proto3#scalar) * 數字類型: double、float、int32、int64、uint32、uint64、sint32、sint64: 存儲長度可變的浮點數、整數、無符號整數和有符號整數 * 存儲固定大小的數字類型:fixed32、fixed64、sfixed32、sfixed64: 存儲空間固定 * 布爾類型: bool * 字符串: string * bytes: 字節數組 * messageType: 消息類型 * enumType:枚舉類型 ![UTOOLS1577338933390.png](http://yanxuan.nosdn.127.net/f1ec32b0cdda86ac5476f56552e0925e.png) ![UTOOLS1577338958932.png](http://yanxuan.nosdn.127.net/3c3a41d3081d73b48d77829a44a16720.png) ![](https://img.kancloud.cn/18/83/18833840da725c74b38f8a7925a02b2f_1342x1248.png) ### Oneof 如果你有一組字段,同時最多允許這一組中的一個字段出現,就可以使用`Oneof`定義這一組字段,這有點Union的意思,但是Oneof允許你設置零各值。 因為proto3沒有辦法區分正常的值是否是設置了還是取得缺省值(比如int64類型字段,如果它的值是0,你無法判斷數據是否包含這個字段,因為0幾可能是數據中設置的值,也可能是這個字段的零值),所以你可以通過Oneof取得這個功能,因為Oneof有判斷字段是否設置的功能。 ``` syntax = "proto3"; package abc; message OneofMessage { oneof test_oneof { string name = 4; int64 value = 9; } } ``` >`oneof`字段不能同時使用`repeated`。 ### map類型 `map<int64,string> values = 1; ` ### Reserved 也就是忽略某些字段,可以通過字段編號范圍或者字段名稱指定保留的字段 ``` syntax = "proto3"; package abc; message AllNormalypes { reserved 2, 4 to 6; reserved "field14", "field11"; double field1 = 1; // float field2 = 2; int32 field3 = 3; // int64 field4 = 4; // uint32 field5 = 5; // uint64 field6 = 6; sint32 field7 = 7; sint64 field8 = 8; fixed32 field9 = 9; fixed64 field10 = 10; // sfixed32 field11 = 11; sfixed64 field12 = 12; bool field13 = 13; // string field14 = 14; bytes field15 = 15; } ``` > 聲明保留的字段你就不要再定義了,需注釋,否則編譯的時候會出錯。 ### 枚舉類型 避免在同一個package定義重名的枚舉字段 ``` enum EnumAllowingAlias { option allow_alias = true; UNKNOWN = 0; STARTED = 1; RUNNING = 1; } enum EnumNotAllowingAlias { UNKNOWN2 = 0; STARTED2 = 1; // RUNNING = 1; } ``` > 設置 `allow_alias` 允許重復字段編號 如`STARTED`和`RUNNING ` > **第一個枚舉值必須是0**,而且必須定義 枚舉類型定義到message中 ``` message SearchRequest { string query = 1; int32 page_number = 2; int32 result_per_page = 3; enum Corpus { UNIVERSAL = 0; WEB = 1; IMAGES = 2; LOCAL = 3; NEWS = 4; PRODUCTS = 5; VIDEO = 6; } Corpus corpus = 4; } ``` ### 使用其它類型 在SearchResponse 中調用 Result 類型 ``` message SearchResponse { repeated Result results = 1; } message Result { string url = 1; string title = 2; repeated string snippets = 3; } ``` ### 嵌套類型 ``` message SearchResponse { message Result { string url = 1; string title = 2; repeated string snippets = 3; } repeated Result results = 1; } ``` ### Any Any字段允許你處理嵌套數據,并不需要它的proto定義。一個Any以bytes呈現序列化的消息,并且包含一個URL作為這個類型的唯一標識和元數據 ``` import "google/protobuf/any.proto"; message ErrorStatus { string message = 1; repeated google.protobuf.Any details = 2; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看