{"id":57716,"date":"2024-10-10T20:36:16","date_gmt":"2024-10-10T12:36:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57716\/"},"modified":"2024-10-10T20:36:16","modified_gmt":"2024-10-10T12:36:16","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e6%8e%a5%e5%8f%a3%e8%81%b2%e6%98%8e%ef%bc%88%e5%ae%9a%e7%be%a9%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57716\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u63a5\u53e3\u8072\u660e\uff08\u5b9a\u7fa9\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e6%8e%a5%e5%8f%a3%e8%81%b2%e6%98%8e%ef%bc%88%e5%ae%9a%e7%be%a9%ef%bc%89-EusHZxkiBR\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u63a5\u53e3\u8072\u660e\uff08\u5b9a\u7fa9\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u9ad4\u958b\u767c\u4e2d\uff0cGo \u8a9e\u8a00\u56e0\u5176\u7c21\u6f54\u6027\u548c\u9ad8\u6548\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002Go \u8a9e\u8a00\u7684\u63a5\u53e3\uff08interface\uff09\u662f\u4e00\u500b\u5f37\u5927\u7684\u7279\u6027\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5be6\u73fe\u591a\u614b\u6027\u548c\u89e3\u8026\u5408\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Go \u8a9e\u8a00\u7684\u63a5\u53e3\u8072\u660e\u53ca\u5176\u5b9a\u7fa9\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u7bc4\u4f8b\u4ee5\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u6982\u5ff5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%8e%a5%e5%8f%a3%ef%bc%9f-EusHZxkiBR\">\u4ec0\u9ebc\u662f\u63a5\u53e3\uff1f<\/h2>\n<p>\u5728 Go \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\u4f7f\u5f97 Go \u8a9e\u8a00\u80fd\u5920\u652f\u6301\u591a\u614b\u6027\uff0c\u5141\u8a31\u4e0d\u540c\u985e\u578b\u7684\u5c0d\u8c61\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e6%8e%a5%e5%8f%a3%e7%9a%84%e8%81%b2%e6%98%8e-EusHZxkiBR\">\u63a5\u53e3\u7684\u8072\u660e<\/h2>\n<p>\u63a5\u53e3\u7684\u8072\u660e\u975e\u5e38\u7c21\u55ae\uff0c\u4f7f\u7528\u95dc\u9375\u5b57 <code>type<\/code> \u4f86\u5b9a\u7fa9\u4e00\u500b\u65b0\u7684\u63a5\u53e3\u985e\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u63a5\u53e3\u8072\u660e\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>type Animal interface {\n    Speak() string\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <code>Animal<\/code> \u7684\u63a5\u53e3\uff0c\u8a72\u63a5\u53e3\u5305\u542b\u4e00\u500b\u65b9\u6cd5 <code>Speak<\/code>\uff0c\u8fd4\u56de\u4e00\u500b\u5b57\u7b26\u4e32\u3002<\/p>\n<h2 id=\"%e5%af%a6%e7%8f%be%e6%8e%a5%e5%8f%a3-EusHZxkiBR\">\u5be6\u73fe\u63a5\u53e3<\/h2>\n<p>\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\u4ee5\u4e0b\u662f\u5982\u4f55\u5be6\u73fe <code>Animal<\/code> \u63a5\u53e3\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>type Dog struct{}\n\nfunc (d Dog) Speak() string {\n    return \"Woof!\"\n}\n\ntype Cat struct{}\n\nfunc (c Cat) Speak() string {\n    return \"Meow!\"\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u5169\u500b\u7d50\u69cb\u9ad4 <code>Dog<\/code> \u548c <code>Cat<\/code>\uff0c\u4e26\u70ba\u5b83\u5011\u5be6\u73fe\u4e86 <code>Animal<\/code> \u63a5\u53e3\u4e2d\u7684 <code>Speak<\/code> \u65b9\u6cd5\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e6%8e%a5%e5%8f%a3-EusHZxkiBR\">\u4f7f\u7528\u63a5\u53e3<\/h2>\n<p>\u4e00\u65e6\u6211\u5011\u6709\u4e86\u63a5\u53e3\u7684\u5be6\u73fe\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u5011\u4f86\u9032\u884c\u591a\u614b\u6027\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u63a5\u53e3\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>func MakeSound(a Animal) {\n    fmt.Println(a.Speak())\n}\n\nfunc main() {\n    var dog Animal = Dog{}\n    var cat Animal = Cat{}\n\n    MakeSound(dog) \/\/ \u8f38\u51fa: Woof!\n    MakeSound(cat) \/\/ \u8f38\u51fa: Meow!\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u6578 <code>MakeSound<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u500b <code>Animal<\/code> \u985e\u578b\u7684\u53c3\u6578\u3002\u7121\u8ad6\u50b3\u5165\u7684\u662f <code>Dog<\/code> \u9084\u662f <code>Cat<\/code>\uff0c\u90fd\u80fd\u6b63\u78ba\u8abf\u7528\u76f8\u61c9\u7684 <code>Speak<\/code> \u65b9\u6cd5\uff0c\u5be6\u73fe\u4e86\u591a\u614b\u6027\u3002<\/p>\n<h2 id=\"%e6%8e%a5%e5%8f%a3%e7%9a%84%e5%84%aa%e5%8b%a2-EusHZxkiBR\">\u63a5\u53e3\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u89e3\u8026\u5408\uff1a<\/strong>\u63a5\u53e3\u4f7f\u5f97\u4e0d\u540c\u6a21\u7d44\u4e4b\u9593\u7684\u4f9d\u8cf4\u6027\u964d\u4f4e\uff0c\u4fbf\u65bc\u7dad\u8b77\u548c\u64f4\u5c55\u3002<\/li>\n<li><strong>\u591a\u614b\u6027\uff1a<\/strong>\u901a\u904e\u63a5\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u5f0f\u8655\u7406\u4e0d\u540c\u985e\u578b\u7684\u5c0d\u8c61\u3002<\/li>\n<li><strong>\u6e2c\u8a66\u53cb\u597d\uff1a<\/strong>\u63a5\u53e3\u4f7f\u5f97\u55ae\u5143\u6e2c\u8a66\u8b8a\u5f97\u66f4\u52a0\u5bb9\u6613\uff0c\u56e0\u70ba\u53ef\u4ee5\u4f7f\u7528\u6a21\u64ec\u5c0d\u8c61\u4f86\u66ff\u4ee3\u5be6\u969b\u5c0d\u8c61\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-EusHZxkiBR\">\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u63a5\u53e3\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5be6\u73fe\u9748\u6d3b\u548c\u53ef\u64f4\u5c55\u7684\u4ee3\u78bc\u7d50\u69cb\u3002\u901a\u904e\u63a5\u53e3\u7684\u8072\u660e\u548c\u5be6\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5be6\u73fe\u591a\u614b\u6027\uff0c\u4e26\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u7dad\u8b77\u6027\u548c\u53ef\u6e2c\u8a66\u6027\u3002\u5c0d\u65bc\u5e0c\u671b\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u80fd\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u7406\u89e3 Go \u8a9e\u8a00\u7684\u63a5\u53e3\u7279\u6027\u5c07\u662f\u63d0\u5347\u958b\u767c\u6548\u7387\u7684\u91cd\u8981\u4e00\u6b65\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Go\u8a9e\u8a00\u4e2d\u7684\u63a5\u53e3\u8072\u660e\u8207\u5b9a\u7fa9\uff0c\u638c\u63e1\u5982\u4f55\u4f7f\u7528\u63a5\u53e3\u63d0\u5347\u7a0b\u5f0f\u8a2d\u8a08\u7684\u9748\u6d3b\u6027\u8207\u53ef\u64f4\u5c55\u6027\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-57716","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57716","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=57716"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57716\/revisions"}],"predecessor-version":[{"id":57717,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57716\/revisions\/57717"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}