{"id":208271,"date":"2025-07-08T12:23:29","date_gmt":"2025-07-08T04:23:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208271\/"},"modified":"2025-07-08T12:23:29","modified_gmt":"2025-07-08T04:23:29","slug":"golang-%e6%b3%9b%e5%9e%8b-%e7%ae%80%e5%8d%95%e7%94%a8%e4%be%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208271\/","title":{"rendered":"Golang \u6cdb\u578b &#8211; \u7b80\u5355\u7528\u4f8b"},"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>Golang \u6cdb\u578b &#8211; \u7b80\u5355\u7528\u4f8b<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 08:54:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang \u6cdb\u578b &#8211; \u7b80\u5355\u7528\u4f8b\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5047\u8bbe\u6211\u6709 3 \u4e2a\u7ed3\u6784\uff1a<\/p>\n<pre>type a struct{\n   foo map[string]string\n}\n\ntype b struct{\n   foo map[string]string\n}\n\ntype c struct{\n   foo map[string]string\n}<\/pre>\n<p>\u7136\u540e\u6211\u60f3\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u8fd9\u4e9b\u7ed3\u6784\u7684\u51fd\u6570\uff1a<\/p>\n<pre>func handlefoo (){\n\n}<\/pre>\n<p>\u6709\u4ec0\u4e48\u529e\u6cd5\u53ef\u4ee5\u7528 golang \u6765\u505a\u5230\u8fd9\u4e00\u70b9\u5417\uff1f\u50cf\u8fd9\u6837\u7684\u4e1c\u897f\uff1a<\/p>\n<pre>type abc = a | b | c\n\nfunc handlefoo(v abc){\n   x: = v.foo[\"barbie\"] \/\/ this would be nice!\n}<\/pre>\n<p>\u597d\u7684\uff0c\u8ba9\u6211\u4eec\u5c1d\u8bd5\u4e00\u4e0b\u754c\u9762\uff1a<\/p>\n<pre>type FML interface {\n  Bar() string\n}\n\nfunc handleFoo(v FML){\n   z := v.Bar() \/\/ this will compile\n   x: = v.Foo[\"barbie\"] \/\/ this won't compile - can't access properties like Foo from v\n}<\/pre>\n<p>\u5728\u4e00\u79cd\u9f13\u52b1\/\u5f3a\u5236\u7ec4\u5408\u7684\u8bed\u8a00\u4e2d\uff0c\u6211\u65e0\u6cd5\u7406\u89e3\u4e3a\u4ec0\u4e48\u4f60\u4e0d\u80fd\u8bbf\u95ee\u50cf foo \u8fd9\u6837\u7684\u5c5e\u6027\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\u63a5\u53e3\uff0c\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5 <code>getfoo<\/code> \u6765\u83b7\u53d6\u6bcf\u4e2a\u7ed3\u6784\u4f53\u7684 foo\u3002<\/p>\n<pre>type A struct{\n    Foo map[string]string\n}\n\nfunc(a *A) GetFoo() map[string]string {\n    return a.Foo\n}\n\ntype B struct{\n    Foo map[string]string\n}\n\nfunc(b *B) GetFoo() map[string]string {\n    return b.Foo\n}\n\ntype C struct{\n    Foo map[string]string\n}\n\nfunc(c *C) GetFoo() map[string]string {\n    return c.Foo\n}\n\ntype ABC interface {\n    GetFoo() map[string][string]\n}\n\nfunc handleFoo (v ABC){\n    foo := v.GetFoo()\n    x:=foo[\"barbie\"]\n}<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-208271","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208271","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=208271"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208271\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}