{"id":9298,"date":"2024-04-28T14:42:08","date_gmt":"2024-04-28T06:42:08","guid":{"rendered":"\/?p=9298"},"modified":"2024-04-28T14:42:08","modified_gmt":"2024-04-28T06:42:08","slug":"protobuf3-%e8%af%ad%e6%b3%95%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"\/?p=9298","title":{"rendered":"Protobuf3 \u8bed\u6cd5\u6307\u5357"},"content":{"rendered":"<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20240428144009424.png\" alt=\"image-20240428144009424\" \/><\/p>\n<p>\u4ee5\u524d\u6211\u7ffb\u8bd1\u4e86 <a href=\"http:\/\/colobu.com\/2015\/01\/07\/Protobuf-language-guide\/\">Protobuf2 \u8bed\u6cd5\u6307\u5357<\/a>\uff0c\u73b0\u5728 <a href=\"http:\/\/blog.csdn.net\/u011518120\">\u5343\u5ff5\u98de\u7fbd<\/a>\u628aprotobuf3\u7684\u8bed\u6cd5\u6307\u5357\u4e5f\u7ffb\u8bd1\u4e86\uff0c\u6211\u4e5f\u8f6c\u8f7d\u4e00\u4e0b\uff0c\u8bfb\u8005\u53ef\u4ee5\u6709\u4e2a\u53c2\u8003\u3002 \u8bd1\u6587\u5730\u5740\u662f: <a href=\"http:\/\/blog.csdn.net\/u011518120\/article\/details\/54604615\">Protobuf3\u8bed\u8a00\u6307\u5357<\/a>\u3002<\/p>\n<blockquote>\n<p>\u82f1\u6587\u539f\u6587\uff1a<br \/>\n<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/proto3?hl=zh-cn#generating\">Language Guide (proto3)<\/a><br \/>\n\u4e2d\u6587\u51fa\u5904\uff1a<br \/>\n<a href=\"http:\/\/www.open-open.com\/home\/space.php?uid=37924&amp;do=blog&amp;id=5873\">Protobuf\u8bed\u8a00\u6307\u5357<\/a><br \/>\n<a href=\"http:\/\/colobu.com\/2015\/01\/07\/Protobuf-language-guide\/\">[\u8bd1]Protobuf \u8bed\u6cd5\u6307\u5357<\/a><br \/>\n\u4e2d\u6587\u51fa\u5904\u662fproto2\u7684\u8bd1\u6587\uff0cproto3\u7684\u82f1\u6587\u51fa\u73b0\u540e\u5728\u539f\u6765\u57fa\u7840\u4e0a\u589e\u6539\u4e86\uff0c\u6c34\u5e73\u6709\u9650\uff0c\u8fd8\u8bf7\u6307\u6b63<\/p>\n<\/blockquote>\n<p>\u8fd9\u4e2a\u6307\u5357\u63cf\u8ff0\u4e86\u5982\u4f55\u4f7f\u7528Protocol buffer \u8bed\u8a00\u53bb\u63cf\u8ff0\u4f60\u7684protocol buffer \u6570\u636e\uff0c \u5305\u62ec .proto\u6587\u4ef6\u7b26\u53f7\u548c\u5982\u4f55\u4ece.proto\u6587\u4ef6\u751f\u6210\u7c7b\u3002\u5305\u542b\u4e86proto2\u7248\u672c\u7684protocol buffer\u8bed\u8a00\uff1a\u5bf9\u4e8e\u8001\u7248\u672c\u7684proto3 \u7b26\u53f7\uff0c\u8bf7\u89c1<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/proto?hl=zh-cn\">Proto2 Language Guide<\/a>\uff08\u4ee5\u53ca<a href=\"http:\/\/colobu.com\/2015\/01\/07\/Protobuf-language-guide\/\">\u4e2d\u6587\u8bd1\u672c<\/a>\uff0c\u6284\u4e86\u5f88\u591a\u8fd9\u91cc\u7684\u611f\u8c22\u4e0b\u8001\u7248\u672c\u7684\u7ffb\u8bd1\u8005\uff09<\/p>\n<p>\u672c\u6587\u662f\u4e00\u4e2a\u53c2\u8003\u6307\u5357\u2014\u2014\u5982\u679c\u8981\u67e5\u770b\u5982\u4f55\u4f7f\u7528\u672c\u6587\u4e2d\u63cf\u8ff0\u7684\u591a\u4e2a\u7279\u6027\u7684\u5faa\u5e8f\u6e10\u8fdb\u7684\u4f8b\u5b50\uff0c\u8bf7\u5728\u6559\u7a0b\u4e2d\u67e5\u627e\u9700\u8981\u7684\u8bed\u8a00\u7684\u6559\u7a0b\u3002<\/p>\n<h2>\u5b9a\u4e49\u4e00\u4e2a\u6d88\u606f\u7c7b\u578b<\/h2>\n<p>\u5148\u6765\u770b\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u5047\u8bbe\u4f60\u60f3\u5b9a\u4e49\u4e00\u4e2a\u201c\u641c\u7d22\u8bf7\u6c42\u201d\u7684\u6d88\u606f\u683c\u5f0f\uff0c\u6bcf\u4e00\u4e2a\u8bf7\u6c42\u542b\u6709\u4e00\u4e2a\u67e5\u8be2\u5b57\u7b26\u4e32\u3001\u4f60\u611f\u5174\u8da3\u7684\u67e5\u8be2\u7ed3\u679c\u6240\u5728\u7684\u9875\u6570\uff0c\u4ee5\u53ca\u6bcf\u4e00\u9875\u591a\u5c11\u6761\u67e5\u8be2\u7ed3\u679c\u3002\u53ef\u4ee5\u91c7\u7528\u5982\u4e0b\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u6d88\u606f\u7c7b\u578b\u7684.proto\u6587\u4ef6\u4e86\uff1a<\/p>\n<pre><code>syntax = &quot;proto3&quot;;\n\nmessage SearchRequest {\n  string query = 1;\n  int32 page_number = 2;\n  int32 result_per_page = 3;\n}<\/code><\/pre>\n<ul>\n<li>\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6307\u5b9a\u4e86\u4f60\u6b63\u5728\u4f7f\u7528proto3\u8bed\u6cd5\uff1a\u5982\u679c\u4f60\u6ca1\u6709\u6307\u5b9a\u8fd9\u4e2a\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f7f\u7528proto2\u3002\u8fd9\u4e2a\u6307\u5b9a\u8bed\u6cd5\u884c\u5fc5\u987b\u662f\u6587\u4ef6\u7684\u975e\u7a7a\u975e\u6ce8\u91ca\u7684\u7b2c\u4e00\u4e2a\u884c\u3002<\/li>\n<li>SearchRequest\u6d88\u606f\u683c\u5f0f\u67093\u4e2a\u5b57\u6bb5\uff0c\u5728\u6d88\u606f\u4e2d\u627f\u8f7d\u7684\u6570\u636e\u5206\u522b\u5bf9\u5e94\u4e8e\u6bcf\u4e00\u4e2a\u5b57\u6bb5\u3002\u5176\u4e2d\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u6709\u4e00\u4e2a\u540d\u5b57\u548c\u4e00\u79cd\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<h3>\u6307\u5b9a\u5b57\u6bb5\u7c7b\u578b<\/h3>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u5b57\u6bb5\u90fd\u662f\u6807\u91cf\u7c7b\u578b\uff1a\u4e24\u4e2a\u6574\u578b\uff08page_number\u548cresult_per_page\uff09\uff0c\u4e00\u4e2astring\u7c7b\u578b\uff08query\uff09\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4e3a\u5b57\u6bb5\u6307\u5b9a\u5176\u4ed6\u7684\u5408\u6210\u7c7b\u578b\uff0c\u5305\u62ec\u679a\u4e3e\uff08enumerations\uff09\u6216\u5176\u4ed6\u6d88\u606f\u7c7b\u578b\u3002<\/p>\n<h3>\u5206\u914d\u6807\u8bc6\u53f7<\/h3>\n<p>\u6b63\u5982\u4f60\u6240\u89c1\uff0c\u5728\u6d88\u606f\u5b9a\u4e49\u4e2d\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u6709\u552f\u4e00\u7684\u4e00\u4e2a\u6570\u5b57\u6807\u8bc6\u7b26\u3002\u8fd9\u4e9b\u6807\u8bc6\u7b26\u662f\u7528\u6765\u5728\u6d88\u606f\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u4e2d\u8bc6\u522b\u5404\u4e2a\u5b57\u6bb5\u7684\uff0c\u4e00\u65e6\u5f00\u59cb\u4f7f\u7528\u5c31\u4e0d\u80fd\u591f\u518d\u6539\u53d8\u3002\u6ce8\uff1a[1,15]\u4e4b\u5185\u7684\u6807\u8bc6\u53f7\u5728\u7f16\u7801\u7684\u65f6\u5019\u4f1a\u5360\u7528\u4e00\u4e2a\u5b57\u8282\u3002[16,2047]\u4e4b\u5185\u7684\u6807\u8bc6\u53f7\u5219\u5360\u75282\u4e2a\u5b57\u8282\u3002\u6240\u4ee5\u5e94\u8be5\u4e3a\u90a3\u4e9b\u9891\u7e41\u51fa\u73b0\u7684\u6d88\u606f\u5143\u7d20\u4fdd\u7559 [1,15]\u4e4b\u5185\u7684\u6807\u8bc6\u53f7\u3002\u5207\u8bb0\uff1a\u8981\u4e3a\u5c06\u6765\u6709\u53ef\u80fd\u6dfb\u52a0\u7684\u3001\u9891\u7e41\u51fa\u73b0\u7684\u6807\u8bc6\u53f7\u9884\u7559\u4e00\u4e9b\u6807\u8bc6\u53f7\u3002<\/p>\n<p>\u6700\u5c0f\u7684\u6807\u8bc6\u53f7\u53ef\u4ee5\u4ece1\u5f00\u59cb\uff0c\u6700\u5927\u52302^29 - 1, or 536,870,911\u3002\u4e0d\u53ef\u4ee5\u4f7f\u7528\u5176\u4e2d\u7684[19000\uff0d19999]\uff08 (\u4eceFieldDescriptor::kFirstReservedNumber \u5230 FieldDescriptor::kLastReservedNumber)\uff09\u7684\u6807\u8bc6\u53f7\uff0c Protobuf\u534f\u8bae\u5b9e\u73b0\u4e2d\u5bf9\u8fd9\u4e9b\u8fdb\u884c\u4e86\u9884\u7559\u3002\u5982\u679c\u975e\u8981\u5728.proto\u6587\u4ef6\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u9884\u7559\u6807\u8bc6\u53f7\uff0c\u7f16\u8bd1\u65f6\u5c31\u4f1a\u62a5\u8b66\u3002\u540c\u6837\u4f60\u4e5f\u4e0d\u80fd\u4f7f\u7528\u65e9\u671f\u4fdd\u7559\u7684\u6807\u8bc6\u53f7\u3002<\/p>\n<h3>\u6307\u5b9a\u5b57\u6bb5\u89c4\u5219<\/h3>\n<p>\u6240\u6307\u5b9a\u7684\u6d88\u606f\u5b57\u6bb5\u4fee\u9970\u7b26\u5fc5\u987b\u662f\u5982\u4e0b\u4e4b\u4e00\uff1a<\/p>\n<ul>\n<li>singular\uff1a\u4e00\u4e2a\u683c\u5f0f\u826f\u597d\u7684\u6d88\u606f\u5e94\u8be5\u67090\u4e2a\u6216\u80051\u4e2a\u8fd9\u79cd\u5b57\u6bb5\uff08\u4f46\u662f\u4e0d\u80fd\u8d85\u8fc71\u4e2a\uff09\u3002<\/li>\n<li>repeated\uff1a\u5728\u4e00\u4e2a\u683c\u5f0f\u826f\u597d\u7684\u6d88\u606f\u4e2d\uff0c\u8fd9\u79cd\u5b57\u6bb5\u53ef\u4ee5\u91cd\u590d\u4efb\u610f\u591a\u6b21\uff08\u5305\u62ec0\u6b21\uff09\u3002\u91cd\u590d\u7684\u503c\u7684\u987a\u5e8f\u4f1a\u88ab\u4fdd\u7559\u3002<\/li>\n<\/ul>\n<p>\u5728proto3\u4e2d\uff0crepeated\u7684\u6807\u91cf\u57df\u9ed8\u8ba4\u60c5\u51b5\u867e\u4f7f\u7528packed\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u4e86\u89e3\u66f4\u591a\u7684pakced\u5c5e\u6027\u5728<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding?hl=zh-cn#packed\">Protocol Buffer \u7f16\u7801<\/a><\/p>\n<h3>\u6dfb\u52a0\u66f4\u591a\u6d88\u606f\u7c7b\u578b<\/h3>\n<p>\u5728\u4e00\u4e2a.proto\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u6d88\u606f\u7c7b\u578b\u3002\u5728\u5b9a\u4e49\u591a\u4e2a\u76f8\u5173\u7684\u6d88\u606f\u7684\u65f6\u5019\uff0c\u8fd9\u4e00\u70b9\u7279\u522b\u6709\u7528\u2014\u2014\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u5b9a\u4e49\u4e0eSearchResponse\u6d88\u606f\u7c7b\u578b\u5bf9\u5e94\u7684\u56de\u590d\u6d88\u606f\u683c\u5f0f\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b83\u6dfb\u52a0\u5230\u76f8\u540c\u7684.proto\u6587\u4ef6\u4e2d\uff0c\u5982\uff1a<\/p>\n<pre><code>message SearchRequest {\n  string query = 1;\n  int32 page_number = 2;\n  int32 result_per_page = 3;\n}\n\nmessage SearchResponse {\n ...\n}<\/code><\/pre>\n<h3>\u6dfb\u52a0\u6ce8\u91ca<\/h3>\n<p>\u5411.proto\u6587\u4ef6\u6dfb\u52a0\u6ce8\u91ca\uff0c\u53ef\u4ee5\u4f7f\u7528C\/C++\/Java\u98ce\u683c\u7684\u53cc\u659c\u6760\uff08\/\/\uff09 \u8bed\u6cd5\u683c\u5f0f\uff0c\u5982\uff1a<\/p>\n<pre><code>message SearchRequest {\n  string query = 1;\n  int32 page_number = 2;  \/\/ Which page number do we want?\n  int32 result_per_page = 3;  \/\/ Number of results to return per page.\n}<\/code><\/pre>\n<h3>\u4fdd\u7559\u6807\u8bc6\u7b26\uff08Reserved\uff09<\/h3>\n<p>\u5982\u679c\u4f60\u901a\u8fc7\u5220\u9664\u6216\u8005\u6ce8\u91ca\u6240\u6709\u57df\uff0c\u4ee5\u540e\u7684\u7528\u6237\u5728\u66f4\u65b0\u8fd9\u4e2a\u7c7b\u578b\u7684\u65f6\u5019\u53ef\u80fd\u91cd\u7528\u8fd9\u4e9b\u6807\u8bc6\u53f7\u3002\u5982\u679c\u4f60\u4f7f\u7528\u65e7\u7248\u672c\u52a0\u8f7d\u76f8\u540c\u7684.proto\u6587\u4ef6\u4f1a\u5bfc\u81f4\u4e25\u91cd\u7684\u95ee\u9898\uff0c\u5305\u62ec\u6570\u636e\u635f\u574f\u3001\u9690\u79c1\u9519\u8bef\u7b49\u7b49\u3002\u73b0\u5728\u6709\u4e00\u79cd\u786e\u4fdd\u4e0d\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u7684\u65b9\u6cd5\u5c31\u662f\u4e3a\u5b57\u6bb5tag\uff08reserved name\u53ef\u80fd\u4f1aJSON\u5e8f\u5217\u5316\u7684\u95ee\u9898\uff09\u6307\u5b9a<code>reserved<\/code>\u6807\u8bc6\u7b26\uff0cprotocol buffer\u7684\u7f16\u8bd1\u5668\u4f1a\u8b66\u544a\u672a\u6765\u5c1d\u8bd5\u4f7f\u7528\u8fd9\u4e9b\u57df\u6807\u8bc6\u7b26\u7684\u7528\u6237\u3002<\/p>\n<pre><code>message Foo {\n  reserved 2, 15, 9 to 11;\n  reserved &quot;foo&quot;, &quot;bar&quot;;\n}<\/code><\/pre>\n<p>\u6ce8\uff1a\u4e0d\u8981\u5728\u540c\u4e00\u884creserved\u58f0\u660e\u4e2d\u540c\u65f6\u58f0\u660e\u57df\u540d\u5b57\u548ctag number\u3002<\/p>\n<h3>\u4ece.proto\u6587\u4ef6\u751f\u6210\u4e86\u4ec0\u4e48\uff1f<\/h3>\n<p>\u5f53\u7528protocol buffer\u7f16\u8bd1\u5668\u6765\u8fd0\u884c.proto\u6587\u4ef6\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u751f\u6210\u6240\u9009\u62e9\u8bed\u8a00\u7684\u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u64cd\u4f5c\u5728.proto\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u6d88\u606f\u7c7b\u578b\uff0c\u5305\u62ec\u83b7\u53d6\u3001\u8bbe\u7f6e\u5b57\u6bb5\u503c\uff0c\u5c06\u6d88\u606f\u5e8f\u5217\u5316\u5230\u4e00\u4e2a\u8f93\u51fa\u6d41\u4e2d\uff0c\u4ee5\u53ca\u4ece\u4e00\u4e2a\u8f93\u5165\u6d41\u4e2d\u89e3\u6790\u6d88\u606f\u3002<\/p>\n<ul>\n<li>\u5bf9C++\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u6bcf\u4e2a.proto\u6587\u4ef6\u751f\u6210\u4e00\u4e2a.h\u6587\u4ef6\u548c\u4e00\u4e2a.cc\u6587\u4ef6\uff0c.proto\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6d88\u606f\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u7c7b\u3002<\/li>\n<li>\u5bf9Java\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u4e3a\u6bcf\u4e00\u4e2a\u6d88\u606f\u7c7b\u578b\u751f\u6210\u4e86\u4e00\u4e2a.java\u6587\u4ef6\uff0c\u4ee5\u53ca\u4e00\u4e2a\u7279\u6b8a\u7684Builder\u7c7b\uff08\u8be5\u7c7b\u662f\u7528\u6765\u521b\u5efa\u6d88\u606f\u7c7b\u63a5\u53e3\u7684\uff09\u3002<\/li>\n<li>\u5bf9Python\u6765\u8bf4\uff0c\u6709\u70b9\u4e0d\u592a\u4e00\u6837\u2014\u2014Python\u7f16\u8bd1\u5668\u4e3a.proto\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e2a\u6d88\u606f\u7c7b\u578b\u751f\u6210\u4e00\u4e2a\u542b\u6709\u9759\u6001\u63cf\u8ff0\u7b26\u7684\u6a21\u5757\uff0c\uff0c\u8be5\u6a21\u5757\u4e0e\u4e00\u4e2a\u5143\u7c7b\uff08metaclass\uff09\u5728\u8fd0\u884c\u65f6\uff08runtime\uff09\u88ab\u7528\u6765\u521b\u5efa\u6240\u9700\u7684Python\u6570\u636e\u8bbf\u95ee\u7c7b\u3002<\/li>\n<li>\u5bf9go\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f4d\u6bcf\u4e2a\u6d88\u606f\u7c7b\u578b\u751f\u6210\u4e86\u4e00\u4e2a.pd.go\u6587\u4ef6\u3002<\/li>\n<li>\u5bf9\u4e8eRuby\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u6bcf\u4e2a\u6d88\u606f\u7c7b\u578b\u751f\u6210\u4e86\u4e00\u4e2a.rb\u6587\u4ef6\u3002<\/li>\n<li>javaNano\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u8f93\u51fa\u7c7b\u4f3c\u57dfjava\u4f46\u662f\u6ca1\u6709Builder\u7c7b<\/li>\n<li>\u5bf9\u4e8eObjective-C\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u6bcf\u4e2a\u6d88\u606f\u7c7b\u578b\u751f\u6210\u4e86\u4e00\u4e2apbobjc.h\u6587\u4ef6\u548cpbobjcm\u6587\u4ef6\uff0c.proto\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6d88\u606f\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u7c7b\u3002<\/li>\n<li>\u5bf9\u4e8eC#\u6765\u8bf4\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u6bcf\u4e2a\u6d88\u606f\u7c7b\u578b\u751f\u6210\u4e86\u4e00\u4e2a.cs\u6587\u4ef6\uff0c.proto\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6d88\u606f\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u7c7b\u3002<br \/>\n\u4f60\u53ef\u4ee5\u4ece\u5982\u4e0b\u7684\u6587\u6863\u94fe\u63a5\u4e2d\u83b7\u53d6\u6bcf\u79cd\u8bed\u8a00\u66f4\u591aAPI(proto3\u7248\u672c\u7684\u5185\u5bb9\u5f88\u5feb\u5c31\u516c\u5e03)\u3002<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/overview\">API Reference<\/a><\/li>\n<\/ul>\n<h2>\u6807\u91cf\u6570\u503c\u7c7b\u578b<\/h2>\n<p>\u4e00\u4e2a\u6807\u91cf\u6d88\u606f\u5b57\u6bb5\u53ef\u4ee5\u542b\u6709\u4e00\u4e2a\u5982\u4e0b\u7684\u7c7b\u578b\u2014\u2014\u8be5\u8868\u683c\u5c55\u793a\u4e86\u5b9a\u4e49\u4e8e.proto\u6587\u4ef6\u4e2d\u7684\u7c7b\u578b\uff0c\u4ee5\u53ca\u4e0e\u4e4b\u5bf9\u5e94\u7684\u3001\u5728\u81ea\u52a8\u751f\u6210\u7684\u8bbf\u95ee\u7c7b\u4e2d\u5b9a\u4e49\u7684\u7c7b\u578b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">.proto Type<\/th>\n<th style=\"text-align: left;\">Notes<\/th>\n<th style=\"text-align: left;\">C++ Type<\/th>\n<th style=\"text-align: left;\">Java Type<\/th>\n<th style=\"text-align: left;\">Python Type[2]<\/th>\n<th style=\"text-align: left;\">Go Type<\/th>\n<th style=\"text-align: left;\">Ruby Type<\/th>\n<th style=\"text-align: left;\">C# Type<\/th>\n<th style=\"text-align: left;\">PHP Type<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">double<\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">double<\/td>\n<td style=\"text-align: left;\">double<\/td>\n<td style=\"text-align: left;\">float<\/td>\n<td style=\"text-align: left;\">float64<\/td>\n<td style=\"text-align: left;\">Float<\/td>\n<td style=\"text-align: left;\">double<\/td>\n<td style=\"text-align: left;\">float<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">float<\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">float<\/td>\n<td style=\"text-align: left;\">float<\/td>\n<td style=\"text-align: left;\">float<\/td>\n<td style=\"text-align: left;\">float32<\/td>\n<td style=\"text-align: left;\">Float<\/td>\n<td style=\"text-align: left;\">float<\/td>\n<td style=\"text-align: left;\">float<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">int32<\/td>\n<td style=\"text-align: left;\">\u4f7f\u7528\u53d8\u957f\u7f16\u7801\uff0c\u5bf9\u4e8e\u8d1f\u503c\u7684\u6548\u7387\u5f88\u4f4e\uff0c\u5982\u679c\u4f60\u7684\u57df\u6709\u53ef\u80fd\u6709\u8d1f\u503c\uff0c\u8bf7\u4f7f\u7528sint64\u66ff\u4ee3<\/td>\n<td style=\"text-align: left;\">int32<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">int32<\/td>\n<td style=\"text-align: left;\">Fixnum \u6216\u8005 Bignum\uff08\u6839\u636e\u9700\u8981\uff09<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">integer<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">uint32<\/td>\n<td style=\"text-align: left;\">\u4f7f\u7528\u53d8\u957f\u7f16\u7801<\/td>\n<td style=\"text-align: left;\">uint32<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">int\/long<\/td>\n<td style=\"text-align: left;\">uint32<\/td>\n<td style=\"text-align: left;\">Fixnum \u6216\u8005 Bignum\uff08\u6839\u636e\u9700\u8981\uff09<\/td>\n<td style=\"text-align: left;\">uint<\/td>\n<td style=\"text-align: left;\">integer<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">uint64<\/td>\n<td style=\"text-align: left;\">\u4f7f\u7528\u53d8\u957f\u7f16\u7801<\/td>\n<td style=\"text-align: left;\">uint64<\/td>\n<td style=\"text-align: left;\">long<\/td>\n<td style=\"text-align: left;\">int\/long<\/td>\n<td style=\"text-align: left;\">uint64<\/td>\n<td style=\"text-align: left;\">Bignum<\/td>\n<td style=\"text-align: left;\">ulong<\/td>\n<td style=\"text-align: left;\">integer\/string<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">sint32<\/td>\n<td style=\"text-align: left;\">\u4f7f\u7528\u53d8\u957f\u7f16\u7801\uff0c\u8fd9\u4e9b\u7f16\u7801\u5728\u8d1f\u503c\u65f6\u6bd4int32\u9ad8\u6548\u7684\u591a<\/td>\n<td style=\"text-align: left;\">int32<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">int32<\/td>\n<td style=\"text-align: left;\">Fixnum \u6216\u8005 Bignum\uff08\u6839\u636e\u9700\u8981\uff09<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">integer<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">sint64<\/td>\n<td style=\"text-align: left;\">\u4f7f\u7528\u53d8\u957f\u7f16\u7801\uff0c\u6709\u7b26\u53f7\u7684\u6574\u578b\u503c\u3002\u7f16\u7801\u65f6\u6bd4\u901a\u5e38\u7684int64\u9ad8\u6548\u3002<\/td>\n<td style=\"text-align: left;\">int64<\/td>\n<td style=\"text-align: left;\">long<\/td>\n<td style=\"text-align: left;\">int\/long<\/td>\n<td style=\"text-align: left;\">int64<\/td>\n<td style=\"text-align: left;\">Bignum<\/td>\n<td style=\"text-align: left;\">long<\/td>\n<td style=\"text-align: left;\">integer\/string<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">fixed32<\/td>\n<td style=\"text-align: left;\">\u603b\u662f4\u4e2a\u5b57\u8282\uff0c\u5982\u679c\u6570\u503c\u603b\u662f\u6bd4\u603b\u662f\u6bd4228\u5927\u7684\u8bdd\uff0c\u8fd9\u4e2a\u7c7b\u578b\u4f1a\u6bd4uint32\u9ad8\u6548\u3002<\/td>\n<td style=\"text-align: left;\">uint32<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">uint32<\/td>\n<td style=\"text-align: left;\">Fixnum \u6216\u8005 Bignum\uff08\u6839\u636e\u9700\u8981\uff09<\/td>\n<td style=\"text-align: left;\">uint<\/td>\n<td style=\"text-align: left;\">integer<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">fixed64<\/td>\n<td style=\"text-align: left;\">\u603b\u662f8\u4e2a\u5b57\u8282\uff0c\u5982\u679c\u6570\u503c\u603b\u662f\u6bd4\u603b\u662f\u6bd4256\u5927\u7684\u8bdd\uff0c\u8fd9\u4e2a\u7c7b\u578b\u4f1a\u6bd4uint64\u9ad8\u6548\u3002<\/td>\n<td style=\"text-align: left;\">uint64<\/td>\n<td style=\"text-align: left;\">long<\/td>\n<td style=\"text-align: left;\">int\/long<\/td>\n<td style=\"text-align: left;\">uint64<\/td>\n<td style=\"text-align: left;\">Bignum<\/td>\n<td style=\"text-align: left;\">ulong<\/td>\n<td style=\"text-align: left;\">integer\/string<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">sfixed32<\/td>\n<td style=\"text-align: left;\">\u603b\u662f4\u4e2a\u5b57\u8282<\/td>\n<td style=\"text-align: left;\">int32<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">int32<\/td>\n<td style=\"text-align: left;\">Fixnum \u6216\u8005 Bignum\uff08\u6839\u636e\u9700\u8981\uff09<\/td>\n<td style=\"text-align: left;\">int<\/td>\n<td style=\"text-align: left;\">integer<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">sfixed64<\/td>\n<td style=\"text-align: left;\">\u603b\u662f8\u4e2a\u5b57\u8282<\/td>\n<td style=\"text-align: left;\">int64<\/td>\n<td style=\"text-align: left;\">long<\/td>\n<td style=\"text-align: left;\">int\/long<\/td>\n<td style=\"text-align: left;\">int64<\/td>\n<td style=\"text-align: left;\">Bignum<\/td>\n<td style=\"text-align: left;\">long<\/td>\n<td style=\"text-align: left;\">integer\/string<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">bool<\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">bool<\/td>\n<td style=\"text-align: left;\">boolean<\/td>\n<td style=\"text-align: left;\">bool<\/td>\n<td style=\"text-align: left;\">bool<\/td>\n<td style=\"text-align: left;\">TrueClass\/FalseClass<\/td>\n<td style=\"text-align: left;\">bool<\/td>\n<td style=\"text-align: left;\">boolean<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">\u4e00\u4e2a\u5b57\u7b26\u4e32\u5fc5\u987b\u662fUTF-8\u7f16\u7801\u6216\u80057-bit ASCII\u7f16\u7801\u7684\u6587\u672c\u3002<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">String<\/td>\n<td style=\"text-align: left;\">str\/unicode<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">String (UTF-8)<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">bytes<\/td>\n<td style=\"text-align: left;\">\u53ef\u80fd\u5305\u542b\u4efb\u610f\u987a\u5e8f\u7684\u5b57\u8282\u6570\u636e\u3002<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">ByteString<\/td>\n<td style=\"text-align: left;\">str<\/td>\n<td style=\"text-align: left;\">[]byte<\/td>\n<td style=\"text-align: left;\">String (ASCII-8BIT)<\/td>\n<td style=\"text-align: left;\">ByteString<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f60\u53ef\u4ee5\u5728\u6587\u7ae0<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding?hl=zh-cn\">Protocol Buffer \u7f16\u7801<\/a>\u4e2d\uff0c\u627e\u5230\u66f4\u591a\u201c\u5e8f\u5217\u5316\u6d88\u606f\u65f6\u5404\u79cd\u7c7b\u578b\u5982\u4f55\u7f16\u7801\u201d\u7684\u4fe1\u606f\u3002<\/p>\n<ol>\n<li>\u5728java\u4e2d\uff0c\u65e0\u7b26\u53f732\u4f4d\u548c64\u4f4d\u6574\u578b\u88ab\u8868\u793a\u6210\u4ed6\u4eec\u7684\u6574\u578b\u5bf9\u5e94\u5f62\u5f0f\uff0c\u6700\u9ad8\u4f4d\u88ab\u50a8\u5b58\u5728\u6807\u5fd7\u4f4d\u4e2d\u3002<\/li>\n<li>\u5bf9\u4e8e\u6240\u6709\u7684\u60c5\u51b5\uff0c\u8bbe\u5b9a\u503c\u4f1a\u6267\u884c\u7c7b\u578b\u68c0\u67e5\u4ee5\u786e\u4fdd\u6b64\u503c\u662f\u6709\u6548\u3002<\/li>\n<li>64\u4f4d\u6216\u8005\u65e0\u7b26\u53f732\u4f4d\u6574\u578b\u5728\u89e3\u7801\u65f6\u88ab\u8868\u793a\u6210\u4e3ailong\uff0c\u4f46\u662f\u5728\u8bbe\u7f6e\u65f6\u53ef\u4ee5\u4f7f\u7528int\u578b\u503c\u8bbe\u5b9a\uff0c\u5728\u6240\u6709\u7684\u60c5\u51b5\u4e0b\uff0c\u503c\u5fc5\u987b\u7b26\u5408\u5176\u8bbe\u7f6e\u5176\u7c7b\u578b\u7684\u8981\u6c42\u3002<\/li>\n<li>python\u4e2dstring\u88ab\u8868\u793a\u6210\u5728\u89e3\u7801\u65f6\u8868\u793a\u6210unicode\u3002\u4f46\u662f\u4e00\u4e2aASCIIstring\u53ef\u4ee5\u88ab\u8868\u793a\u6210str\u7c7b\u578b\u3002<\/li>\n<li>Integer\u572864\u4f4d\u7684\u673a\u5668\u4e0a\u4f7f\u7528\uff0cstring\u572832\u4f4d\u673a\u5668\u4e0a\u4f7f\u7528<\/li>\n<\/ol>\n<h2>\u9ed8\u8ba4\u503c<\/h2>\n<p>\u5f53\u4e00\u4e2a\u6d88\u606f\u88ab\u89e3\u6790\u7684\u65f6\u5019\uff0c\u5982\u679c\u88ab\u7f16\u7801\u7684\u4fe1\u606f\u4e0d\u5305\u542b\u4e00\u4e2a\u7279\u5b9a\u7684singular\u5143\u7d20\uff0c\u88ab\u89e3\u6790\u7684\u5bf9\u8c61\u9501\u5bf9\u5e94\u7684\u57df\u88ab\u8bbe\u7f6e\u4f4d\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7c7b\u578b\u6307\u5b9a\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8estring\uff0c\u9ed8\u8ba4\u662f\u4e00\u4e2a\u7a7astring<\/li>\n<li>\u5bf9\u4e8ebytes\uff0c\u9ed8\u8ba4\u662f\u4e00\u4e2a\u7a7a\u7684bytes<\/li>\n<li>\u5bf9\u4e8ebool\uff0c\u9ed8\u8ba4\u662ffalse<\/li>\n<li>\u5bf9\u4e8e\u6570\u503c\u7c7b\u578b\uff0c\u9ed8\u8ba4\u662f0<\/li>\n<li>\u5bf9\u4e8e\u679a\u4e3e\uff0c\u9ed8\u8ba4\u662f\u7b2c\u4e00\u4e2a\u5b9a\u4e49\u7684\u679a\u4e3e\u503c\uff0c\u5fc5\u987b\u4e3a0;<\/li>\n<li>\u5bf9\u4e8e\u6d88\u606f\u7c7b\u578b\uff08message\uff09\uff0c\u57df\u6ca1\u6709\u88ab\u8bbe\u7f6e\uff0c\u786e\u5207\u7684\u6d88\u606f\u662f\u6839\u636e\u8bed\u8a00\u786e\u5b9a\u7684\uff0c\u8be6\u89c1generated code guide<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u53ef\u91cd\u590d\u57df\u7684\u9ed8\u8ba4\u503c\u662f\u7a7a\uff08\u901a\u5e38\u60c5\u51b5\u4e0b\u662f\u5bf9\u5e94\u8bed\u8a00\u4e2d\u7a7a\u5217\u8868\uff09\u3002<\/p>\n<p><em>\u6ce8\uff1a\u5bf9\u4e8e\u6807\u91cf\u6d88\u606f\u57df\uff0c\u4e00\u65e6\u6d88\u606f\u88ab\u89e3\u6790\uff0c\u5c31\u65e0\u6cd5\u5224\u65ad\u57df\u91ca\u653e\u88ab\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\uff08\u4f8b\u5982\uff0c\u4f8b\u5982boolean\u503c\u662f\u5426\u88ab\u8bbe\u7f6e\u4e3afalse\uff09\u8fd8\u662f\u6839\u672c\u6ca1\u6709\u88ab\u8bbe\u7f6e\u3002\u4f60\u5e94\u8be5\u5728\u5b9a\u4e49\u4f60\u7684\u6d88\u606f\u7c7b\u578b\u65f6\u975e\u5e38\u6ce8\u610f\u3002\u4f8b\u5982\uff0c\u6bd4\u5982\u4f60\u4e0d\u5e94\u8be5\u5b9a\u4e49boolean\u7684\u9ed8\u8ba4\u503cfalse\u4f5c\u4e3a\u4efb\u4f55\u884c\u4e3a\u7684\u89e6\u53d1\u65b9\u5f0f\u3002\u4e5f\u5e94\u8be5\u6ce8\u610f\u5982\u679c\u4e00\u4e2a\u6807\u91cf\u6d88\u606f\u57df\u88ab\u8bbe\u7f6e\u4e3a\u6807\u5fd7\u4f4d\uff0c\u8fd9\u4e2a\u503c\u4e0d\u5e94\u8be5\u88ab\u5e8f\u5217\u5316\u4f20\u8f93\u3002<\/em><\/p>\n<p>\u67e5\u770b<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/overview?hl=zh-cn\">generated code guide<\/a>\u9009\u62e9\u4f60\u7684\u8bed\u8a00\u7684\u9ed8\u8ba4\u503c\u7684\u5de5\u4f5c\u7ec6\u8282\u3002<\/p>\n<h2>\u679a\u4e3e<\/h2>\n<p>\u5f53\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u6d88\u606f\u7c7b\u578b\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u60f3\u4e3a\u4e00\u4e2a\u5b57\u6bb5\u6307\u5b9a\u67d0\u201c\u9884\u5b9a\u4e49\u503c\u5e8f\u5217\u201d\u4e2d\u7684\u4e00\u4e2a\u503c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u8981\u4e3a\u6bcf\u4e00\u4e2aSearchRequest\u6d88\u606f\u6dfb\u52a0\u4e00\u4e2a corpus\u5b57\u6bb5\uff0c\u800ccorpus\u7684\u503c\u53ef\u80fd\u662fUNIVERSAL\uff0cWEB\uff0cIMAGES\uff0cLOCAL\uff0cNEWS\uff0cPRODUCTS\u6216VIDEO\u4e2d\u7684\u4e00\u4e2a\u3002 \u5176\u5b9e\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a\u901a\u8fc7\u5411\u6d88\u606f\u5b9a\u4e49\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u679a\u4e3e\uff08enum\uff09\u5e76\u4e14\u4e3a\u6bcf\u4e2a\u53ef\u80fd\u7684\u503c\u5b9a\u4e49\u4e00\u4e2a\u5e38\u91cf\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5728\u6d88\u606f\u683c\u5f0f\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u53eb\u505aCorpus\u7684\u679a\u4e3e\u7c7b\u578b\u2014\u2014\u5b83\u542b\u6709\u6240\u6709\u53ef\u80fd\u7684\u503c \u2014\u2014\u4ee5\u53ca\u4e00\u4e2a\u7c7b\u578b\u4e3aCorpus\u7684\u5b57\u6bb5\uff1a<\/p>\n<pre><code>message SearchRequest {\n  string query = 1;\n  int32 page_number = 2;\n  int32 result_per_page = 3;\n  enum Corpus {\n    UNIVERSAL = 0;\n    WEB = 1;\n    IMAGES = 2;\n    LOCAL = 3;\n    NEWS = 4;\n    PRODUCTS = 5;\n    VIDEO = 6;\n  }\n  Corpus corpus = 4;\n}<\/code><\/pre>\n<p>\u5982\u4f60\u6240\u89c1\uff0cCorpus\u679a\u4e3e\u7684\u7b2c\u4e00\u4e2a\u5e38\u91cf\u6620\u5c04\u4e3a0\uff1a\u6bcf\u4e2a\u679a\u4e3e\u7c7b\u578b\u5fc5\u987b\u5c06\u5176\u7b2c\u4e00\u4e2a\u7c7b\u578b\u6620\u5c04\u4e3a0\uff0c\u8fd9\u662f\u56e0\u4e3a\uff1a<\/p>\n<ul>\n<li>\u5fc5\u987b\u6709\u6709\u4e00\u4e2a0\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u8fd9\u4e2a0\u503c\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u3002<\/li>\n<li>\u8fd9\u4e2a\u96f6\u503c\u5fc5\u987b\u4e3a\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4e3a\u4e86\u517c\u5bb9proto2\u8bed\u4e49\uff0c\u679a\u4e3e\u7c7b\u7684\u7b2c\u4e00\u4e2a\u503c\u603b\u662f\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ul>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e0d\u540c\u7684\u679a\u4e3e\u5e38\u91cf\u6307\u5b9a\u4f4d\u76f8\u540c\u7684\u503c\u3002\u5982\u679c\u8fd9\u6837\u505a\u4f60\u9700\u8981\u5c06allow_alias\u8bbe\u5b9a\u4f4dtrue\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u4f1a\u5728\u522b\u540d\u7684\u5730\u65b9\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<pre><code>enum EnumAllowingAlias {\n  option allow_alias = true;\n  UNKNOWN = 0;\n  STARTED = 1;\n  RUNNING = 1;\n}\nenum EnumNotAllowingAlias {\n  UNKNOWN = 0;\n  STARTED = 1;\n  \/\/ RUNNING = 1;  \/\/ Uncommenting this line will cause a compile error inside Google and a warning message outside.\n}<\/code><\/pre>\n<p>\u679a\u4e3e\u5e38\u91cf\u5fc5\u987b\u572832\u4f4d\u6574\u578b\u503c\u7684\u8303\u56f4\u5185\u3002\u56e0\u4e3aenum\u503c\u662f\u4f7f\u7528\u53ef\u53d8\u7f16\u7801\u65b9\u5f0f\u7684\uff0c\u5bf9\u8d1f\u6570\u4e0d\u591f\u9ad8\u6548\uff0c\u56e0\u6b64\u4e0d\u63a8\u8350\u5728enum\u4e2d\u4f7f\u7528\u8d1f\u6570\u3002\u5982\u4e0a\u4f8b\u6240\u793a\uff0c\u53ef\u4ee5\u5728 \u4e00\u4e2a\u6d88\u606f\u5b9a\u4e49\u7684\u5185\u90e8\u6216\u5916\u90e8\u5b9a\u4e49\u679a\u4e3e\u2014\u2014\u8fd9\u4e9b\u679a\u4e3e\u53ef\u4ee5\u5728.proto\u6587\u4ef6\u4e2d\u7684\u4efb\u4f55\u6d88\u606f\u5b9a\u4e49\u91cc\u91cd\u7528\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u5728\u4e00\u4e2a\u6d88\u606f\u4e2d\u58f0\u660e\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\uff0c\u800c\u5728\u53e6\u4e00\u4e2a\u4e0d\u540c \u7684\u6d88\u606f\u4e2d\u4f7f\u7528\u5b83\u2014\u2014\u91c7\u7528MessageType.EnumType\u7684\u8bed\u6cd5\u683c\u5f0f\u3002<\/p>\n<p>\u5f53\u5bf9\u4e00\u4e2a\u4f7f\u7528\u4e86\u679a\u4e3e\u7684.proto\u6587\u4ef6\u8fd0\u884cprotocol buffer\u7f16\u8bd1\u5668\u7684\u65f6\u5019\uff0c\u751f\u6210\u7684\u4ee3\u7801\u4e2d\u5c06\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684enum\uff08\u5bf9Java\u6216C++\u6765\u8bf4\uff09\uff0c\u6216\u8005\u4e00\u4e2a\u7279\u6b8a\u7684EnumDescriptor\u7c7b\uff08\u5bf9 Python\u6765\u8bf4\uff09\uff0c\u5b83\u88ab\u7528\u6765\u5728\u8fd0\u884c\u65f6\u751f\u6210\u7684\u7c7b\u4e2d\u521b\u5efa\u4e00\u7cfb\u5217\u7684\u6574\u578b\u503c\u7b26\u53f7\u5e38\u91cf\uff08symbolic constants\uff09\u3002<\/p>\n<p>\u5728\u53cd\u5e8f\u5217\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u65e0\u6cd5\u8bc6\u522b\u7684\u679a\u4e3e\u503c\u4f1a\u88ab\u4fdd\u5b58\u5728\u6d88\u606f\u4e2d\uff0c\u867d\u7136\u8fd9\u79cd\u8868\u793a\u65b9\u5f0f\u9700\u8981\u4f9d\u636e\u6240\u4f7f\u7528\u8bed\u8a00\u800c\u5b9a\u3002\u5728\u90a3\u4e9b\u652f\u6301\u5f00\u653e\u679a\u4e3e\u7c7b\u578b\u8d85\u51fa\u6307\u5b9a\u8303\u56f4\u4e4b\u5916\u7684\u8bed\u8a00\u4e2d\uff08\u4f8b\u5982C++\u548cGo\uff09\uff0c\u4e3a\u8bc6\u522b\u7684\u503c\u4f1a\u88ab\u8868\u793a\u6210\u6240\u652f\u6301\u7684\u6574\u578b\u3002\u5728\u4f7f\u7528\u5c01\u95ed\u679a\u4e3e\u7c7b\u578b\u7684\u8bed\u8a00\u4e2d\uff08Java\uff09\uff0c\u4f7f\u7528\u679a\u4e3e\u4e2d\u7684\u4e00\u4e2a\u7c7b\u578b\u6765\u8868\u793a\u672a\u8bc6\u522b\u7684\u503c\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528\u6240\u652f\u6301\u6574\u578b\u6765\u8bbf\u95ee\u3002\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u89e3\u6790\u7684\u6d88\u606f\u88ab\u5e8f\u5217\u53f7\uff0c\u672a\u8bc6\u522b\u7684\u503c\u5c06\u4fdd\u6301\u539f\u6837\u3002<\/p>\n<p>\u5173\u4e8e\u5982\u4f55\u5728\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u6d88\u606f\u4e2d\u4f7f\u7528\u679a\u4e3e\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u67e5\u770b\u6240\u9009\u62e9\u7684\u8bed\u8a00<a href=\"http:\/\/code.google.com\/intl\/zh-CN\/apis\/protocolbuffers\/docs\/reference\/overview.html\">generated code guide<\/a>\u3002<\/p>\n<h2>\u4f7f\u7528\u5176\u4ed6\u6d88\u606f\u7c7b\u578b<\/h2>\n<p>\u4f60\u53ef\u4ee5\u5c06\u5176\u4ed6\u6d88\u606f\u7c7b\u578b\u7528\u4f5c\u5b57\u6bb5\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u5728\u6bcf\u4e00\u4e2aSearchResponse\u6d88\u606f\u4e2d\u5305\u542bResult\u6d88\u606f\uff0c\u6b64\u65f6\u53ef\u4ee5\u5728\u76f8\u540c\u7684.proto\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2aResult\u6d88\u606f\u7c7b\u578b\uff0c\u7136\u540e\u5728SearchResponse\u6d88\u606f\u4e2d\u6307\u5b9a\u4e00\u4e2aResult\u7c7b\u578b\u7684\u5b57\u6bb5\uff0c\u5982\uff1a<\/p>\n<pre><code>message SearchResponse {\n  repeated Result results = 1;\n}\n\nmessage Result {\n  string url = 1;\n  string title = 2;\n  repeated string snippets = 3;\n}<\/code><\/pre>\n<h3>\u5bfc\u5165\u5b9a\u4e49<\/h3>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cResult\u6d88\u606f\u7c7b\u578b\u4e0eSearchResponse\u662f\u5b9a\u4e49\u5728\u540c\u4e00\u6587\u4ef6\u4e2d\u7684\u3002\u5982\u679c\u60f3\u8981\u4f7f\u7528\u7684\u6d88\u606f\u7c7b\u578b\u5df2\u7ecf\u5728\u5176\u4ed6.proto\u6587\u4ef6\u4e2d\u5df2\u7ecf\u5b9a\u4e49\u8fc7\u4e86\u5462\uff1f<br \/>\n\u4f60\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\uff08importing\uff09\u5176\u4ed6.proto\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u6765\u4f7f\u7528\u5b83\u4eec\u3002\u8981\u5bfc\u5165\u5176\u4ed6.proto\u6587\u4ef6\u7684\u5b9a\u4e49\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5bfc\u5165\u58f0\u660e\uff0c\u5982\uff1a<\/p>\n<pre><code>import &quot;myproject\/other_protos.proto&quot;;<\/code><\/pre>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f60\u53ea\u80fd\u4f7f\u7528\u76f4\u63a5\u5bfc\u5165\u7684.proto\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49. \u7136\u800c\uff0c \u6709\u65f6\u5019\u4f60\u9700\u8981\u79fb\u52a8\u4e00\u4e2a.proto\u6587\u4ef6\u5230\u4e00\u4e2a\u65b0\u7684\u4f4d\u7f6e\uff0c \u53ef\u4ee5\u4e0d\u76f4\u63a5\u79fb\u52a8.proto\u6587\u4ef6\uff0c \u53ea\u9700\u653e\u5165\u4e00\u4e2a\u4f2a .proto \u6587\u4ef6\u5728\u8001\u7684\u4f4d\u7f6e\uff0c \u7136\u540e\u4f7f\u7528import public\u8f6c\u5411\u65b0\u7684\u4f4d\u7f6e\u3002import public \u4f9d\u8d56\u6027\u4f1a\u901a\u8fc7\u4efb\u610f\u5bfc\u5165\u5305\u542bimport public\u58f0\u660e\u7684proto\u6587\u4ef6\u4f20\u9012\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\/\/ \u8fd9\u662f\u65b0\u7684proto\n\/\/ All definitions are moved here\n\/\/ \u8fd9\u662f\u4e45\u7684proto\n\/\/ \u8fd9\u662f\u6240\u6709\u5ba2\u6237\u7aef\u6b63\u5728\u5bfc\u5165\u7684\u5305\nimport public &quot;new.proto&quot;;\nimport &quot;other.proto&quot;;\n\/\/ \u5ba2\u6237\u7aefproto\nimport &quot;old.proto&quot;;\n\/\/ \u73b0\u5728\u4f60\u53ef\u4ee5\u4f7f\u7528\u65b0\u65e7\u4e24\u79cd\u5305\u7684proto\u5b9a\u4e49\u4e86\u3002<\/code><\/pre>\n<p>\u901a\u8fc7\u5728\u7f16\u8bd1\u5668\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u4f7f\u7528-I\/--proto_pathprotocal \u7f16\u8bd1\u5668\u4f1a\u5728\u6307\u5b9a\u76ee\u5f55\u641c\u7d22\u8981\u5bfc\u5165\u7684\u6587\u4ef6\u3002\u5982\u679c\u6ca1\u6709\u7ed9\u51fa\u6807\u5fd7\uff0c\u7f16\u8bd1\u5668\u4f1a\u641c\u7d22\u7f16\u8bd1\u547d\u4ee4\u88ab\u8c03\u7528\u7684\u76ee\u5f55\u3002\u901a\u5e38\u4f60\u53ea\u8981\u6307\u5b9aproto_path\u6807\u5fd7\u4e3a\u4f60\u7684\u5de5\u7a0b\u6839\u76ee\u5f55\u5c31\u597d\u3002\u5e76\u4e14\u6307\u5b9a\u597d\u5bfc\u5165\u7684\u6b63\u786e\u540d\u79f0\u5c31\u597d\u3002<\/p>\n<h3>\u4f7f\u7528proto2\u6d88\u606f\u7c7b\u578b<\/h3>\n<p>\u5728\u4f60\u7684proto3\u6d88\u606f\u4e2d\u5bfc\u5165proto2\u7684\u6d88\u606f\u7c7b\u578b\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u53cd\u4e4b\u4ea6\u7136\uff0c\u7136\u540eproto2\u679a\u4e3e\u4e0d\u53ef\u4ee5\u76f4\u63a5\u5728proto3\u7684\u6807\u8bc6\u7b26\u4e2d\u4f7f\u7528\uff08\u5982\u679c\u4ec5\u4ec5\u5728proto2\u6d88\u606f\u4e2d\u4f7f\u7528\u662f\u53ef\u4ee5\u7684\uff09\u3002<\/p>\n<h2>\u5d4c\u5957\u7c7b\u578b<\/h2>\n<p>\u4f60\u53ef\u4ee5\u5728\u5176\u4ed6\u6d88\u606f\u7c7b\u578b\u4e2d\u5b9a\u4e49\u3001\u4f7f\u7528\u6d88\u606f\u7c7b\u578b\uff0c\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cResult\u6d88\u606f\u5c31\u5b9a\u4e49\u5728SearchResponse\u6d88\u606f\u5185\uff0c\u5982\uff1a<\/p>\n<pre><code>message SearchResponse {\n  message Result {\n    string url = 1;\n    string title = 2;\n    repeated string snippets = 3;\n  }\n  repeated Result results = 1;\n}<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u5728\u5b83\u7684\u7236\u6d88\u606f\u7c7b\u578b\u7684\u5916\u90e8\u91cd\u7528\u8fd9\u4e2a\u6d88\u606f\u7c7b\u578b\uff0c\u4f60\u9700\u8981\u4ee5Parent.Type\u7684\u5f62\u5f0f\u4f7f\u7528\u5b83\uff0c\u5982\uff1a<\/p>\n<pre><code>message SomeOtherMessage {\n  SearchResponse.Result result = 1;\n}<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06\u6d88\u606f\u5d4c\u5957\u4efb\u610f\u591a\u5c42\uff0c\u5982\uff1a<\/p>\n<pre><code>message Outer {                  \/\/ Level 0\n  message MiddleAA {  \/\/ Level 1\n    message Inner {   \/\/ Level 2\n      int64 ival = 1;\n      bool  booly = 2;\n    }\n  }\n  message MiddleBB {  \/\/ Level 1\n    message Inner {   \/\/ Level 2\n      int32 ival = 1;\n      bool  booly = 2;\n    }\n  }\n}<\/code><\/pre>\n<h2>\u66f4\u65b0\u4e00\u4e2a\u6d88\u606f\u7c7b\u578b<\/h2>\n<p>\u5982\u679c\u4e00\u4e2a\u5df2\u6709\u7684\u6d88\u606f\u683c\u5f0f\u5df2\u65e0\u6cd5\u6ee1\u8db3\u65b0\u7684\u9700\u6c42\u2014\u2014\u5982\uff0c\u8981\u5728\u6d88\u606f\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u5b57\u6bb5\u2014\u2014\u4f46\u662f\u540c\u65f6\u65e7\u7248\u672c\u5199\u7684\u4ee3\u7801\u4ecd\u7136\u53ef\u7528\u3002\u4e0d\u7528\u62c5\u5fc3\uff01\u66f4\u65b0\u6d88\u606f\u800c\u4e0d\u7834\u574f\u5df2\u6709\u4ee3\u7801\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002\u5728\u66f4\u65b0\u65f6\u53ea\u8981\u8bb0\u4f4f\u4ee5\u4e0b\u7684\u89c4\u5219\u5373\u53ef\u3002<\/p>\n<ul>\n<li>\u4e0d\u8981\u66f4\u6539\u4efb\u4f55\u5df2\u6709\u7684\u5b57\u6bb5\u7684\u6570\u503c\u6807\u8bc6\u3002<\/li>\n<li>\u5982\u679c\u4f60\u589e\u52a0\u65b0\u7684\u5b57\u6bb5\uff0c\u4f7f\u7528\u65e7\u683c\u5f0f\u7684\u5b57\u6bb5\u4ecd\u7136\u53ef\u4ee5\u88ab\u4f60\u65b0\u4ea7\u751f\u7684\u4ee3\u7801\u6240\u89e3\u6790\u3002\u4f60\u5e94\u8be5\u8bb0\u4f4f\u8fd9\u4e9b\u5143\u7d20\u7684\u9ed8\u8ba4\u503c\u8fd9\u6837\u4f60\u7684\u65b0\u4ee3\u7801\u5c31\u53ef\u4ee5\u4ee5\u9002\u5f53\u7684\u65b9\u5f0f\u548c\u65e7\u4ee3\u7801\u4ea7\u751f\u7684\u6570\u636e\u4ea4\u4e92\u3002\u76f8\u4f3c\u7684\uff0c\u901a\u8fc7\u65b0\u4ee3\u7801\u4ea7\u751f\u7684\u6d88\u606f\u4e5f\u53ef\u4ee5\u88ab\u65e7\u4ee3\u7801\u89e3\u6790\uff1a\u53ea\u4e0d\u8fc7\u65b0\u7684\u5b57\u6bb5\u4f1a\u88ab\u5ffd\u89c6\u6389\u3002\u6ce8\u610f\uff0c\u672a\u88ab\u8bc6\u522b\u7684\u5b57\u6bb5\u4f1a\u5728\u53cd\u5e8f\u5217\u5316\u7684\u8fc7\u7a0b\u4e2d\u4e22\u5f03\u6389\uff0c\u6240\u4ee5\u5982\u679c\u6d88\u606f\u518d\u88ab\u4f20\u9012\u7ed9\u65b0\u7684\u4ee3\u7801\uff0c\u65b0\u7684\u5b57\u6bb5\u4f9d\u7136\u662f\u4e0d\u53ef\u7528\u7684\uff08\u8fd9\u548cproto2\u4e2d\u7684\u884c\u4e3a\u662f\u4e0d\u540c\u7684\uff0c\u5728proto2\u4e2d\u672a\u5b9a\u4e49\u7684\u57df\u4f9d\u7136\u4f1a\u968f\u7740\u6d88\u606f\u88ab\u5e8f\u5217\u5316\uff09<\/li>\n<li>\u975erequired\u7684\u5b57\u6bb5\u53ef\u4ee5\u79fb\u9664\u2014\u2014\u53ea\u8981\u5b83\u4eec\u7684\u6807\u8bc6\u53f7\u5728\u65b0\u7684\u6d88\u606f\u7c7b\u578b\u4e2d\u4e0d\u518d\u4f7f\u7528\uff08\u66f4\u597d\u7684\u505a\u6cd5\u53ef\u80fd\u662f\u91cd\u547d\u540d\u90a3\u4e2a\u5b57\u6bb5\uff0c\u4f8b\u5982\u5728\u5b57\u6bb5\u524d\u6dfb\u52a0\u201cOBSOLETE_\u201d\u524d\u7f00\uff0c\u90a3\u6837\u7684\u8bdd\uff0c\u4f7f\u7528\u7684.proto\u6587\u4ef6\u7684\u7528\u6237\u5c06\u6765\u5c31\u4e0d\u4f1a\u65e0\u610f\u4e2d\u91cd\u65b0\u4f7f\u7528\u4e86\u90a3\u4e9b\u4e0d\u8be5\u4f7f\u7528\u7684\u6807\u8bc6\u53f7\uff09\u3002<\/li>\n<li>int32, uint32, int64, uint64,\u548cbool\u662f\u5168\u90e8\u517c\u5bb9\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u7c7b\u578b\u4e2d\u7684\u4e00\u4e2a\u8f6c\u6362\u4e3a\u53e6\u5916\u4e00\u4e2a\uff0c\u800c\u4e0d\u4f1a\u7834\u574f\u5411\u524d\u3001 \u5411\u540e\u7684\u517c\u5bb9\u6027\u3002\u5982\u679c\u89e3\u6790\u51fa\u6765\u7684\u6570\u5b57\u4e0e\u5bf9\u5e94\u7684\u7c7b\u578b\u4e0d\u76f8\u7b26\uff0c\u90a3\u4e48\u7ed3\u679c\u5c31\u50cf\u5728C++\u4e2d\u5bf9\u5b83\u8fdb\u884c\u4e86\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u4e00\u6837\uff08\u4f8b\u5982\uff0c\u5982\u679c\u628a\u4e00\u4e2a64\u4f4d\u6570\u5b57\u5f53\u4f5cint32\u6765 \u8bfb\u53d6\uff0c\u90a3\u4e48\u5b83\u5c31\u4f1a\u88ab\u622a\u65ad\u4e3a32\u4f4d\u7684\u6570\u5b57\uff09\u3002<\/li>\n<li>sint32\u548csint64\u662f\u4e92\u76f8\u517c\u5bb9\u7684\uff0c\u4f46\u662f\u5b83\u4eec\u4e0e\u5176\u4ed6\u6574\u6570\u7c7b\u578b\u4e0d\u517c\u5bb9\u3002<\/li>\n<li>string\u548cbytes\u662f\u517c\u5bb9\u7684\u2014\u2014\u53ea\u8981bytes\u662f\u6709\u6548\u7684UTF-8\u7f16\u7801\u3002<\/li>\n<li>\u5d4c\u5957\u6d88\u606f\u4e0ebytes\u662f\u517c\u5bb9\u7684\u2014\u2014\u53ea\u8981bytes\u5305\u542b\u8be5\u6d88\u606f\u7684\u4e00\u4e2a\u7f16\u7801\u8fc7\u7684\u7248\u672c\u3002<\/li>\n<li>fixed32\u4e0esfixed32\u662f\u517c\u5bb9\u7684\uff0cfixed64\u4e0esfixed64\u662f\u517c\u5bb9\u7684\u3002<\/li>\n<li>\u679a\u4e3e\u7c7b\u578b\u4e0eint32\uff0cuint32\uff0cint64\u548cuint64\u76f8\u517c\u5bb9\uff08\u6ce8\u610f\u5982\u679c\u503c\u4e0d\u76f8\u517c\u5bb9\u5219\u4f1a\u88ab\u622a\u65ad\uff09\uff0c\u7136\u800c\u5728\u5ba2\u6237\u7aef\u53cd\u5e8f\u5217\u5316\u4e4b\u540e\u4ed6\u4eec\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u4f8b\u5982\uff0c\u672a\u8bc6\u522b\u7684proto3\u679a\u4e3e\u7c7b\u578b\u4f1a\u88ab\u4fdd\u7559\u5728\u6d88\u606f\u4e2d\uff0c\u4f46\u662f\u4ed6\u7684\u8868\u793a\u65b9\u5f0f\u4f1a\u4f9d\u7167\u8bed\u8a00\u800c\u5b9a\u3002int\u7c7b\u578b\u7684\u5b57\u6bb5\u603b\u4f1a\u4fdd\u7559\u4ed6\u4eec\u7684<\/li>\n<\/ul>\n<h2>Any<\/h2>\n<p>Any\u7c7b\u578b\u6d88\u606f\u5141\u8bb8\u4f60\u5728\u6ca1\u6709\u6307\u5b9a\u4ed6\u4eec\u7684.proto\u5b9a\u4e49\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u6d88\u606f\u4f5c\u4e3a\u4e00\u4e2a\u5d4c\u5957\u7c7b\u578b\u3002\u4e00\u4e2aAny\u7c7b\u578b\u5305\u62ec\u4e00\u4e2a\u53ef\u4ee5\u88ab\u5e8f\u5217\u5316bytes\u7c7b\u578b\u7684\u4efb\u610f\u6d88\u606f\uff0c\u4ee5\u53ca\u4e00\u4e2aURL\u4f5c\u4e3a\u4e00\u4e2a\u5168\u5c40\u6807\u8bc6\u7b26\u548c\u89e3\u6790\u6d88\u606f\u7c7b\u578b\u3002\u4e3a\u4e86\u4f7f\u7528Any\u7c7b\u578b\uff0c\u4f60\u9700\u8981\u5bfc\u5165<code>import google\/protobuf\/any.proto<\/code>\u3002<\/p>\n<pre><code>import &quot;google\/protobuf\/any.proto&quot;;\n\nmessage ErrorStatus {\n  string message = 1;\n  repeated google.protobuf.Any details = 2;\n}<\/code><\/pre>\n<p>\u5bf9\u4e8e\u7ed9\u5b9a\u7684\u6d88\u606f\u7c7b\u578b\u7684\u9ed8\u8ba4\u7c7b\u578bURL\u662ftype.googleapis.com\/packagename.messagename\u3002<\/p>\n<p>\u4e0d\u540c\u8bed\u8a00\u7684\u5b9e\u73b0\u4f1a\u652f\u6301\u52a8\u6001\u5e93\u4ee5\u7ebf\u7a0b\u5b89\u5168\u7684\u65b9\u5f0f\u53bb\u5e2e\u52a9\u5c01\u88c5\u6216\u8005\u89e3\u5c01\u88c5Any\u503c\u3002\u4f8b\u5982\u5728java\u4e2d\uff0cAny\u7c7b\u578b\u4f1a\u6709\u7279\u6b8a\u7684<code>pack()<\/code>\u548c<code>unpack()<\/code>\u8bbf\u95ee\u5668\uff0c\u5728C++\u4e2d\u4f1a\u6709<code>PackFrom()<\/code>\u548c<code>UnpackTo()<\/code>\u65b9\u6cd5\u3002<\/p>\n<pre><code>\/\/ Storing an arbitrary message type in Any.\nNetworkErrorDetails details = ...;\nErrorStatus status;\nstatus.add_details()-&gt;PackFrom(details);\n\n\/\/ Reading an arbitrary message from Any.\nErrorStatus status = ...;\nfor (const Any&amp; detail : status.details()) {\n  if (detail.Is&lt;NetworkErrorDetails&gt;()) {\n    NetworkErrorDetails network_error;\n    detail.UnpackTo(&amp;network_error);\n    ... processing network_error ...\n  }\n}<\/code><\/pre>\n<p>\u76ee\u524d\uff0c\u7528\u4e8eAny\u7c7b\u578b\u7684\u52a8\u6001\u5e93\u4ecd\u5728\u5f00\u53d1\u4e4b\u4e2d<br \/>\n\u5982\u679c\u4f60\u5df2\u7ecf\u5f88\u719f\u6089<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/proto\">proto2\u8bed\u6cd5<\/a>\uff0c\u4f7f\u7528Any\u66ff\u6362<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/proto#extensions\">\u6269\u5c55<\/a>\u3002<\/p>\n<h2>Oneof<\/h2>\n<p>\u5982\u679c\u4f60\u7684\u6d88\u606f\u4e2d\u6709\u5f88\u591a\u53ef\u9009\u5b57\u6bb5\uff0c \u5e76\u4e14\u540c\u65f6\u81f3\u591a\u4e00\u4e2a\u5b57\u6bb5\u4f1a\u88ab\u8bbe\u7f6e\uff0c \u4f60\u53ef\u4ee5\u52a0\u5f3a\u8fd9\u4e2a\u884c\u4e3a\uff0c\u4f7f\u7528oneof\u7279\u6027\u8282\u7701\u5185\u5b58.<\/p>\n<p>Oneof\u5b57\u6bb5\u5c31\u50cf\u53ef\u9009\u5b57\u6bb5\uff0c \u9664\u4e86\u5b83\u4eec\u4f1a\u5171\u4eab\u5185\u5b58\uff0c \u81f3\u591a\u4e00\u4e2a\u5b57\u6bb5\u4f1a\u88ab\u8bbe\u7f6e\u3002 \u8bbe\u7f6e\u5176\u4e2d\u4e00\u4e2a\u5b57\u6bb5\u4f1a\u6e05\u9664\u5176\u5b83\u5b57\u6bb5\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528<code>case()<\/code>\u6216\u8005<code>WhichOneof()<\/code> \u65b9\u6cd5\u68c0\u67e5\u54ea\u4e2aoneof\u5b57\u6bb5\u88ab\u8bbe\u7f6e\uff0c \u770b\u4f60\u4f7f\u7528\u4ec0\u4e48\u8bed\u8a00\u4e86.<\/p>\n<h3>\u4f7f\u7528Oneof<\/h3>\n<p>\u4e3a\u4e86\u5728.proto\u5b9a\u4e49Oneof\u5b57\u6bb5\uff0c \u4f60\u9700\u8981\u5728\u540d\u5b57\u524d\u9762\u52a0\u4e0aoneof\u5173\u952e\u5b57, \u6bd4\u5982\u4e0b\u9762\u4f8b\u5b50\u7684test_oneof:<\/p>\n<pre><code>message SampleMessage {\n  oneof test_oneof {\n    string name = 4;\n    SubMessage sub_message = 9;\n  }\n}<\/code><\/pre>\n<p>\u7136\u540e\u4f60\u53ef\u4ee5\u589e\u52a0oneof\u5b57\u6bb5\u5230 oneof \u5b9a\u4e49\u4e2d. \u4f60\u53ef\u4ee5\u589e\u52a0\u4efb\u610f\u7c7b\u578b\u7684\u5b57\u6bb5, \u4f46\u662f\u4e0d\u80fd\u4f7f\u7528repeated \u5173\u952e\u5b57.<\/p>\n<p>\u5728\u4ea7\u751f\u7684\u4ee3\u7801\u4e2d, oneof\u5b57\u6bb5\u62e5\u6709\u540c\u6837\u7684 getters \u548csetters\uff0c \u5c31\u50cf\u6b63\u5e38\u7684\u53ef\u9009\u5b57\u6bb5\u4e00\u6837. \u4e5f\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u5230\u5e95\u90a3\u4e2a\u5b57\u6bb5\u88ab\u8bbe\u7f6e. \u4f60\u53ef\u4ee5\u5728\u76f8\u5e94\u7684\u8bed\u8a00<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/overview\">API\u6307\u5357<\/a>\u4e2d\u627e\u5230oneof API\u4ecb\u7ecd.<\/p>\n<h3>Oneof \u7279\u6027<\/h3>\n<ul>\n<li>\u8bbe\u7f6eoneof\u4f1a\u81ea\u52a8\u6e05\u695a\u5176\u5b83oneof\u5b57\u6bb5\u7684\u503c. \u6240\u4ee5\u8bbe\u7f6e\u591a\u6b21\u540e\uff0c\u53ea\u6709\u6700\u540e\u4e00\u6b21\u8bbe\u7f6e\u7684\u5b57\u6bb5\u6709\u503c.<\/li>\n<\/ul>\n<pre><code>SampleMessage message;\nmessage.set_name(&quot;name&quot;);\nCHECK(message.has_name());\nmessage.mutable_sub_message();   \/\/ Will clear name field.\nCHECK(!message.has_name());<\/code><\/pre>\n<ul>\n<li>\u5982\u679c\u89e3\u6790\u5668\u9047\u5230\u540c\u4e00\u4e2aoneof\u4e2d\u6709\u591a\u4e2a\u6210\u5458\uff0c\u53ea\u6709\u6700\u4f1a\u4e00\u4e2a\u4f1a\u88ab\u89e3\u6790\u6210\u6d88\u606f\u3002<\/li>\n<li>oneof\u4e0d\u652f\u6301repeated.<\/li>\n<li>\u53cd\u5c04API\u5bf9oneof \u5b57\u6bb5\u6709\u6548.<\/li>\n<li>\u5982\u679c\u4f7f\u7528C++,\u9700\u786e\u4fdd\u4ee3\u7801\u4e0d\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f. \u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u5d29\u6e83\uff0c \u56e0\u4e3asub_message \u5df2\u7ecf\u901a\u8fc7set_name()\u5220\u9664\u4e86<\/li>\n<\/ul>\n<pre><code>SampleMessage message;\nSubMessage* sub_message = message.mutable_sub_message();\nmessage.set_name(&quot;name&quot;);      \/\/ Will delete sub_message\nsub_message-&gt;set_...            \/\/ Crashes here<\/code><\/pre>\n<ul>\n<li>\u5728C++\u4e2d\uff0c\u5982\u679c\u4f60\u4f7f\u7528Swap()\u4e24\u4e2aoneof\u6d88\u606f\uff0c\u6bcf\u4e2a\u6d88\u606f\uff0c\u4e24\u4e2a\u6d88\u606f\u5c06\u62e5\u6709\u5bf9\u65b9\u7684\u503c\uff0c\u4f8b\u5982\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cmsg1\u4f1a\u62e5\u6709sub_message\u5e76\u4e14msg2\u4f1a\u6709name\u3002<\/li>\n<\/ul>\n<pre><code>SampleMessage msg1;\nmsg1.set_name(&quot;name&quot;);\nSampleMessage msg2;\nmsg2.mutable_sub_message();\nmsg1.swap(&amp;msg2);\nCHECK(msg1.has_sub_message());\nCHECK(msg2.has_name());<\/code><\/pre>\n<h3>\u5411\u540e\u517c\u5bb9\u6027\u95ee\u9898<\/h3>\n<p>\u5f53\u589e\u52a0\u6216\u8005\u5220\u9664oneof\u5b57\u6bb5\u65f6\u4e00\u5b9a\u8981\u5c0f\u5fc3. \u5982\u679c\u68c0\u67e5oneof\u7684\u503c\u8fd4\u56deNone\/NOT_SET, \u5b83\u610f\u5473\u7740oneof\u5b57\u6bb5\u6ca1\u6709\u88ab\u8d4b\u503c\u6216\u8005\u5728\u4e00\u4e2a\u4e0d\u540c\u7684\u7248\u672c\u4e2d\u8d4b\u503c\u4e86\u3002 \u4f60\u4e0d\u4f1a\u77e5\u9053\u662f\u54ea\u79cd\u60c5\u51b5\uff0c\u56e0\u4e3a\u6ca1\u6709\u529e\u6cd5\u5224\u65ad\u5982\u679c\u672a\u8bc6\u522b\u7684\u5b57\u6bb5\u662f\u4e00\u4e2aoneof\u5b57\u6bb5\u3002<\/p>\n<p>Tag \u91cd\u7528\u95ee\u9898\uff1a<\/p>\n<ul>\n<li><strong>\u5c06\u5b57\u6bb5\u79fb\u5165\u6216\u79fb\u9664oneof<\/strong>\uff1a\u5728\u6d88\u606f\u88ab\u5e8f\u5217\u53f7\u6216\u8005\u89e3\u6790\u540e\uff0c\u4f60\u4e5f\u8bb8\u4f1a\u5931\u53bb\u4e00\u4e9b\u4fe1\u606f\uff08\u6709\u4e9b\u5b57\u6bb5\u4e5f\u8bb8\u4f1a\u88ab\u6e05\u9664\uff09<\/li>\n<li><strong>\u5220\u9664\u4e00\u4e2a\u5b57\u6bb5\u6216\u8005\u52a0\u5165\u4e00\u4e2a\u5b57\u6bb5<\/strong>\uff1a\u5728\u6d88\u606f\u88ab\u5e8f\u5217\u53f7\u6216\u8005\u89e3\u6790\u540e\uff0c\u8fd9\u4e5f\u8bb8\u4f1a\u6e05\u9664\u4f60\u73b0\u5728\u8bbe\u7f6e\u7684oneof\u5b57\u6bb5<\/li>\n<li><strong>\u5206\u79bb\u6216\u8005\u878d\u5408oneof<\/strong>\uff1a\u884c\u4e3a\u4e0e\u79fb\u52a8\u5e38\u89c4\u5b57\u6bb5\u76f8\u4f3c\u3002<\/li>\n<\/ul>\n<h2>Map<\/h2>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u521b\u5efa\u4e00\u4e2a\u5173\u8054\u6620\u5c04\uff0cprotocol buffer\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre><code>map&lt;key_type, value_type&gt; map_field = N;<\/code><\/pre>\n<p>\u5176\u4e2dkey_type\u53ef\u4ee5\u662f\u4efb\u610fInteger\u6216\u8005string\u7c7b\u578b\uff08\u6240\u4ee5\uff0c\u9664\u4e86floating\u548cbytes\u7684\u4efb\u610f\u6807\u91cf\u7c7b\u578b\u90fd\u662f\u53ef\u4ee5\u7684\uff09value_type\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u521b\u5efa\u4e00\u4e2aproject\u7684\u6620\u5c04\uff0c\u6bcf\u4e2aProjecct\u4f7f\u7528\u4e00\u4e2astring\u4f5c\u4e3akey\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9a\u4e49\uff1a<\/p>\n<pre><code>map&lt;string, Project&gt; projects = 3;<\/code><\/pre>\n<ul>\n<li>Map\u7684\u5b57\u6bb5\u53ef\u4ee5\u662frepeated\u3002<\/li>\n<li>\u5e8f\u5217\u5316\u540e\u7684\u987a\u5e8f\u548cmap\u8fed\u4ee3\u5668\u7684\u987a\u5e8f\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u6240\u4ee5\u4f60\u4e0d\u8981\u671f\u671b\u4ee5\u56fa\u5b9a\u987a\u5e8f\u5904\u7406Map<\/li>\n<li>\u5f53\u4e3a.proto\u6587\u4ef6\u4ea7\u751f\u751f\u6210\u6587\u672c\u683c\u5f0f\u7684\u65f6\u5019\uff0cmap\u4f1a\u6309\u7167key \u7684\u987a\u5e8f\u6392\u5e8f\uff0c\u6570\u503c\u5316\u7684key\u4f1a\u6309\u7167\u6570\u503c\u6392\u5e8f\u3002<\/li>\n<li>\u4ece\u5e8f\u5217\u5316\u4e2d\u89e3\u6790\u6216\u8005\u878d\u5408\u65f6\uff0c\u5982\u679c\u6709\u91cd\u590d\u7684key\u5219\u540e\u4e00\u4e2akey\u4e0d\u4f1a\u88ab\u4f7f\u7528\uff0c\u5f53\u4ece\u6587\u672c\u683c\u5f0f\u4e2d\u89e3\u6790map\u65f6\uff0c\u5982\u679c\u5b58\u5728\u91cd\u590d\u7684key\u3002<\/li>\n<\/ul>\n<p>\u751f\u6210map\u7684API\u73b0\u5728\u5bf9\u4e8e\u6240\u6709proto3\u652f\u6301\u7684\u8bed\u8a00\u90fd\u53ef\u7528\u4e86\uff0c\u4f60\u53ef\u4ee5\u4ece<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/overview\">API\u6307\u5357<\/a>\u627e\u5230\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<h3>\u5411\u540e\u517c\u5bb9\u6027\u95ee\u9898<\/h3>\n<p>map\u8bed\u6cd5\u5e8f\u5217\u5316\u540e\u7b49\u540c\u4e8e\u5982\u4e0b\u5185\u5bb9\uff0c\u56e0\u6b64\u5373\u4f7f\u662f\u4e0d\u652f\u6301map\u8bed\u6cd5\u7684protocol buffer\u5b9e\u73b0\u4e5f\u662f\u53ef\u4ee5\u5904\u7406\u4f60\u7684\u6570\u636e\u7684\uff1a<\/p>\n<pre><code>message MapFieldEntry {\n  key_type key = 1;\n  value_type value = 2;\n}\n\nrepeated MapFieldEntry map_field = N;<\/code><\/pre>\n<h2>Package<\/h2>\n<p>\u5f53\u7136\u53ef\u4ee5\u4e3a.proto\u6587\u4ef6\u65b0\u589e\u4e00\u4e2a\u53ef\u9009\u7684package\u58f0\u660e\u7b26\uff0c\u7528\u6765\u9632\u6b62\u4e0d\u540c\u7684\u6d88\u606f\u7c7b\u578b\u6709\u547d\u540d\u51b2\u7a81\u3002\u5982\uff1a<\/p>\n<pre><code>package foo.bar;\nmessage Open { ... }<\/code><\/pre>\n<p>\u5728\u5176\u4ed6\u7684\u6d88\u606f\u683c\u5f0f\u5b9a\u4e49\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5305\u540d+\u6d88\u606f\u540d\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u57df\u7684\u7c7b\u578b\uff0c\u5982\uff1a<\/p>\n<pre><code>message Foo {\n  ...\n  required foo.bar.Open open = 1;\n  ...\n}<\/code><\/pre>\n<p>\u5305\u7684\u58f0\u660e\u7b26\u4f1a\u6839\u636e\u4f7f\u7528\u8bed\u8a00\u7684\u4e0d\u540c\u5f71\u54cd\u751f\u6210\u7684\u4ee3\u7801\u3002<\/p>\n<ul>\n<li>\u5bf9\u4e8eC++\uff0c\u4ea7\u751f\u7684\u7c7b\u4f1a\u88ab\u5305\u88c5\u5728C++\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u5982\u4e0a\u4f8b\u4e2d\u7684Open\u4f1a\u88ab\u5c01\u88c5\u5728 foo::bar\u7a7a\u95f4\u4e2d\uff1b - \u5bf9\u4e8eJava\uff0c\u5305\u58f0\u660e\u7b26\u4f1a\u53d8\u4e3ajava\u7684\u4e00\u4e2a\u5305\uff0c\u9664\u975e\u5728.proto\u6587\u4ef6\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u660e\u786e\u6709java_package\uff1b<\/li>\n<li>\u5bf9\u4e8e Python\uff0c\u8fd9\u4e2a\u5305\u58f0\u660e\u7b26\u662f\u88ab\u5ffd\u7565\u7684\uff0c\u56e0\u4e3aPython\u6a21\u5757\u662f\u6309\u7167\u5176\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u4f4d\u7f6e\u8fdb\u884c\u7ec4\u7ec7\u7684\u3002<\/li>\n<li>\u5bf9\u4e8eGo\uff0c\u5305\u53ef\u4ee5\u88ab\u7528\u505aGo\u5305\u540d\u79f0\uff0c\u9664\u975e\u4f60\u663e\u5f0f\u7684\u63d0\u4f9b\u4e00\u4e2aoption go_package\u5728\u4f60\u7684.proto\u6587\u4ef6\u4e2d\u3002<\/li>\n<li>\u5bf9\u4e8eRuby\uff0c\u751f\u6210\u7684\u7c7b\u53ef\u4ee5\u88ab\u5305\u88c5\u5728\u5185\u7f6e\u7684Ruby\u540d\u79f0\u7a7a\u95f4\u4e2d\uff0c\u8f6c\u6362\u6210Ruby\u6240\u9700\u7684\u5927\u5c0f\u5199\u6837\u5f0f \uff08\u9996\u5b57\u6bcd\u5927\u5199\uff1b\u5982\u679c\u7b2c\u4e00\u4e2a\u7b26\u53f7\u4e0d\u662f\u4e00\u4e2a\u5b57\u6bcd\uff0c\u5219\u4f7f\u7528PB_\u524d\u7f00\uff09\uff0c\u4f8b\u5982Open\u4f1a\u5728Foo::Bar\u540d\u79f0\u7a7a\u95f4\u4e2d\u3002<\/li>\n<li>\u5bf9\u4e8ejavaNano\u5305\u4f1a\u4f7f\u7528Java\u5305\uff0c\u9664\u975e\u4f60\u5728\u4f60\u7684\u6587\u4ef6\u4e2d\u663e\u5f0f\u7684\u63d0\u4f9b\u4e00\u4e2aoption java_package\u3002<\/li>\n<li>\u5bf9\u4e8eC#\u5305\u53ef\u4ee5\u8f6c\u6362\u4e3aPascalCase\u540e\u4f5c\u4e3a\u540d\u79f0\u7a7a\u95f4\uff0c\u9664\u975e\u4f60\u5728\u4f60\u7684\u6587\u4ef6\u4e2d\u663e\u5f0f\u7684\u63d0\u4f9b\u4e00\u4e2aoption csharp_namespace\uff0c\u4f8b\u5982\uff0cOpen\u4f1a\u5728Foo.Bar\u540d\u79f0\u7a7a\u95f4\u4e2d<\/li>\n<\/ul>\n<h3>\u5305\u53ca\u540d\u79f0\u7684\u89e3\u6790<\/h3>\n<p>Protocol buffer\u8bed\u8a00\u4e2d\u7c7b\u578b\u540d\u79f0\u7684\u89e3\u6790\u4e0eC++\u662f\u4e00\u81f4\u7684\uff1a\u9996\u5148\u4ece\u6700\u5185\u90e8\u5f00\u59cb\u67e5\u627e\uff0c\u4f9d\u6b21\u5411\u5916\u8fdb\u884c\uff0c\u6bcf\u4e2a\u5305\u4f1a\u88ab\u770b\u4f5c\u662f\u5176\u7236\u7c7b\u5305\u7684\u5185\u90e8\u7c7b\u3002\u5f53\u7136\u5bf9\u4e8e \uff08foo.bar.Baz\uff09\u8fd9\u6837\u4ee5\u201c.\u201d\u5206\u9694\u7684\u610f\u5473\u7740\u662f\u4ece\u6700\u5916\u56f4\u5f00\u59cb\u7684\u3002<\/p>\n<p>ProtocolBuffer\u7f16\u8bd1\u5668\u4f1a\u89e3\u6790.proto\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u7c7b\u578b\u540d\u3002 \u5bf9\u4e8e\u4e0d\u540c\u8bed\u8a00\u7684\u4ee3\u7801\u751f\u6210\u5668\u4f1a\u77e5\u9053\u5982\u4f55\u6765\u6307\u5411\u6bcf\u4e2a\u5177\u4f53\u7684\u7c7b\u578b\uff0c\u5373\u4f7f\u5b83\u4eec\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u89c4\u5219\u3002<\/p>\n<h2>\u5b9a\u4e49\u670d\u52a1(Service)<\/h2>\n<p>\u5982\u679c\u60f3\u8981\u5c06\u6d88\u606f\u7c7b\u578b\u7528\u5728RPC(\u8fdc\u7a0b\u65b9\u6cd5\u8c03\u7528)\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u5728.proto\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2aRPC\u670d\u52a1\u63a5\u53e3\uff0cprotocol buffer\u7f16\u8bd1\u5668\u5c06\u4f1a\u6839\u636e\u6240\u9009\u62e9\u7684\u4e0d\u540c\u8bed\u8a00\u751f\u6210\u670d\u52a1\u63a5\u53e3\u4ee3\u7801\u53ca\u5b58\u6839\u3002\u5982\uff0c\u60f3\u8981\u5b9a\u4e49\u4e00\u4e2aRPC\u670d\u52a1\u5e76\u5177\u6709\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u80fd\u591f\u63a5\u6536 SearchRequest\u5e76\u8fd4\u56de\u4e00\u4e2aSearchResponse\uff0c\u6b64\u65f6\u53ef\u4ee5\u5728.proto\u6587\u4ef6\u4e2d\u8fdb\u884c\u5982\u4e0b\u5b9a\u4e49\uff1a<\/p>\n<pre><code>service SearchService {\n  rpc Search (SearchRequest) returns (SearchResponse);\n}<\/code><\/pre>\n<p>\u6700\u76f4\u89c2\u7684\u4f7f\u7528protocol buffer\u7684RPC\u7cfb\u7edf\u662f<a href=\"https:\/\/github.com\/grpc\/grpc-experiments\">gRPC<\/a>,\u4e00\u4e2a\u7531\u8c37\u6b4c\u5f00\u53d1\u7684\u8bed\u8a00\u548c\u5e73\u53f0\u4e2d\u7684\u5f00\u6e90\u7684PRC\u7cfb\u7edf\uff0cgRPC\u5728\u4f7f\u7528protocl buffer\u65f6\u975e\u5e38\u6709\u6548\uff0c\u5982\u679c\u4f7f\u7528\u7279\u6b8a\u7684protocol buffer\u63d2\u4ef6\u53ef\u4ee5\u76f4\u63a5\u4e3a\u60a8\u4ece.proto\u6587\u4ef6\u4e2d\u4ea7\u751f\u76f8\u5173\u7684RPC\u4ee3\u7801\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528gRPC\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528protocol buffer\u7528\u4e8e\u81ea\u5df1\u7684RPC\u5b9e\u73b0\uff0c\u4f60\u53ef\u4ee5\u4ece<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/proto#services\">proto2\u8bed\u8a00\u6307\u5357<\/a>\u4e2d\u627e\u5230\u66f4\u591a\u4fe1\u606f<\/p>\n<p>\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5f00\u53d1\u7684PRC\u5b9e\u73b0\u4f7f\u7528Protocol Buffer\u3002\u53c2\u8003<a href=\"https:\/\/github.com\/google\/protobuf\/blob\/master\/docs\/third_party.md\">\u7b2c\u4e09\u65b9\u63d2\u4ef6wiki<\/a>\u67e5\u770b\u8fd9\u4e9b\u5b9e\u73b0\u7684\u5217\u8868\u3002<\/p>\n<h2>JSON \u6620\u5c04<\/h2>\n<p>Proto3 \u652f\u6301JSON\u7684\u7f16\u7801\u89c4\u8303\uff0c\u4f7f\u4ed6\u66f4\u5bb9\u6613\u5728\u4e0d\u540c\u7cfb\u7edf\u4e4b\u95f4\u5171\u4eab\u6570\u636e\uff0c\u5728\u4e0b\u8868\u4e2d\u9010\u4e2a\u63cf\u8ff0\u7c7b\u578b\u3002<\/p>\n<p>\u5982\u679cJSON\u7f16\u7801\u7684\u6570\u636e\u4e22\u5931\u6216\u8005\u5176\u672c\u8eab\u5c31\u662fnull\uff0c\u8fd9\u4e2a\u6570\u636e\u4f1a\u5728\u89e3\u6790\u6210protocol buffer\u7684\u65f6\u5019\u88ab\u8868\u793a\u6210\u9ed8\u8ba4\u503c\u3002\u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u5728protocol buffer\u4e2d\u8868\u793a\u4e3a\u9ed8\u8ba4\u503c\uff0c\u4f53\u4f1a\u5728\u8f6c\u5316\u6210JSON\u7684\u65f6\u5019\u7f16\u7801\u7684\u65f6\u5019\u5ffd\u7565\u6389\u4ee5\u8282\u7701\u7a7a\u95f4\u3002\u5177\u4f53\u5b9e\u73b0\u53ef\u4ee5\u63d0\u4f9b\u5728JSON\u7f16\u7801\u4e2d\u53ef\u9009\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">proto3<\/th>\n<th style=\"text-align: left;\">JSON<\/th>\n<th style=\"text-align: left;\">JSON\u793a\u4f8b<\/th>\n<th style=\"text-align: left;\">\u6ce8\u610f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">message<\/td>\n<td style=\"text-align: left;\">object<\/td>\n<td style=\"text-align: left;\">{\u201cfBar\u201d: v, \u201cg\u201d: null, \u2026}<\/td>\n<td style=\"text-align: left;\">\u4ea7\u751fJSON\u5bf9\u8c61\uff0c\u6d88\u606f\u5b57\u6bb5\u540d\u53ef\u4ee5\u88ab\u6620\u5c04\u6210lowerCamelCase\u5f62\u5f0f\uff0c\u5e76\u4e14\u6210\u4e3aJSON\u5bf9\u8c61\u952e\uff0cnull\u88ab\u63a5\u53d7\u5e76\u6210\u4e3a\u5bf9\u5e94\u5b57\u6bb5\u7684\u9ed8\u8ba4\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">enum<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">\u201cFOO_BAR\u201d<\/td>\n<td style=\"text-align: left;\">\u679a\u4e3e\u503c\u7684\u540d\u5b57\u5728proto\u6587\u4ef6\u4e2d\u88ab\u6307\u5b9a<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">map<\/td>\n<td style=\"text-align: left;\">object<\/td>\n<td style=\"text-align: left;\">{\u201ck\u201d: v, \u2026}<\/td>\n<td style=\"text-align: left;\">\u6240\u6709\u7684\u952e\u90fd\u88ab\u8f6c\u6362\u6210string<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">repeated V<\/td>\n<td style=\"text-align: left;\">array<\/td>\n<td style=\"text-align: left;\">[v, \u2026]<\/td>\n<td style=\"text-align: left;\">null\u88ab\u89c6\u4e3a\u7a7a\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">bool<\/td>\n<td style=\"text-align: left;\">true, false<\/td>\n<td style=\"text-align: left;\">true, false<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">\u201cHello World!\u201d<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">bytes<\/td>\n<td style=\"text-align: left;\">base64 string<\/td>\n<td style=\"text-align: left;\">\u201cYWJjMTIzIT8kKiYoKSctPUB+\u201d<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">int32, fixed32, uint32<\/td>\n<td style=\"text-align: left;\">number<\/td>\n<td style=\"text-align: left;\">1, -10, 0<\/td>\n<td style=\"text-align: left;\">JSON\u503c\u4f1a\u662f\u4e00\u4e2a\u5341\u8fdb\u5236\u6570\uff0c\u6570\u503c\u578b\u6216\u8005string\u7c7b\u578b\u90fd\u4f1a\u63a5\u53d7<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">int64, fixed64, uint64<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">\u201c1\u201d, \u201c-10\u201d<\/td>\n<td style=\"text-align: left;\">JSON\u503c\u4f1a\u662f\u4e00\u4e2a\u5341\u8fdb\u5236\u6570\uff0c\u6570\u503c\u578b\u6216\u8005string\u7c7b\u578b\u90fd\u4f1a\u63a5\u53d7<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">float, double<\/td>\n<td style=\"text-align: left;\">number<\/td>\n<td style=\"text-align: left;\">1.1, -10.0, 0, \u201cNaN\u201d, \u201cInfinity\u201d<\/td>\n<td style=\"text-align: left;\">JSON\u503c\u4f1a\u662f\u4e00\u4e2a\u6570\u5b57\u6216\u8005\u4e00\u4e2a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u5982\u201dNaN\u201d,\u201dinfinity\u201d\u6216\u8005\u201d-Infinity\u201d\uff0c\u6570\u503c\u578b\u6216\u8005\u5b57\u7b26\u4e32\u90fd\u662f\u53ef\u63a5\u53d7\u7684\uff0c\u6307\u6570\u7b26\u53f7\u4e5f\u53ef\u4ee5\u63a5\u53d7<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">Any<\/td>\n<td style=\"text-align: left;\">object<\/td>\n<td style=\"text-align: left;\">{\u201c@type\u201d: \u201curl\u201d, \u201cf\u201d: v, \u2026 }<\/td>\n<td style=\"text-align: left;\">\u5982\u679c\u4e00\u4e2aAny\u4fdd\u7559\u4e00\u4e2a\u7279\u4e0a\u8ff0\u7684JSON\u6620\u5c04\uff0c\u5219\u5b83\u4f1a\u8f6c\u6362\u6210\u4e00\u4e2a\u5982\u4e0b\u5f62\u5f0f\uff1a<code>{&quot;@type&quot;: xxx, &quot;value&quot;: yyy}<\/code>\u5426\u5219\uff0c\u8be5\u503c\u4f1a\u88ab\u8f6c\u6362\u6210\u4e00\u4e2aJSON\u5bf9\u8c61\uff0c<code>@type<\/code>\u5b57\u6bb5\u4f1a\u88ab\u63d2\u5165\u6240\u6307\u5b9a\u7684\u786e\u5b9a\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">Timestamp<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">\u201c1972-01-01T10:00:20.021Z\u201d<\/td>\n<td style=\"text-align: left;\">\u4f7f\u7528RFC 339\uff0c\u5176\u4e2d\u751f\u6210\u7684\u8f93\u51fa\u5c06\u59cb\u7ec8\u662fZ-\u5f52\u4e00\u5316\u554a\u7684\uff0c\u5e76\u4e14\u4f7f\u75280\uff0c3\uff0c6\u6216\u80059\u4f4d\u5c0f\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">Duration<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">\u201c1.000340012s\u201d, \u201c1s\u201d<\/td>\n<td style=\"text-align: left;\">\u751f\u6210\u7684\u8f93\u51fa\u603b\u662f0\uff0c3\uff0c6\u6216\u80059\u4f4d\u5c0f\u6570\uff0c\u5177\u4f53\u4f9d\u8d56\u4e8e\u6240\u9700\u8981\u7684\u7cbe\u5ea6\uff0c\u63a5\u53d7\u6240\u6709\u53ef\u4ee5\u8f6c\u6362\u4e3a\u7eb3\u79d2\u7ea7\u7684\u7cbe\u5ea6<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">Struct<\/td>\n<td style=\"text-align: left;\">object<\/td>\n<td style=\"text-align: left;\">{ \u2026 }<\/td>\n<td style=\"text-align: left;\">\u4efb\u610f\u7684JSON\u5bf9\u8c61\uff0c\u89c1struct.proto<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">Wrapper types<\/td>\n<td style=\"text-align: left;\">various types<\/td>\n<td style=\"text-align: left;\">2, \u201c2\u201d, \u201cfoo\u201d, true, \u201ctrue\u201d, null, 0, \u2026<\/td>\n<td style=\"text-align: left;\">\u5305\u88c5\u5668\u5728JSON\u4e2d\u7684\u8868\u793a\u65b9\u5f0f\u7c7b\u4f3c\u4e8e\u57fa\u672c\u7c7b\u578b\uff0c\u4f46\u662f\u5141\u8bb8nulll\uff0c\u5e76\u4e14\u5728\u8f6c\u6362\u7684\u8fc7\u7a0b\u4e2d\u4fdd\u7559null<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">FieldMask<\/td>\n<td style=\"text-align: left;\">string<\/td>\n<td style=\"text-align: left;\">\u201cf.fooBar,h\u201d<\/td>\n<td style=\"text-align: left;\">\u89c1fieldmask.proto<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">ListValue<\/td>\n<td style=\"text-align: left;\">array<\/td>\n<td style=\"text-align: left;\">[foo, bar, \u2026]<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">Value<\/td>\n<td style=\"text-align: left;\">value<\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u4efb\u610fJSON\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">NullValue<\/td>\n<td style=\"text-align: left;\">null<\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">JSON null<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u9009\u9879<\/h2>\n<p>\u5b9a\u4e49.proto\u6587\u4ef6\u65f6\u80fd\u591f\u6807\u6ce8\u4e00\u7cfb\u5217\u7684option\u3002Option\u5e76\u4e0d\u6539\u53d8\u6574\u4e2a\u6587\u4ef6\u58f0\u660e\u7684\u542b\u4e49\uff0c\u4f46\u5374\u80fd\u591f\u5f71\u54cd\u7279\u5b9a\u73af\u5883\u4e0b\u5904\u7406\u65b9\u5f0f\u3002\u5b8c\u6574\u7684\u53ef\u7528\u9009\u9879\u53ef\u4ee5\u5728<code>google\/protobuf\/descriptor.proto<\/code>\u627e\u5230\u3002<\/p>\n<p>\u4e00\u4e9b\u9009\u9879\u662f\u6587\u4ef6\u7ea7\u522b\u7684\uff0c\u610f\u5473\u7740\u5b83\u53ef\u4ee5\u4f5c\u7528\u4e8e\u6700\u5916\u8303\u56f4\uff0c\u4e0d\u5305\u542b\u5728\u4efb\u4f55\u6d88\u606f\u5185\u90e8\u3001enum\u6216\u670d\u52a1\u5b9a\u4e49\u4e2d\u3002\u4e00\u4e9b\u9009\u9879\u662f\u6d88\u606f\u7ea7\u522b\u7684\uff0c\u610f\u5473\u7740\u5b83\u53ef\u4ee5\u7528\u5728\u6d88\u606f\u5b9a\u4e49\u7684\u5185\u90e8\u3002\u5f53\u7136\u6709\u4e9b\u9009\u9879\u53ef\u4ee5\u4f5c\u7528\u5728\u57df\u3001enum\u7c7b\u578b\u3001enum\u503c\u3001\u670d\u52a1\u7c7b\u578b\u53ca\u670d\u52a1\u65b9\u6cd5\u4e2d\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u5e76\u6ca1\u6709\u4e00\u79cd\u6709\u6548\u7684\u9009\u9879\u80fd\u4f5c\u7528\u4e8e\u6240\u6709\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u5982\u4e0b\u5c31\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u9009\u9879\uff1a<\/p>\n<ul>\n<li>java_package (\u6587\u4ef6\u9009\u9879) :\u8fd9\u4e2a\u9009\u9879\u8868\u660e\u751f\u6210java\u7c7b\u6240\u5728\u7684\u5305\u3002\u5982\u679c\u5728.proto\u6587\u4ef6\u4e2d\u6ca1\u6709\u660e\u786e\u7684\u58f0\u660ejava_package\uff0c\u5c31\u91c7\u7528\u9ed8\u8ba4\u7684\u5305\u540d\u3002\u5f53\u7136\u4e86\uff0c\u9ed8\u8ba4\u65b9\u5f0f\u4ea7\u751f\u7684 java\u5305\u540d\u5e76\u4e0d\u662f\u6700\u597d\u7684\u65b9\u5f0f\uff0c\u6309\u7167\u5e94\u7528\u540d\u79f0\u5012\u5e8f\u65b9\u5f0f\u8fdb\u884c\u6392\u5e8f\u7684\u3002\u5982\u679c\u4e0d\u9700\u8981\u4ea7\u751fjava\u4ee3\u7801\uff0c\u5219\u8be5\u9009\u9879\u5c06\u4e0d\u8d77\u4efb\u4f55\u4f5c\u7528\u3002\u5982\uff1a<\/li>\n<\/ul>\n<pre><code>option java_package = &quot;com.example.foo&quot;;<\/code><\/pre>\n<ul>\n<li>java_outer_classname (\u6587\u4ef6\u9009\u9879): \u8be5\u9009\u9879\u8868\u660e\u60f3\u8981\u751f\u6210Java\u7c7b\u7684\u540d\u79f0\u3002\u5982\u679c\u5728.proto\u6587\u4ef6\u4e2d\u6ca1\u6709\u660e\u786e\u7684java_outer_classname\u5b9a\u4e49\uff0c\u751f\u6210\u7684class\u540d\u79f0\u5c06\u4f1a\u6839\u636e.proto\u6587\u4ef6\u7684\u540d\u79f0\u91c7\u7528\u9a7c\u5cf0\u5f0f\u7684\u547d\u540d\u65b9\u5f0f\u8fdb\u884c\u751f\u6210\u3002\u5982\uff08foo_bar.proto\u751f\u6210\u7684java\u7c7b\u540d\u4e3aFooBar.java\uff09,\u5982\u679c\u4e0d\u751f\u6210java\u4ee3\u7801\uff0c\u5219\u8be5\u9009\u9879\u4e0d\u8d77\u4efb\u4f55\u4f5c\u7528\u3002\u5982\uff1a<\/li>\n<\/ul>\n<pre><code>option java_outer_classname = &quot;Ponycopter&quot;;<\/code><\/pre>\n<ul>\n<li>optimize_for(\u6587\u4ef6\u9009\u9879): \u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3a SPEED, CODE_SIZE,\u6216\u8005LITE_RUNTIME\u3002\u8fd9\u4e9b\u503c\u5c06\u901a\u8fc7\u5982\u4e0b\u7684\u65b9\u5f0f\u5f71\u54cdC++\u53cajava\u4ee3\u7801\u7684\u751f\u6210\uff1a\n<ul>\n<li>SPEED (default): protocol buffer\u7f16\u8bd1\u5668\u5c06\u901a\u8fc7\u5728\u6d88\u606f\u7c7b\u578b\u4e0a\u6267\u884c\u5e8f\u5217\u5316\u3001\u8bed\u6cd5\u5206\u6790\u53ca\u5176\u4ed6\u901a\u7528\u7684\u64cd\u4f5c\u3002\u8fd9\u79cd\u4ee3\u7801\u662f\u6700\u4f18\u7684\u3002<\/li>\n<li>CODE_SIZE: protocol buffer\u7f16\u8bd1\u5668\u5c06\u4f1a\u4ea7\u751f\u6700\u5c11\u91cf\u7684\u7c7b\uff0c\u901a\u8fc7\u5171\u4eab\u6216\u57fa\u4e8e\u53cd\u5c04\u7684\u4ee3\u7801\u6765\u5b9e\u73b0\u5e8f\u5217\u5316\u3001\u8bed\u6cd5\u5206\u6790\u53ca\u5404\u79cd\u5176\u5b83\u64cd\u4f5c\u3002\u91c7\u7528\u8be5\u65b9\u5f0f\u4ea7\u751f\u7684\u4ee3\u7801\u5c06\u6bd4SPEED\u8981\u5c11\u5f97\u591a\uff0c \u4f46\u662f\u64cd\u4f5c\u8981\u76f8\u5bf9\u6162\u4e9b\u3002\u5f53\u7136\u5b9e\u73b0\u7684\u7c7b\u53ca\u5176\u5bf9\u5916\u7684API\u4e0eSPEED\u6a21\u5f0f\u90fd\u662f\u4e00\u6837\u7684\u3002\u8fd9\u79cd\u65b9\u5f0f\u7ecf\u5e38\u7528\u5728\u4e00\u4e9b\u5305\u542b\u5927\u91cf\u7684.proto\u6587\u4ef6\u800c\u4e14\u5e76\u4e0d\u76f2\u76ee\u8ffd\u6c42\u901f\u5ea6\u7684 \u5e94\u7528\u4e2d\u3002<\/li>\n<li>LITE_RUNTIME: protocol buffer\u7f16\u8bd1\u5668\u4f9d\u8d56\u4e8e\u8fd0\u884c\u65f6\u6838\u5fc3\u7c7b\u5e93\u6765\u751f\u6210\u4ee3\u7801\uff08\u5373\u91c7\u7528libprotobuf-lite \u66ff\u4ee3libprotobuf\uff09\u3002\u8fd9\u79cd\u6838\u5fc3\u7c7b\u5e93\u7531\u4e8e\u5ffd\u7565\u4e86\u4e00 \u4e9b\u63cf\u8ff0\u7b26\u53ca\u53cd\u5c04\uff0c\u8981\u6bd4\u5168\u7c7b\u5e93\u5c0f\u5f97\u591a\u3002\u8fd9\u79cd\u6a21\u5f0f\u7ecf\u5e38\u5728\u79fb\u52a8\u624b\u673a\u5e73\u53f0\u5e94\u7528\u591a\u4e00\u4e9b\u3002\u7f16\u8bd1\u5668\u91c7\u7528\u8be5\u6a21\u5f0f\u4ea7\u751f\u7684\u65b9\u6cd5\u5b9e\u73b0\u4e0eSPEED\u6a21\u5f0f\u4e0d\u76f8\u4e0a\u4e0b\uff0c\u4ea7\u751f\u7684\u7c7b\u901a\u8fc7\u5b9e\u73b0 MessageLite\u63a5\u53e3\uff0c\u4f46\u5b83\u4ec5\u4ec5\u662fMessager\u63a5\u53e3\u7684\u4e00\u4e2a\u5b50\u96c6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code>option optimize_for = CODE_SIZE;<\/code><\/pre>\n<ul>\n<li>cc_enable_arenas(\u6587\u4ef6\u9009\u9879):\u5bf9\u4e8eC++\u4ea7\u751f\u7684\u4ee3\u7801\u542f\u7528arena allocation<\/li>\n<li>objc_class_prefix(\u6587\u4ef6\u9009\u9879):\u8bbe\u7f6eObjective-C\u7c7b\u7684\u524d\u7f00\uff0c\u6dfb\u52a0\u5230\u6240\u6709Objective-C\u4ece\u6b64.proto\u6587\u4ef6\u4ea7\u751f\u7684\u7c7b\u548c\u679a\u4e3e\u7c7b\u578b\u3002\u6ca1\u6709\u9ed8\u8ba4\u503c\uff0c\u6240\u4f7f\u7528\u7684\u524d\u7f00\u5e94\u8be5\u662f\u82f9\u679c\u63a8\u8350\u76843-5\u4e2a\u5927\u5199\u5b57\u7b26\uff0c\u6ce8\u610f2\u4e2a\u5b57\u8282\u7684\u524d\u7f00\u662f\u82f9\u679c\u6240\u4fdd\u7559\u7684\u3002<\/li>\n<li>deprecated(\u5b57\u6bb5\u9009\u9879):\u5982\u679c\u8bbe\u7f6e\u4e3atrue\u5219\u8868\u793a\u8be5\u5b57\u6bb5\u5df2\u7ecf\u88ab\u5e9f\u5f03\uff0c\u5e76\u4e14\u4e0d\u5e94\u8be5\u5728\u65b0\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002\u5728\u5927\u591a\u6570\u8bed\u8a00\u4e2d\u6ca1\u6709\u5b9e\u9645\u7684\u610f\u4e49\u3002\u5728java\u4e2d\uff0c\u8fd9\u56de\u53d8\u6210@Deprecated\u6ce8\u91ca\uff0c\u5728\u672a\u6765\uff0c\u5176\u4ed6\u8bed\u8a00\u7684\u4ee3\u7801\u751f\u6210\u5668\u4e5f\u8bb8\u4f1a\u5728\u5b57\u6807\u8bc6\u7b26\u4e2d\u4ea7\u751f\u5e9f\u5f03\u6ce8\u91ca\uff0c\u5e9f\u5f03\u6ce8\u91ca\u4f1a\u5728\u7f16\u8bd1\u5668\u5c1d\u8bd5\u4f7f\u7528\u8be5\u5b57\u6bb5\u65f6\u53d1\u51fa\u8b66\u544a\u3002\u5982\u679c\u5b57\u6bb5\u6ca1\u6709\u88ab\u4f7f\u7528\u4f60\u4e5f\u4e0d\u5e0c\u671b\u6709\u65b0\u7528\u6237\u4f7f\u7528\u5b83\uff0c\u5c1d\u8bd5\u4f7f\u7528\u4fdd\u7559\u8bed\u53e5\u66ff\u6362\u5b57\u6bb5\u58f0\u660e\u3002<\/li>\n<\/ul>\n<pre><code>int32 old_field = 6 [deprecated=true];<\/code><\/pre>\n<h3>\u81ea\u5b9a\u4e49\u9009\u9879<\/h3>\n<p>ProtocolBuffers\u5141\u8bb8\u81ea\u5b9a\u4e49\u5e76\u4f7f\u7528\u9009\u9879\u3002\u8be5\u529f\u80fd\u5e94\u8be5\u5c5e\u4e8e\u4e00\u4e2a\u9ad8\u7ea7\u7279\u6027\uff0c\u5bf9\u4e8e\u5927\u90e8\u5206\u4eba\u662f\u7528\u4e0d\u5230\u7684\u3002\u5982\u679c\u4f60\u7684\u786e\u5e0c\u671b\u521b\u5efa\u81ea\u5df1\u7684\u9009\u9879\uff0c\u8bf7\u53c2\u770b Proto2 Language Guide\u3002\u6ce8\u610f\u521b\u5efa\u81ea\u5b9a\u4e49\u9009\u9879\u4f7f\u7528\u4e86\u62d3\u5c55\uff0c\u62d3\u5c55\u53ea\u5728proto3\u4e2d\u53ef\u7528\u3002<\/p>\n<h2>\u751f\u6210\u8bbf\u95ee\u7c7b<\/h2>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u597d\u7684.proto\u6587\u4ef6\u6765\u751f\u6210Java,Python,C++, Ruby, JavaNano, Objective-C,\u6216\u8005C# \u4ee3\u7801\uff0c\u9700\u8981\u57fa\u4e8e.proto\u6587\u4ef6\u8fd0\u884cprotocol buffer\u7f16\u8bd1\u5668protoc\u3002\u5982\u679c\u4f60\u6ca1\u6709\u5b89\u88c5\u7f16\u8bd1\u5668\uff0c\u4e0b\u8f7d\u5b89\u88c5\u5305\u5e76\u9075\u7167README\u5b89\u88c5\u3002\u5bf9\u4e8eGo,\u4f60\u8fd8\u9700\u8981\u5b89\u88c5\u4e00\u4e2a\u7279\u6b8a\u7684\u4ee3\u7801\u751f\u6210\u5668\u63d2\u4ef6\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7GitHub\u4e0a\u7684protobuf\u5e93\u627e\u5230\u5b89\u88c5\u8fc7\u7a0b<\/p>\n<p>\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u8c03\u7528protocol\u7f16\u8bd1\u5668\uff1a<\/p>\n<pre><code>protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR --go_out=DST_DIR --ruby_out=DST_DIR --javanano_out=DST_DIR --objc_out=DST_DIR --csharp_out=DST_DIR path\/to\/file.proto<\/code><\/pre>\n<ul>\n<li>IMPORT_PATH\u58f0\u660e\u4e86\u4e00\u4e2a.proto\u6587\u4ef6\u6240\u5728\u7684\u89e3\u6790import\u5177\u4f53\u76ee\u5f55\u3002\u5982\u679c\u5ffd\u7565\u8be5\u503c\uff0c\u5219\u4f7f\u7528\u5f53\u524d\u76ee\u5f55\u3002\u5982\u679c\u6709\u591a\u4e2a\u76ee\u5f55\u5219\u53ef\u4ee5\u591a\u6b21\u8c03\u7528--proto_path\uff0c\u5b83\u4eec\u5c06\u4f1a\u987a\u5e8f\u7684\u88ab\u8bbf\u95ee\u5e76\u6267\u884c\u5bfc\u5165\u3002-I=IMPORT_PATH\u662f--proto_path\u7684\u7b80\u5316\u5f62\u5f0f\u3002<\/li>\n<li>\u5f53\u7136\u4e5f\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u6216\u591a\u4e2a\u8f93\u51fa\u8def\u5f84\uff1a\n<ul>\n<li>--cpp_out \u5728\u76ee\u6807\u76ee\u5f55DST_DIR\u4e2d\u4ea7\u751fC++\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/cpp-generated\">C++\u4ee3\u7801\u751f\u6210\u53c2\u8003<\/a>\u4e2d\u67e5\u770b\u66f4\u591a\u3002<\/li>\n<li>--java_out \u5728\u76ee\u6807\u76ee\u5f55DST_DIR\u4e2d\u4ea7\u751fJava\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728 <a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/java-generated\">Java\u4ee3\u7801\u751f\u6210\u53c2\u8003<\/a>\u4e2d\u67e5\u770b\u66f4\u591a\u3002<\/li>\n<li>--python_out \u5728\u76ee\u6807\u76ee\u5f55 DST_DIR \u4e2d\u4ea7\u751fPython\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/python-generated\">Python\u4ee3\u7801\u751f\u6210\u53c2\u8003<\/a>\u4e2d\u67e5\u770b\u66f4\u591a\u3002<\/li>\n<li>--go_out \u5728\u76ee\u6807\u76ee\u5f55 DST_DIR \u4e2d\u4ea7\u751fGo\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/go-generated\">GO\u4ee3\u7801\u751f\u6210\u53c2\u8003<\/a>\u4e2d\u67e5\u770b\u66f4\u591a\u3002<\/li>\n<li>--ruby_out\u5728\u76ee\u6807\u76ee\u5f55 DST_DIR \u4e2d\u4ea7\u751fRuby\u4ee3\u7801\uff0c\u53c2\u8003\u6b63\u5728\u5236\u4f5c\u4e2d\u3002<\/li>\n<li>--javanano_out\u5728\u76ee\u6807\u76ee\u5f55DST_DIR\u4e2d\u751f\u6210JavaNano\uff0cJavaNano\u4ee3\u7801\u751f\u6210\u5668\u6709\u4e00\u7cfb\u5217\u7684\u9009\u9879\u7528\u4e8e\u5b9a\u5236\u81ea\u5b9a\u4e49\u751f\u6210\u5668\u7684\u8f93\u51fa\uff1a\u4f60\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u5668\u7684<a href=\"https:\/\/github.com\/google\/protobuf\/tree\/master\/javanano\">README<\/a>\u67e5\u627e\u66f4\u591a\u4fe1\u606f\uff0cJavaNano\u53c2\u8003\u6b63\u5728\u5236\u4f5c\u4e2d\u3002<\/li>\n<li>--objc_out\u5728\u76ee\u6807\u76ee\u5f55DST_DIR\u4e2d\u4ea7\u751fObject\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/objective-c-generated\">Objective-C\u4ee3\u7801\u751f\u6210\u53c2\u8003<\/a>\u4e2d\u67e5\u770b\u66f4\u591a\u3002<\/li>\n<li>--csharp_out\u5728\u76ee\u6807\u76ee\u5f55DST_DIR\u4e2d\u4ea7\u751fObject\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/csharp-generated\">C#\u4ee3\u7801\u751f\u6210\u53c2\u8003<\/a>\u4e2d\u67e5\u770b\u66f4\u591a\u3002<\/li>\n<li>--php_out\u5728\u76ee\u6807\u76ee\u5f55DST_DIR\u4e2d\u4ea7\u751fObject\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728<a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/php-generated\">PHP\u4ee3\u7801\u751f\u6210\u53c2\u8003<\/a>\u4e2d\u67e5\u770b\u66f4\u591a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4f5c\u4e3a\u4e00\u4e2a\u65b9\u4fbf\u7684\u62d3\u5c55\uff0c\u5982\u679cDST_DIR\u4ee5.zip\u6216\u8005.jar\u7ed3\u5c3e\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u8f93\u51fa\u5199\u5230\u4e00\u4e2aZIP\u683c\u5f0f\u6587\u4ef6\u6216\u8005\u7b26\u5408JAR\u6807\u51c6\u7684.jar\u6587\u4ef6\u4e2d\u3002\u6ce8\u610f\u5982\u679c\u8f93\u51fa\u5df2\u7ecf\u5b58\u5728\u5219\u4f1a\u88ab\u8986\u76d6\uff0c\u7f16\u8bd1\u5668\u8fd8\u6ca1\u6709\u667a\u80fd\u5230\u53ef\u4ee5\u8ffd\u52a0\u6587\u4ef6\u3002<\/p>\n<ul>\n<li>\u4f60\u5fc5\u987b\u63d0\u8bae\u4e00\u4e2a\u6216\u591a\u4e2a.proto\u6587\u4ef6\u4f5c\u4e3a\u8f93\u5165\uff0c\u591a\u4e2a.proto\u6587\u4ef6\u53ef\u4ee5\u53ea\u6307\u5b9a\u4e00\u6b21\u3002\u867d\u7136\u6587\u4ef6\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5f53\u524d\u76ee\u5f55\u7684\uff0c\u6bcf\u4e2a\u6587\u4ef6\u5fc5\u987b\u4f4d\u4e8e\u5176IMPORT_PATH\u4e0b\uff0c\u4ee5\u4fbf\u6bcf\u4e2a\u6587\u4ef6\u53ef\u4ee5\u786e\u5b9a\u5176\u89c4\u8303\u7684\u540d\u79f0\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d\u6211\u7ffb\u8bd1\u4e86 Protobuf2 \u8bed\u6cd5\u6307\u5357\uff0c\u73b0\u5728 \u5343\u5ff5\u98de\u7fbd\u628aprotobuf3\u7684\u8bed\u6cd5\u6307\u5357\u4e5f\u7ffb\u8bd1\u4e86\uff0c\u6211\u4e5f\u8f6c\u8f7d\u4e00\u4e0b\uff0c\u8bfb\u8005\u53ef\u4ee5\u6709\u4e2a\u53c2\u8003\u3002 \u8bd1\u6587   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[189],"tags":[272],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9298"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9298"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9298\/revisions"}],"predecessor-version":[{"id":9299,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9298\/revisions\/9299"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9298"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}