{"id":207473,"date":"2025-07-08T13:35:15","date_gmt":"2025-07-08T05:35:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207473\/"},"modified":"2025-07-08T13:35:15","modified_gmt":"2025-07-08T05:35:15","slug":"%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e4%bd%9c%e4%b8%ba%e6%8e%a5%e5%8f%a3%e4%bc%a0%e9%80%92%e7%9a%84%e5%af%b9%e8%b1%a1%e7%9a%84%e5%b5%8c%e5%85%a5%e7%bb%93%e6%9e%84%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207473\/","title":{"rendered":"\u5982\u4f55\u8c03\u7528\u4f5c\u4e3a\u63a5\u53e3\u4f20\u9012\u7684\u5bf9\u8c61\u7684\u5d4c\u5165\u7ed3\u6784\u65b9\u6cd5\uff1f"},"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>\u5982\u4f55\u8c03\u7528\u4f5c\u4e3a\u63a5\u53e3\u4f20\u9012\u7684\u5bf9\u8c61\u7684\u5d4c\u5165\u7ed3\u6784\u65b9\u6cd5\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 21:03:27<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u8c03\u7528\u4f5c\u4e3a\u63a5\u53e3\u4f20\u9012\u7684\u5bf9\u8c61\u7684\u5d4c\u5165\u7ed3\u6784\u65b9\u6cd5\uff1f\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u7684\u573a\u666f\u8981\u6c42\u7528\u6237\u5d4c\u5165\u4e00\u4e2a\u57fa\u672c\u7ed3\u6784\u5e76\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53e3\u3002<\/p>\n<p> \u7136\u540e\uff0c\u5e94\u8be5\u5c06\u8be5\u7ed3\u6784\u7684\u5b9e\u4f8b\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u8be5\u51fd\u6570\u9700\u8981\u8c03\u7528\u57fa\u672c\u7ed3\u6784\u7684\u65b9\u6cd5\u3002\u8fd9\u5931\u8d25\u4e86<\/p>\n<pre>\/\/ Given base struct and interface\n\ntype Interface interface {\n    Do()\n}\n\ntype BaseStruct struct {\n    i int\n    s string\n}\n\nfunc (*b BaseStruct) Stuff() {}\n\n\/\/ The user needs to create a struct that embeds BaseStruct and to implement Interface:\ntype CustomStruct struct {\n    *BaseStruct\n}\n\nfunc (*c CustomStruct) Do() {}\n\n\/\/ The user now instantiates the struct and needs to call a function\n\ninst := CustomStruct{}\nSomePackageFun(&amp;inst)\n\n\/\/ The below function receives the custom struct, and must call the base struct's method, but this fails\n\nfunc SomePackageFunc(i Interface) {\n    \/\/ declaring the function with Interface works but I can't call the methods of BaseStruct\n    i.Stuff() \/\/ not recognized by the compiler\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u80fd\u591f\u8c03\u7528\u63a5\u53e3\u7c7b\u578b\u7684\u53d8\u91cf\u4e0a\u7684\u65b9\u6cd5\uff0c\u5219\u5e94\u8be5\u5c06\u8be5\u65b9\u6cd5\u6dfb\u52a0\u5230\u63a5\u53e3\u4e2d\u3002\u6765\u81ea\u5d4c\u5165\u5f0f\u7ed3\u6784\u7684\u65b9\u6cd5\u51fa\u4e8e\u6ee1\u8db3\u63a5\u53e3\u7684\u76ee\u7684\u800c\u8ba1\u6570\u3002\u8981\u8c03\u7528\u4e0d\u5c5e\u4e8e\u63a5\u53e3\u4e00\u90e8\u5206\u7684\u4efb\u4f55\u5185\u5bb9\uff0c\u60a8\u5fc5\u987b\u65ad\u8a00\u5230\u5177\u4f53\u7c7b\u578b\uff08\u6216\u5177\u6709\u8be5\u65b9\u6cd5\u7684\u4e0d\u540c\u63a5\u53e3\u7c7b\u578b\uff09\uff0c\u8fd9\u8fdd\u80cc\u4e86\u8fd9\u4e00\u70b9\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u8c03\u7528\u4f5c\u4e3a\u63a5\u53e3\u4f20\u9012\u7684\u5bf9\u8c61\u7684\u5d4c\u5165\u7ed3\u6784\u65b9\u6cd5\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/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-207473","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207473","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=207473"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207473\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}