{"id":207391,"date":"2025-07-08T09:48:58","date_gmt":"2025-07-08T01:48:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207391\/"},"modified":"2025-07-08T09:48:58","modified_gmt":"2025-07-08T01:48:58","slug":"%e6%98%af%e5%90%a6%e5%8f%af%e4%bb%a5%e5%9c%a8-go-%e4%b8%ad%e5%ae%9a%e4%b9%89%e5%8c%bf%e5%90%8d%e6%8e%a5%e5%8f%a3%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207391\/","title":{"rendered":"\u662f\u5426\u53ef\u4ee5\u5728 Go \u4e2d\u5b9a\u4e49\u533f\u540d\u63a5\u53e3\u5b9e\u73b0\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>\u662f\u5426\u53ef\u4ee5\u5728 Go \u4e2d\u5b9a\u4e49\u533f\u540d\u63a5\u53e3\u5b9e\u73b0\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 10:36:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u662f\u5426\u53ef\u4ee5\u5728 Go \u4e2d\u5b9a\u4e49\u533f\u540d\u63a5\u53e3\u5b9e\u73b0\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u8003\u8651\u4e00\u4e9b\u7ed9\u5b9a\u7684\u63a5\u53e3\u548c\u4e00\u4e2a\u4f7f\u7528\u5b83\u7684\u865a\u6784\u5e93\u7684\u51fd\u6570<\/p>\n<pre>\/\/ binary and ternary operation on ints\ntype numop interface {\n    binary(int, int) int\n    ternary(int, int, int) int\n}\n\nfunc randomnumop(op numop) {\n    var (\n        a = rand.intn(100) - 50\n        b = rand.intn(100) - 50\n        c = rand.intn(100) - 50\n    )\n    fmt.printf(\"%d &lt;op&gt; %d = %d\\n\", a, b, op.binary(a,b))\n    fmt.printf(\"%d &lt;op&gt; %d &lt;op&gt; %d = %d\\n\", a, b, c, op.ternary(a,b,c))\n}<\/pre>\n<p>\u5b9e\u73b0\u8be5\u63a5\u53e3\u7684\u53ef\u80fd\u7c7b\u578b\u53ef\u80fd\u662f<\/p>\n<pre>\/\/ myadd defines additions on 2 or 3 int variables\ntype myadd struct {}\nfunc (myadd) binary(a, b int) int {return a + b }\nfunc (myadd) ternary(a, b, c int) int {return a + b + c }<\/pre>\n<p>\u6211\u6b63\u5728\u5904\u7406\u8bb8\u591a\u4e0d\u540c\u7684\u63a5\u53e3\uff0c\u5b9a\u4e49\u4e00\u4e9b\u51fd\u6570\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u9700\u8981\u4f7f\u7528\u4e3b\u8981\u4f5c\u4e3a <code>nop<\/code> \u7c7b\u4f3c\u64cd\u4f5c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u4e0d\u4f9d\u8d56\u4e8e\u4efb\u4f55 struct <em>member<\/em> \u5e76\u4e14\u4ec5\u4f7f\u7528\u5728\u9879\u76ee\u4e2d\u7684\u5355\u4e00\u4f4d\u7f6e\uff08\u4e0d\u9700\u8981\u53ef\u91cd\u7528\u6027\uff09\u3002<\/p>\n<p>go \u4e2d\u662f\u5426\u6709\u4e00\u79cd\u66f4\u7b80\u5355\uff08\u4e0d\u592a\u5197\u957f\uff09\u7684\u65b9\u6cd5\u6765\u4f7f\u7528\u533f\u540d\u51fd\u6570\u5b9a\u4e49\uff08\u6700\u597d\uff09\u63a5\u53e3\u7684\u533f\u540d\u5b9e\u73b0\uff0c\u5c31\u50cf\uff08\u4f2a\u4ee3\u7801\uff0c\u6211\u77e5\u9053\u5b83\u4e0d\u80fd\u90a3\u6837\u5de5\u4f5c\uff09\uff1a<\/p>\n<pre>RandomNumOp({\n   Binary: func(a,b int) int { return a+b},\n   Ternary: func(a,b,c int) int {return a+b+c},\n})<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<h2>\u5982\u679c\u5b9e\u65bd\u5fc5\u987b\u6709\u6548<\/h2>\n<p>\u5982\u679c\u5b9e\u73b0\u63a5\u53e3\u7684\u503c\u5fc5\u987b\u6709\u6548\uff08\u4f8b\u5982\uff0c\u5176\u65b9\u6cd5\u5fc5\u987b\u53ef\u8c03\u7528\u800c\u4e0d\u4f1a\u51fa\u73b0\u6050\u614c\uff09\uff0c\u90a3\u4e48\u60a8\u5c31\u4e0d\u80fd\u8fd9\u6837\u505a\u3002<\/p>\n<p>\u65b9\u6cd5\u58f0\u660e\u5fc5\u987b\u4f4d\u4e8e\u9876\u5c42\uff08\u6587\u4ef6\u7ea7\u522b\uff09\u3002\u8981\u5b9e\u73b0\u4e00\u4e2a\u5177\u6709\u8d85\u8fc7 0 \u4e2a\u65b9\u6cd5\u7684\u63a5\u53e3\uff0c\u9700\u8981\u5728\u67d0\u5904\u58f0\u660e\u65b9\u6cd5\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7ed3\u6784\u4f53\u5e76\u5d4c\u5165\u73b0\u6709\u7684\u5b9e\u73b0\uff0c\u4f46\u8bdd\u53c8\u8bf4\u56de\u6765\uff0c\u5b83\u9700\u8981\u5df2\u7ecf\u6709\u4e00\u4e2a\u73b0\u6709\u7684\u5b9e\u73b0\uff0c\u5176\u65b9\u6cd5\u5fc5\u987b\u5df2\u5728\u201c\u67d0\u5904\u201d\u5b9a\u4e49\uff1a\u5728\u6587\u4ef6\u7ea7\u522b\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u4e00\u4e2a\u201c\u865a\u62df\u201d\u4f46\u53ef\u884c\u7684\u5b9e\u73b0\uff0c\u4ed6\u4eec\u53ef\u4ee5\u4f7f\u7528\/\u4f20\u9012<em>\u4efb\u4f55<\/em>\u5b9e\u73b0\uff0c\u4f8b\u5982<code>myadd<\/code> \u7c7b\u578b\u7684\u503c\u3002\u5982\u679c\u60a8\u60f3\u5f3a\u8c03\u5b9e\u73b0\u5e76\u4e0d\u91cd\u8981\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u5b9e\u73b0\uff0c\u5176\u540d\u79f0\u8868\u660e\uff1a<\/p>\n<pre>type dummyop struct{}\n\nfunc (dummyop) binary(_, _ int) int     { return 0 }\nfunc (dummyop) ternary(_, _, _ int) int { return 0 }<\/pre>\n<p>\u5982\u679c\u60a8\u9700\u8981\u52a8\u6001\u5730\u63d0\u4f9b<em>\u67d0\u4e9b<\/em>\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u59d4\u6258\u8005\u7ed3\u6784\u7c7b\u578b\u6765\u4fdd\u5b58\u8fd9\u4e9b\u65b9\u6cd5\u7684\u51fd\u6570\uff0c\u5e76\u4e14\u5b9e\u9645\u7684\u65b9\u6cd5\u68c0\u67e5\u662f\u5426\u8bbe\u7f6e\u4e86\u76f8\u5e94\u7684\u51fd\u6570\uff0c\u5728\u8c03\u7528\u54ea\u79cd\u60c5\u51b5\uff0c\u5426\u5219\u4ec0\u4e48\u4e5f\u4e0d\u505a\u3002<\/p>\n<p>\u5b83\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>type customop struct {\n    binary  func(int, int) int\n    ternary func(int, int, int) int\n}\n\nfunc (cop customop) binary(a, b int) int {\n    if cop.binary != nil {\n        return cop.binary(a, b)\n    }\n    return 0\n}\n\nfunc (cop customop) ternary(a, b, c int) int {\n    if cop.ternary != nil {\n        return cop.ternary(a, b, c)\n    }\n    return 0\n}<\/pre>\n<p>\u4f7f\u7528\u5b83\u65f6\uff0c\u60a8\u53ef\u4ee5\u81ea\u7531\u5730\u4ec5\u63d0\u4f9b\u51fd\u6570\u7684\u5b50\u96c6\uff0c\u5176\u4f59\u90e8\u5206\u5c06\u662f\u65e0\u64cd\u4f5c\uff1a<\/p>\n<pre>randomnumop(customop{\n    binary: func(a, b int) int { return a + b },\n})<\/pre>\n<h2>\u5982\u679c\u4e0d\u9700\u8981\u5b9e\u65bd\u5373\u53ef\u5de5\u4f5c<\/h2>\n<p>\u5982\u679c\u60a8\u53ea\u9700\u8981\u4e00\u4e2a\u5b9e\u73b0\u63a5\u53e3\u7684\u503c\uff0c\u4f46\u4e0d\u8981\u6c42\u5176\u65b9\u6cd5\u662f\u201c\u53ef\u8c03\u7528\u7684\u201d\uff08\u8c03\u7528\u65f6\u4e0d\u4f1a\u60ca\u614c\uff09\uff0c\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528\u533f\u540d\u7ed3\u6784\u6587\u5b57\uff0c\u5d4c\u5165\u63a5\u53e3\u7c7b\u578b\uff1a p&gt; <\/p>\n<pre>var op NumOp = struct{ NumOp }{}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u662f\u5426\u53ef\u4ee5\u5728 Go \u4e2d\u5b9a\u4e49\u533f\u540d\u63a5\u53e3\u5b9e\u73b0\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207391","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207391","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=207391"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207391\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}