{"id":57728,"date":"2024-10-10T20:41:17","date_gmt":"2024-10-10T12:41:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57728\/"},"modified":"2024-10-10T20:41:17","modified_gmt":"2024-10-10T12:41:17","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%87%bd%e6%95%b8%e9%a1%9e%e5%9e%8b%e5%af%a6%e7%8f%be%e6%8e%a5%e5%8f%a3-%e6%8a%8a%e5%87%bd%e6%95%b8%e4%bd%9c%e7%82%ba%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57728\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u51fd\u6578\u985e\u578b\u5be6\u73fe\u63a5\u53e3\u2014\u2014\u628a\u51fd\u6578\u4f5c\u70ba\u63a5\u53e3\u4f86\u8abf\u7528"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%87%bd%e6%95%b8%e9%a1%9e%e5%9e%8b%e5%af%a6%e7%8f%be%e6%8e%a5%e5%8f%a3-%e6%8a%8a%e5%87%bd%e6%95%b8%e4%bd%9c%e7%82%ba%e6%8e%a5%e5%8f%a3-AOVQlHxhHV\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u51fd\u6578\u985e\u578b\u5be6\u73fe\u63a5\u53e3\u2014\u2014\u628a\u51fd\u6578\u4f5c\u70ba\u63a5\u53e3\u4f86\u8abf\u7528<\/h1>\n<p>\u5728Go\u8a9e\u8a00\u4e2d\uff0c\u51fd\u6578\u662f\u4e00\u7b49\u516c\u6c11\uff0c\u9019\u610f\u5473\u8457\u51fd\u6578\u53ef\u4ee5\u88ab\u8ce6\u503c\u7d66\u8b8a\u91cf\u3001\u4f5c\u70ba\u53c3\u6578\u50b3\u905e\uff0c\u751a\u81f3\u4f5c\u70ba\u8fd4\u56de\u503c\u8fd4\u56de\u3002\u9019\u7a2e\u7279\u6027\u4f7f\u5f97Go\u8a9e\u8a00\u5728\u5be6\u73fe\u63a5\u53e3\u6642\uff0c\u80fd\u5920\u9748\u6d3b\u5730\u4f7f\u7528\u51fd\u6578\u985e\u578b\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728Go\u4e2d\u4f7f\u7528\u51fd\u6578\u4f5c\u70ba\u63a5\u53e3\u4f86\u8abf\u7528\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%8e%a5%e5%8f%a3%ef%bc%9f-AOVQlHxhHV\">\u4ec0\u9ebc\u662f\u63a5\u53e3\uff1f<\/h2>\n<p>\u5728Go\u8a9e\u8a00\u4e2d\uff0c\u63a5\u53e3\u662f\u4e00\u7d44\u65b9\u6cd5\u7684\u96c6\u5408\u3002\u4efb\u4f55\u985e\u578b\u53ea\u8981\u5be6\u73fe\u4e86\u63a5\u53e3\u4e2d\u5b9a\u7fa9\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u5c31\u53ef\u4ee5\u88ab\u8996\u70ba\u8a72\u63a5\u53e3\u7684\u5be6\u73fe\u3002\u9019\u7a2e\u7279\u6027\u4f7f\u5f97Go\u8a9e\u8a00\u5177\u6709\u9ad8\u5ea6\u7684\u9748\u6d3b\u6027\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/p>\n<h2 id=\"%e5%87%bd%e6%95%b8%e9%a1%9e%e5%9e%8b%e7%9a%84%e5%ae%9a%e7%be%a9-AOVQlHxhHV\">\u51fd\u6578\u985e\u578b\u7684\u5b9a\u7fa9<\/h2>\n<p>\u5728Go\u4e2d\uff0c\u51fd\u6578\u985e\u578b\u53ef\u4ee5\u88ab\u5b9a\u7fa9\u70ba\u4e00\u500b\u8b8a\u91cf\u985e\u578b\u3002\u9019\u610f\u5473\u8457\u6211\u5011\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u51fd\u6578\u985e\u578b\uff0c\u7136\u5f8c\u5c07\u5177\u9ad4\u7684\u51fd\u6578\u8ce6\u503c\u7d66\u9019\u500b\u8b8a\u91cf\u3002\u4ee5\u4e0b\u662f\u51fd\u6578\u985e\u578b\u7684\u57fa\u672c\u5b9a\u7fa9\uff1a<\/p>\n<pre><code>type MyFunc func(int) int<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba\u7684\u51fd\u6578\u985e\u578b\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u6574\u6578\u4f5c\u70ba\u53c3\u6578\u4e26\u8fd4\u56de\u4e00\u500b\u6574\u6578\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e5%87%bd%e6%95%b8%e4%bd%9c%e7%82%ba%e6%8e%a5%e5%8f%a3-AOVQlHxhHV\">\u4f7f\u7528\u51fd\u6578\u4f5c\u70ba\u63a5\u53e3<\/h2>\n<p>\u6211\u5011\u53ef\u4ee5\u5c07\u51fd\u6578\u985e\u578b\u4f5c\u70ba\u63a5\u53e3\u4f86\u4f7f\u7528\u3002\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5c07\u4e0d\u540c\u7684\u51fd\u6578\u50b3\u905e\u7d66\u540c\u4e00\u500b\u63a5\u53e3\uff0c\u5f9e\u800c\u5be6\u73fe\u591a\u614b\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff1a<\/p>\n<pre><code>type Operation interface {\n    Execute(int) int\n}\n\ntype Add struct{}\nfunc (a Add) Execute(x int) int {\n    return x + 10\n}\n\ntype Multiply struct{}\nfunc (m Multiply) Execute(x int) int {\n    return x * 10\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba<code>Operation<\/code>\u7684\u63a5\u53e3\uff0c\u4e26\u5275\u5efa\u4e86\u5169\u500b\u7d50\u69cb\u9ad4<code>Add<\/code>\u548c<code>Multiply<\/code>\uff0c\u5b83\u5011\u90fd\u5be6\u73fe\u4e86<code>Execute<\/code>\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"%e5%b0%87%e5%87%bd%e6%95%b8%e9%a1%9e%e5%9e%8b%e8%88%87%e6%8e%a5%e5%8f%a3%e7%b5%90%e5%90%88-AOVQlHxhHV\">\u5c07\u51fd\u6578\u985e\u578b\u8207\u63a5\u53e3\u7d50\u5408<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u51fd\u6578\u985e\u578b\u8207\u63a5\u53e3\u7d50\u5408\uff0c\u5be6\u73fe\u4e00\u500b\u66f4\u9748\u6d3b\u7684\u8a2d\u8a08\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5c07\u51fd\u6578\u4f5c\u70ba\u63a5\u53e3\u4f86\u8abf\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>type FuncOperation func(int) int\n\nfunc (f FuncOperation) Execute(x int) int {\n    return f(x)\n}\n\nfunc main() {\n    add := FuncOperation(func(x int) int {\n        return x + 10\n    })\n\n    multiply := FuncOperation(func(x int) int {\n        return x * 10\n    })\n\n    fmt.Println(add.Execute(5))       \/\/ \u8f38\u51fa: 15\n    fmt.Println(multiply.Execute(5))  \/\/ \u8f38\u51fa: 50\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba<code>FuncOperation<\/code>\u7684\u51fd\u6578\u985e\u578b\uff0c\u4e26\u70ba\u5176\u6dfb\u52a0\u4e86\u4e00\u500b<code>Execute<\/code>\u65b9\u6cd5\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5275\u5efa\u4e86\u5169\u500b\u51fd\u6578<code>add<\/code>\u548c<code>multiply<\/code>\uff0c\u4e26\u5c07\u5b83\u5011\u4f5c\u70ba<code>FuncOperation<\/code>\u7684\u5be6\u4f8b\u4f86\u8abf\u7528\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-AOVQlHxhHV\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u5c07\u51fd\u6578\u4f5c\u70ba\u63a5\u53e3\u4f86\u8abf\u7528\uff0c\u6211\u5011\u53ef\u4ee5\u5728Go\u8a9e\u8a00\u4e2d\u5be6\u73fe\u9748\u6d3b\u7684\u8a2d\u8a08\u6a21\u5f0f\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u9084\u589e\u5f37\u4e86\u4ee3\u78bc\u7684\u53ef\u7dad\u8b77\u6027\u3002\u7121\u8ad6\u662f\u5728\u958b\u767c\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\u6642\uff0c\u9019\u7a2e\u6280\u8853\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7d44\u7e54\u548c\u7ba1\u7406\u4ee3\u78bc\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bcVPS\u3001<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u3001<a href=\"https:\/\/server.hk\">\u670d\u52a1\u5668<\/a>\u6216\u5176\u4ed6\u96f2\u670d\u52d9\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Go\u8a9e\u8a00\u4e2d\u5982\u4f55\u5c07\u51fd\u6578\u4f5c\u70ba\u63a5\u53e3\u8abf\u7528\uff0c\u6df1\u5165\u4e86\u89e3\u51fd\u6578\u985e\u578b\u7684\u5be6\u73fe\u8207\u61c9\u7528\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6280\u5de7\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57728","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57728","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=57728"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57728\/revisions"}],"predecessor-version":[{"id":57729,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57728\/revisions\/57729"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}