{"id":207563,"date":"2025-07-08T10:53:19","date_gmt":"2025-07-08T02:53:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207563\/"},"modified":"2025-07-08T10:53:19","modified_gmt":"2025-07-08T02:53:19","slug":"%e5%9c%a8%e4%b8%8d%e4%b8%8e%e5%8f%82%e6%95%b0%e7%b1%bb%e5%9e%8b%e7%b4%a7%e5%af%86%e8%80%a6%e5%90%88%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%e5%b0%86%e5%8f%82%e6%95%b0%e4%bc%a0%e9%80%92%e7%bb%99%e5%87%bd","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207563\/","title":{"rendered":"\u5728\u4e0d\u4e0e\u53c2\u6570\u7c7b\u578b\u7d27\u5bc6\u8026\u5408\u7684\u60c5\u51b5\u4e0b\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\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>\u5728\u4e0d\u4e0e\u53c2\u6570\u7c7b\u578b\u7d27\u5bc6\u8026\u5408\u7684\u60c5\u51b5\u4e0b\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 17:12: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>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728\u4e0d\u4e0e\u53c2\u6570\u7c7b\u578b\u7d27\u5bc6\u8026\u5408\u7684\u60c5\u51b5\u4e0b\u5c06\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e24\u4e2a\u7ed3\u6784\uff0c\u6bcf\u4e2a\u7ed3\u6784\u90fd\u6709\u6574\u6570\u5b57\u6bb5 a\u3001b\u3002\u73b0\u5728\u6211\u60f3\u7f16\u5199\u4e00\u4e2a\u540d\u4e3a sum \u7684\u51fd\u6570\uff0c\u5176\u7ed3\u679c\u4e3a a+b<\/p>\n<pre>type Type1 struct {\n    a int64\n    b int64\n}\n\ntype Type2 struct {\n    a int64\n    b int64\n}\n\nfunc sum(details Type1) int64 {\n    return details.a + details.b\n}\n\nfunc sum2(details Type2) int64 {\n    return details.a + details.b\n}\n\nfunc main() {\n    type1Obj: = Type1 {}\n    type2Obj: = Type2 {}\n    sum(type1Obj)\n    sum2(type2Obj)\n}\n<\/pre>\n<p>\u5b9e\u9645\uff1a\u6211\u6b63\u5728\u4e3a\u76f8\u540c\u7684\u884c\u4e3a\u521b\u5efa\u4e24\u4e2a\u51fd\u6570\uff0c\u53ea\u662f\u56e0\u4e3a\u7c7b\u578b\u3002 <\/p>\n<p>\u9884\u671f\uff1a\u6211\u9700\u8981\u5728\u5355\u4e2a\u51fd\u6570\u7684\u5e2e\u52a9\u4e0b\u89e3\u51b3\u7528\u4f8b\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u662f\u754c\u9762\u7684\u7ecf\u5178\u7528\u6cd5\u3002<\/p>\n<pre>\/\/ there is some interface that expresses the behaviors you need\ntype summable interface {\n    a() int64\n    b() int64\n}\n\n\/\/ and it can be summed\nfunc sum(s summable) int64 {\n    return s.a() + s.b()\n}<\/pre>\n<p>\u7136\u540e\u4f60\u53ef\u4ee5\u5c06\u5404\u79cd\u7c7b\u578b\u7b26\u5408summable\uff1a<\/p>\n<pre>type type1 struct {\n    a int64\n    b int64\n}\n\n\/\/ type1 conforms to summable\nfunc (t type1) a() int64 { \n    return t.a \n}\n\nfunc (t type1) b() int64 {\n    return t.b\n}\n\ntype type2 struct {\n    a int64\n    b int64\n}\n\n\/\/ and so does type2\nfunc (t type2) a() int64 { \n    return t.a \n}\n\nfunc (t type2) b() int64 {\n    return t.b\n}<\/pre>\n<p>\u7136\u540e\u60a8\u53ef\u4ee5\u5bf9\u4efb\u4f55 summable \u7c7b\u578b\u6c42\u548c\uff1a<\/p>\n<pre>func main() {\n    type1Obj := Type1 {}\n    type2Obj := Type2 {}\n    println(sum(type1Obj))\n    println(sum(type2Obj))\n}<\/pre>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\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-207563","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207563","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=207563"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207563\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}