{"id":1540,"date":"2020-09-24T04:34:54","date_gmt":"2020-09-23T20:34:54","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1540"},"modified":"2022-02-16T21:57:30","modified_gmt":"2022-02-16T13:57:30","slug":"go-interface-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/09\/24\/go-interface-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-\u4ecb\u9762\u7684\u5ba3\u544a\u53ca\u4f7f\u7528"},"content":{"rendered":"<h1>go \u7684\u4ecb\u9762<\/h1>\n<p>\u5728\u524d\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u7d66\u5927\u5bb6\u5982\u4f55\u5728 go \u8a9e\u8a00\u5167\u5beb\u65b9\u6cd5\uff0c\u6709\u4e86\u65b9\u6cd5\u5c31\u53ef\u4ee5\u628a\u76f8\u540c\u7684\u908f\u8f2f\u62bd\u6210\u5171\u7528\u7684\u65b9\u6cd5\uff0c\u8b93\u540c\u4e00\u4ef6\u4e8b\u60c5\u53ef\u4ee5\u91cd\u8907\u547c\u53eb\u5171\u540c\u7684\u65b9\u6cd5\u4f86\u8655\u7406\uff0c\u4f46\u662f\u5982\u679c\u8981\u66f4\u9032\u4e00\u6b65\u7684\u8aaa\u9053\u7a0b\u5f0f\u8a2d\u8a08\u4e0d\u80fd\u4e0d\u63d0\u5230\u7269\u4ef6\u5c0e\u5411\uff0c\u524d\u9762\u4e5f\u6709\u5927\u81f4\u4e0a\u8aaa\u660e go \u7684\u7269\u4ef6\u5c0e\u5411\uff0c\u6240\u4ee5\u9019\u4e00\u7bc7\u8981\u958b\u59cb\u8ddf\u5927\u5bb6\u8aaa\u660e go \u7684\u4ecb\u9762\u3002<\/p>\n<p><!--more--><\/p>\n<h2>go \u4ecb\u9762\u5305\u542b\u7684\u5167\u5bb9<\/h2>\n<p>\u5728\u4ecb\u7d39\u600e\u9ebc\u5728 go \u8a9e\u8a00\u4f7f\u7528\u4ecb\u9762\u4e4b\u524d\u7576\u7136\u8981\u8ddf\u5927\u5bb6\u8aaa\u660e\u4e00\u4e0b\u751a\u9ebc\u662f\u4ecb\u9762\uff0c\u6240\u8b02\u7684\u4ecb\u9762<code>interface<\/code>\u5c31\u662f\u5b9a\u7fa9\u4e00\u500b\u52d5\u4f5c\uff0c\u4f46\u662f\u4e0d\u53bb\u7ba1\u8a72\u52d5\u4f5c\u88e1\u9762\u5982\u4f55\u5be6\u4f5c\u3002\u6240\u4ee5\u8209\u4e00\u500b\u5e38\u5e38\u5728\u7269\u4ef6\u5c0e\u5411\u4e2d\u6703\u7528\u5230\u7684\u4f8b\u5b50\uff0c\u52d5\u7269\u9019\u500b\u985e\u5225\u6709\u4e00\u500b\u4ecb\u9762\u53eb\u505a\u8d70\u8def\uff0c\u800c\u72d7\u7e7c\u627f\u52d5\u7269\u9019\u985e\u5225\uff0c\u6240\u4ee5\u72d7\u4e5f\u8981\u5be6\u4f5c\u8d70\u8def\u9019\u500b\u4ecb\u9762\uff0c\u800c\u4ed6\u5be6\u4f5c\u7684\u65b9\u5f0f\u662f\u7528\u56db\u96bb\u8173\u8d70\u8def\u3002\u53e6\u5916\u4e00\u65b9\u9762\u4eba\u4e5f\u662f\u7e7c\u627f\u52d5\u7269\u9019\u985e\u5225\uff0c\u6240\u4ee5\u4e5f\u8981\u5be6\u4f5c\u8d70\u8def\u9019\u500b\u4ecb\u9762\uff0c\u800c\u4ed6\u5be6\u4f5c\u7684\u65b9\u5f0f\u5c31\u662f\u5169\u96bb\u8173\u8d70\u8def\uff0c\u6240\u4ee5\u6211\u5011\u5c31\u7528\u9019\u4f8b\u5b50\u4f86\u770b\u770b\u5728 go \u600e\u9ebc\u4f7f\u7528\u3002<\/p>\n<h3>go \u4ecb\u9762\u7684\u5ba3\u544a<\/h3>\n<p>\u5c31\u5982\u540c\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u8981\u958b\u59cb\u8aaa\u660e\u5982\u4f55\u5ba3\u544a\u4ecb\u9762\uff0c\u9019\u908a\u8acb\u5927\u5bb6\u5148\u770b\u4e00\u4e0b\u4e0b\u5217\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">type Animal interface {\n    Walk()\n}<\/code><\/pre>\n<p>\u9019\u908a\u8aaa\u660e\u4e00\u4e0b\uff0c\u9019\u5c31\u662f\u5ba3\u544a go \u7684\u4ecb\u9762\u7684\u65b9\u6cd5\uff0c\u9019\u88e1\u9700\u544a\u8aaa<code>Animal<\/code>\u9019\u500b\u4ecb\u9762\u6709\u4e00\u500b\u65b9\u6cd5\u9700\u8981\u5be6\u4f5c\uff0c\u800c\u9019\u500b\u65b9\u6cd5\u5c31\u662f<code>Walk<\/code>\uff0c\u63a5\u4e0b\u4f86\u8aaa\u660e\u4e00\u4e0b\u5982\u4f55\u5be6\u4f5c\u4ecb\u9762\u3002<\/p>\n<h3>go \u4ecb\u9762\u65b9\u6cd5\u7684\u5be6\u4f5c<\/h3>\n<p>\u5728\u524d\u4e00\u500b\u6bb5\u843d\u544a\u8a34\u5927\u5bb6\u5982\u4f55\u5ba3\u544a\u4e00\u500b\u4ecb\u9762\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u8981\u544a\u8a34\u5927\u5bb6\u5982\u4f55\u5be6\u4f5c\u7528\u5169\u96bb\u8173\u8d70\u8def\u6216\u7528\u56db\u96bb\u8173\u8d70\u8def\uff0c\u8acb\u5927\u5bb6\u5148\u770b\u4e00\u4e0b\u4ee5\u4e0b\u7684\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">type Dog int\n\nfunc (d Dog) Walk() {\n    fmt.Println(&quot;Walk with four feets&quot;)\n}\n\ntype Human int\n\nfunc (h Human) Walk() {\n    fmt.Println(&quot;Walk with two feets&quot;)\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u4f8b\u5b50\u5148\u7c21\u55ae\u5beb\u4e00\u4e0b\uff0c\u9019\u88e1\u6709\u4e00\u500b\u985e\u5225\u662f\u72d7\uff0c\u4ed6\u53bb\u5be6\u4f5c\u4e00\u500b\u8d70\u7684\u4ecb\u9762\uff0c\u9084\u6709\u53e6\u5916\u4e00\u500b\u985e\u5225\u662f\u4eba\u985e\u4e5f\u53bb\u5be6\u4f5c\u4e00\u500b\u8d70\u7684\u4ecb\u9762\uff0c\u9019\u88e1\u7c21\u55ae\u5beb\u4e00\u4e0b\u5c31\u662f\u5370\u51fa\u5169\u96bb\u8173\u8d70\u8def\u6216\u662f\u56db\u96bb\u8173\u8d70\u8def\u3002\u5beb\u5230\u9019\u908a\u9084\u6c92\u6709\u8aaa\u660e\u8981\u5982\u4f55\u4f7f\u7528\u9019\u4ecb\u9762\uff0c\u9019\u5c31\u5728\u4e0b\u4e00\u500b\u6bb5\u843d\u8ddf\u5927\u5bb6\u8aaa\u660e\u3002<\/p>\n<h3>go \u4ecb\u9762\u7684\u4f7f\u7528<\/h3>\n<p>\u524d\u5169\u500b\u4f8b\u5b50\u5206\u5225\u544a\u8a34\u5927\u5bb6\u5982\u4f55\u5ba3\u544a\u4e00\u500b\u4ecb\u9762\uff0c\u4e5f\u544a\u8a34\u5927\u5bb6\u5982\u4f55\u5be6\u4f5c\u5169\u500b\u985e\u5225\u7684\u4ecb\u9762\uff0c\u63a5\u4e0b\u4f86\u91cd\u982d\u6232\u5c31\u662f\u8981\u8ddf\u5927\u5bb6\u8aaa\u5982\u4f55\u4f7f\u7528\u9019\u4ecb\u9762\u4e86\uff0c\u5927\u5bb6\u8acb\u770b\u4e00\u4e0b\u4e0b\u5217\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\ntype Animal interface {\n    Walk()\n}\n\nfunc main() {\n    var a Animal\n    d := Dog(1)\n    h := Human(1)\n\n    a = d  \/\/ a Dog implements Animal\n    a.Walk()\n    a = h \/\/ a Human implements Animal\n    a.Walk()\n}\n\ntype Dog int\n\nfunc (d Dog) Walk() {\n    fmt.Println(&quot;Walk with four feet&quot;)\n}\n\ntype Human int\n\nfunc (h Human) Walk() {\n    fmt.Println(&quot;Walk with two feet&quot;)\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u662f\u4e00\u500b\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c\u9996\u5148\u5ba3\u544a\u4e00\u500b\u52d5\u7269\u7684\u8d70\u7684\u4ecb\u9762\uff0c\u7136\u5f8c\u5be6\u4f5c\u4eba\u985e\u7684\u8d70\u8def\u8ddf\u72d7\u7684\u8d70\u8def\uff0c\u6700\u5f8c\u662f\u5ba3\u544a\u4e00\u500b\u52d5\u7269\u7684\u8b8a\u6578\uff0c\u7136\u5f8c\u5ba3\u544a\u4e00\u500b\u72d7\u7684\u8b8a\u6578\u8ddf\u4eba\u985e\u7684\u8b8a\u6578\uff0c\u6700\u5f8c\u5206\u5225\u547c\u53eb\u8eab\u70ba\u72d7\u7684\u8d70\u8def\u9019\u500b\u4ecb\u9762\u8ddf\u8eab\u70ba\u4eba\u985e\u7684\u8d70\u8def\u4ecb\u9762\uff0c\u7d50\u679c\u53ef\u4ee5\u770b\u5230\u5370\u51fa\u4f86\u7528\u56db\u96bb\u8173\u8d70\u8def\u8ddf\u7528\u5169\u96bb\u8173\u8d70\u8def\u7684\u6587\u5b57\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u4e0a\u9762\u8aaa\u660e\u4e86\u5982\u4f55\u5728 go \u8a9e\u8a00\u4e2d\u5ba3\u544a\u4e26\u5be6\u4f5c\u4ecb\u9762\uff0c\u4f46\u662f\u4e0a\u9762\u7684\u4f8b\u5b50\u6709\u5f88\u591a\u8b80\u8005\u53ef\u80fd\u6eff\u8166\u5b50\u4e00\u554f\u9019\u662f\u751a\u9ebc\u6771\u897f\uff0c\u9019\u4e9b\u4f8b\u5982<code>type<\/code>\u9019\u500b\u985e\u5225\u7684\u95dc\u9375\u5b57\u4e0b\u4e00\u7bc7\u5167\u5bb9\u6703\u70ba\u5927\u5bb6\u66f4\u9032\u4e00\u6b65\u4ecb\u7d39\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>go \u7684\u4ecb\u9762 \u5728\u524d\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u7d66\u5927\u5bb6\u5982\u4f55\u5728 go \u8a9e\u8a00\u5167\u5beb\u65b9\u6cd5\uff0c\u6709\u4e86\u65b9\u6cd5\u5c31\u53ef\u4ee5\u628a\u76f8\u540c\u7684\u908f\u8f2f\u62bd\u6210\u5171\u7528\u7684\u65b9\u6cd5\uff0c\u8b93\u540c &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/09\/24\/go-interface-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-\u4ecb\u9762\u7684\u5ba3\u544a\u53ca\u4f7f\u7528\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81],"class_list":["post-1540","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1540"}],"version-history":[{"count":6,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1540\/revisions"}],"predecessor-version":[{"id":2682,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1540\/revisions\/2682"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}