{"id":207311,"date":"2025-07-08T09:02:53","date_gmt":"2025-07-08T01:02:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207311\/"},"modified":"2025-07-08T09:02:53","modified_gmt":"2025-07-08T01:02:53","slug":"%e8%8e%b7%e5%8f%96-golang-%e7%9a%84%e5%8d%8f%e8%ae%ae%e7%bc%93%e5%86%b2%e5%8c%ba%e9%80%89%e9%a1%b9%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207311\/","title":{"rendered":"\u83b7\u53d6 golang \u7684\u534f\u8bae\u7f13\u51b2\u533a\u9009\u9879\u4fe1\u606f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u83b7\u53d6 golang \u7684\u534f\u8bae\u7f13\u51b2\u533a\u9009\u9879\u4fe1\u606f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 17:24:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u83b7\u53d6 golang \u7684\u534f\u8bae\u7f13\u51b2\u533a\u9009\u9879\u4fe1\u606f\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>protocol buffer\u5b9a\u4e49\u5982\u4e0b\uff0c<code>testmessage<\/code>\u6709\u4e24\u4e2a\u9009\u9879<code>msg_option_a<\/code>\u548c<code>msg_option_b<\/code>\uff1a<\/p>\n<pre>syntax = \"proto3\";\npackage grpctest;\n\noption go_package = \"pb\";\n\nimport \"google\/protobuf\/descriptor.proto\";\n\nextend google.protobuf.messageoptions {\n  int32 msg_option_a = 50011;\n  int32 msg_option_b = 50012;\n}\n\nmessage testmessage {\n  option (msg_option_a) = 22;\n  option (msg_option_b) = 33;\n  string name = 1;\n}<\/pre>\n<p>\u6211\u60f3\u8bfb\u53d6\u4e24\u4e2a\u9009\u9879\u7684\u5b9a\u4e49\u503c\uff1a<\/p>\n<pre>var msg *pb.testmessage\n_, md := descriptor.formessage(msg)\noptions := md.getoptions()\n\nfmt.println(options.string()) \/\/ --&gt; [grpcapi.msg_option_a]:22 [grpcapi.msg_option_b]:33\nfmt.println(len(options.getuninterpretedoption())) \/\/ --&gt; 0<\/pre>\n<p>\u6253\u5370\u6574\u4e2a<code>messageoptions<\/code>\u65f6\u53ef\u4ee5\u83b7\u53d6\u6240\u6709\u9009\u9879\u4fe1\u606f\uff0c<code>getuninterpretedoption()<\/code>\u8fd4\u56de\u4e00\u4e2a\u9009\u9879\u5b9a\u4e49\u6570\u7ec4\uff0c\u4f46\u5b83\u7684\u957f\u5ea6\u4e3a\u96f6\u3002 <\/p>\n<p>\u4ee5\u4e0b\u662f <code>uninterpretedoption<\/code> \u7c7b\u578b\u7684\u6ce8\u91ca\uff0c\u4f46\u6211\u65e0\u6cd5\u7406\u89e3\u5b83\u7684\u542b\u4e49\uff0c\u4e5f\u6ca1\u6709\u627e\u5230\u6709\u5173 <code>descriptorpool<\/code> \u7684\u4efb\u4f55\u4fe1\u606f\uff1a<\/p>\n<pre>\/\/ A message representing a option the parser does not recognize. This only\n\/\/ appears in options protos created by the compiler::Parser class.\n\/\/ DescriptorPool resolves these when building Descriptor objects. Therefore,\n\/\/ options protos in descriptor objects (e.g. returned by Descriptor::options(),\n\/\/ or produced by Descriptor::CopyTo()) will never have UninterpretedOptions\n\/\/ in them.<\/pre>\n<p><strong>\u6211\u60f3\u5f97\u5230\u4e00\u4e2a\u5177\u4f53\u7684\u671f\u6743\u4ef7\u503c<\/strong>\uff0c\u4f46\u73b0\u5728\u6ca1\u6709\u60f3\u6cd5\u3002 <\/p>\n<p>\u8bf7\u5e2e\u5fd9\uff01\u8c22\u8c22\uff01 <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4f7f\u7528<code>proto.getextension<\/code>\u83b7\u53d6\u9009\u9879\u503c\uff1a<\/p>\n<pre>var msg *pb.TestMessage\n_, md := descriptor.ForMessage(msg)\noptions := md.GetOptions()\n\nfmt.Println(options.String()) \/\/ --&gt; [grpcapi.msg_option_a]:22 [grpcapi.msg_option_b]:33\nfmt.Println(len(options.GetUninterpretedOption())) \/\/ --&gt; 0\n\na, _ := proto.GetExtension(options, pb.E_MsgOptionA)\nfmt.Println(*a.(*int32)) \/\/ --&gt; 22\n\nb, _ := proto.GetExtension(options, pb.E_MsgOptionB)\nfmt.Println(*b.(*int32)) \/\/ --&gt; 33<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u83b7\u53d6 golang \u7684\u534f\u8bae\u7f13\u51b2\u533a\u9009\u9879\u4fe1\u606f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207311","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207311"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207311\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}