{"id":3208,"date":"2025-03-25T23:58:07","date_gmt":"2025-03-25T15:58:07","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3208"},"modified":"2025-03-25T23:58:07","modified_gmt":"2025-03-25T15:58:07","slug":"go-%e8%af%ad%e8%a8%80%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/25\/go-%e8%af%ad%e8%a8%80%e6%8e%a5%e5%8f%a3\/","title":{"rendered":"Go \u8bed\u8a00\u63a5\u53e3"},"content":{"rendered":"\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u63a5\u53e3\uff08Interface\uff09\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u6307\u5b9a\u4e86\u5bf9\u8c61\u7684\u884c\u4e3a\uff0c\u800c\u4e0d\u6d89\u53ca\u5bf9\u8c61\u7684\u5177\u4f53\u5b9e\u73b0\u3002Go \u8bed\u8a00\u7684\u63a5\u53e3\u4e0e\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684\u63a5\u53e3\u7c7b\u4f3c\uff0c\u4f46\u5b83\u6709\u51e0\u4e2a\u72ec\u7279\u7684\u7279\u70b9\u3002\u6700\u91cd\u8981\u7684\u4e00\u70b9\u662f\uff0c<strong>Go \u4e2d\u7684\u63a5\u53e3\u662f\u9690\u5f0f\u5b9e\u73b0\u7684<\/strong>\uff0c\u5373\u4e00\u4e2a\u7c7b\u578b\u53ea\u8981\u5b9e\u73b0\u4e86\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\uff0c\u5c31\u81ea\u52a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\uff0c\u65e0\u9700\u663e\u5f0f\u58f0\u660e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u63a5\u53e3\u5b9a\u4e49<\/strong><\/h2>\n\n\n\n<p>\u63a5\u53e3\u7684\u5b9a\u4e49\u662f\u901a\u8fc7 <code>type<\/code> \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u7684\uff0c\u63a5\u53e3\u4e2d\u5305\u542b\u4e86\u4e00\u7ec4\u65b9\u6cd5\u7684\u7b7e\u540d\u3002\u4efb\u4f55\u7c7b\u578b\uff0c\u53ea\u8981\u5b9e\u73b0\u4e86\u63a5\u53e3\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u5c31\u81ea\u52a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\ntype Speaker interface {\n    Speak() string\n}\n\ntype Person struct {\n    Name string\n}\n\n\/\/ Person \u5b9e\u73b0\u4e86 Speaker \u63a5\u53e3\u7684 Speak \u65b9\u6cd5\nfunc (p Person) Speak() string {\n    return \"Hello, my name is \" + p.Name\n}\n\nfunc main() {\n    var speaker Speaker\n    p := Person{Name: \"John\"}\n    speaker = p  \/\/ Person \u7c7b\u578b\u9690\u5f0f\u5b9e\u73b0\u4e86 Speaker \u63a5\u53e3\n    fmt.Println(speaker.Speak())  \/\/ \u8f93\u51fa: Hello, my name is John\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u63a5\u53e3\u7684\u7279\u70b9<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u81ea\u52a8\u5b9e\u73b0\u63a5\u53e3<\/strong><\/h3>\n\n\n\n<p>Go \u8bed\u8a00\u63a5\u53e3\u4e0e\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\uff0c<strong>Go \u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\u67d0\u4e2a\u7c7b\u578b\u5b9e\u73b0\u4e86\u67d0\u4e2a\u63a5\u53e3<\/strong>\uff0c\u53ea\u8981\u7c7b\u578b\u5b9e\u73b0\u4e86\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\uff0cGo \u4f1a\u81ea\u52a8\u8ba4\u4e3a\u8be5\u7c7b\u578b\u5b9e\u73b0\u4e86\u63a5\u53e3\u3002\u8fd9\u79cd\u673a\u5236\u4f7f\u5f97 Go \u7684\u63a5\u53e3\u66f4\u52a0\u7075\u6d3b\u548c\u7b80\u6d01\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u7a7a\u63a5\u53e3<\/strong><\/h3>\n\n\n\n<p>\u7a7a\u63a5\u53e3 <code>interface{}<\/code> \u53ef\u4ee5\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b\u7684\u503c\uff0c\u56e0\u4e3a\u4efb\u4f55\u7c7b\u578b\u90fd\u81f3\u5c11\u5b9e\u73b0\u4e86\u96f6\u4e2a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc printValue(i interface{}) {\n    fmt.Println(i)\n}\n\nfunc main() {\n    printValue(42)        \/\/ \u8f93\u51fa: 42\n    printValue(\"Hello\")   \/\/ \u8f93\u51fa: Hello\n    printValue(3.14)      \/\/ \u8f93\u51fa: 3.14\n}<\/code><\/pre>\n\n\n\n<p>\u7a7a\u63a5\u53e3\u5e38\u7528\u4e8e\u5b58\u50a8\u672a\u77e5\u7c7b\u578b\u7684\u503c\uff0c\u5982\u6570\u636e\u7ed3\u6784\u4e2d\u5b58\u50a8\u591a\u79cd\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u6216\u8005\u4e0e\u5916\u90e8\u7cfb\u7edf\u4ea4\u4e92\u65f6\u63a5\u6536\u5404\u79cd\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u63a5\u53e3\u7684\u7c7b\u578b\u65ad\u8a00<\/strong><\/h2>\n\n\n\n<p>Go \u63d0\u4f9b\u4e86 <strong>\u7c7b\u578b\u65ad\u8a00<\/strong> \u6765\u5c06\u63a5\u53e3\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b\u3002\u8bed\u6cd5\u662f\uff1a<code>x.(T)<\/code>\uff0c\u5176\u4e2d <code>x<\/code> \u662f\u63a5\u53e3\u7c7b\u578b\u7684\u53d8\u91cf\uff0c<code>T<\/code> \u662f\u76ee\u6807\u7c7b\u578b\u3002\u5982\u679c\u65ad\u8a00\u6210\u529f\uff0c\u5219\u8fd4\u56de\u8be5\u7c7b\u578b\u7684\u503c\uff0c\u5982\u679c\u5931\u8d25\uff0c\u5219\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5e03\u5c14\u503c\u6765\u68c0\u67e5\u65ad\u8a00\u662f\u5426\u6210\u529f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u57fa\u672c\u7c7b\u578b\u65ad\u8a00<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var i interface{} = 42\n\n    \/\/ \u7c7b\u578b\u65ad\u8a00\u5c06\u7a7a\u63a5\u53e3\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b int\n    if v, ok := i.(int); ok {\n        fmt.Println(\"\u6210\u529f\u65ad\u8a00\u4e3a int:\", v)\n    } else {\n        fmt.Println(\"\u65ad\u8a00\u5931\u8d25\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u63a5\u53e3\u65ad\u8a00\u4e3a\u5177\u4f53\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>\u63a5\u53e3\u53ef\u4ee5\u65ad\u8a00\u4e3a\u5177\u4f53\u7684\u7c7b\u578b\u6765\u8c03\u7528\u8be5\u7c7b\u578b\u7684\u65b9\u6cd5\u6216\u8bbf\u95ee\u5176\u5c5e\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\ntype Animal interface {\n    Speak() string\n}\n\ntype Dog struct{}\n\nfunc (d Dog) Speak() string {\n    return \"Woof!\"\n}\n\nfunc main() {\n    var animal Animal\n    dog := Dog{}\n    animal = dog  \/\/ Dog \u7c7b\u578b\u9690\u5f0f\u5b9e\u73b0 Animal \u63a5\u53e3\n\n    \/\/ \u7c7b\u578b\u65ad\u8a00\n    if d, ok := animal.(Dog); ok {\n        fmt.Println(\"Dog \u7c7b\u578b:\", d.Speak())  \/\/ \u8f93\u51fa: Dog \u7c7b\u578b: Woof!\n    } else {\n        fmt.Println(\"\u7c7b\u578b\u65ad\u8a00\u5931\u8d25\")\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u63a5\u53e3\u7684\u7ec4\u5408<\/strong><\/h2>\n\n\n\n<p>Go \u8bed\u8a00\u652f\u6301\u63a5\u53e3\u7ec4\u5408\uff0c\u4e00\u4e2a\u63a5\u53e3\u53ef\u4ee5\u5d4c\u5165\u53e6\u4e00\u4e2a\u63a5\u53e3\uff0c\u4f7f\u5f97\u4e00\u4e2a\u63a5\u53e3\u53ef\u4ee5\u7ee7\u627f\u5176\u4ed6\u63a5\u53e3\u7684\u884c\u4e3a\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f\u5b9e\u73b0\u63a5\u53e3\u7ee7\u627f\u7684\u673a\u5236\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u5b9a\u4e49\u63a5\u53e3 Speaker\ntype Speaker interface {\n    Speak() string\n}\n\n\/\/ \u5b9a\u4e49\u63a5\u53e3 Listener\uff0c\u7ee7\u627f\u4e86 Speaker \u63a5\u53e3\ntype Listener interface {\n    Speaker\n    Listen() string\n}\n\ntype Person struct {\n    Name string\n}\n\n\/\/ Person \u5b9e\u73b0\u4e86 Speaker \u63a5\u53e3\nfunc (p Person) Speak() string {\n    return \"Hello, my name is \" + p.Name\n}\n\n\/\/ Person \u5b9e\u73b0\u4e86 Listener \u63a5\u53e3\nfunc (p Person) Listen() string {\n    return \"I am listening\"\n}\n\nfunc main() {\n    var listener Listener\n    person := Person{Name: \"John\"}\n    listener = person\n    fmt.Println(listener.Speak())   \/\/ \u8f93\u51fa: Hello, my name is John\n    fmt.Println(listener.Listen())  \/\/ \u8f93\u51fa: I am listening\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u63a5\u53e3\u7684\u96f6\u503c<\/strong><\/h2>\n\n\n\n<p>Go \u8bed\u8a00\u4e2d\u7684\u63a5\u53e3\u7c7b\u578b\u6709\u4e00\u4e2a <strong>\u96f6\u503c<\/strong>\uff0c\u5373 <strong><code>nil<\/code><\/strong>\u3002\u63a5\u53e3\u7684\u96f6\u503c\u8868\u793a\u6ca1\u6709\u5177\u4f53\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u4e5f\u6ca1\u6709\u7ed1\u5b9a\u4efb\u4f55\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\ntype Speaker interface {\n    Speak() string\n}\n\nfunc main() {\n    var speaker Speaker\n    fmt.Println(speaker == nil)  \/\/ \u8f93\u51fa: true\uff0c\u63a5\u53e3\u7684\u96f6\u503c\u662f nil\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u7a7a\u63a5\u53e3\u4e0e\u7c7b\u578b\u65ad\u8a00<\/strong><\/h2>\n\n\n\n<p>\u7a7a\u63a5\u53e3 <code>interface{}<\/code> \u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u6765\u83b7\u53d6\u5176\u5177\u4f53\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc printType(i interface{}) {\n    switch v := i.(type) {\n    case int:\n        fmt.Println(\"int \u7c7b\u578b:\", v)\n    case string:\n        fmt.Println(\"string \u7c7b\u578b:\", v)\n    default:\n        fmt.Println(\"\u672a\u77e5\u7c7b\u578b\")\n    }\n}\n\nfunc main() {\n    printType(42)        \/\/ \u8f93\u51fa: int \u7c7b\u578b: 42\n    printType(\"Hello\")   \/\/ \u8f93\u51fa: string \u7c7b\u578b: Hello\n    printType(3.14)      \/\/ \u8f93\u51fa: \u672a\u77e5\u7c7b\u578b\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63a5\u53e3\u5b9a\u4e49<\/strong>\uff1a\u63a5\u53e3\u662f\u5b9a\u4e49\u4e00\u7ec4\u65b9\u6cd5\u7b7e\u540d\uff0c\u4efb\u4f55\u7c7b\u578b\u5b9e\u73b0\u4e86\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5373\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u5b9e\u73b0\u63a5\u53e3<\/strong>\uff1aGo \u7684\u63a5\u53e3\u662f\u9690\u5f0f\u5b9e\u73b0\u7684\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\u7c7b\u578b\u5b9e\u73b0\u63a5\u53e3\u3002<\/li>\n\n\n\n<li><strong>\u7a7a\u63a5\u53e3<\/strong>\uff1a<code>interface{}<\/code> \u7c7b\u578b\u53ef\u4ee5\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u7c7b\u578b\u65ad\u8a00<\/strong>\uff1a\u7528\u4e8e\u5c06\u63a5\u53e3\u7c7b\u578b\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>.(T)<\/code> \u6765\u6267\u884c\u65ad\u8a00\u3002<\/li>\n\n\n\n<li><strong>\u63a5\u53e3\u7ec4\u5408<\/strong>\uff1a\u4e00\u4e2a\u63a5\u53e3\u53ef\u4ee5\u901a\u8fc7\u7ec4\u5408\u5176\u4ed6\u63a5\u53e3\u6765\u6269\u5c55\u5176\u884c\u4e3a\u3002<\/li>\n\n\n\n<li><strong>\u96f6\u503c<\/strong>\uff1a\u63a5\u53e3\u7684\u96f6\u503c\u4e3a <code>nil<\/code>\uff0c\u8868\u793a\u63a5\u53e3\u6ca1\u6709\u7ed1\u5b9a\u4efb\u4f55\u5177\u4f53\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 \u53c2\u8003\u8d44\u6599<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/golang.org\/doc\/effective_go.html#interfaces_and_types\">Go \u5b98\u65b9\u6587\u6863 &#8211; \u63a5\u53e3<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/golang\/go\/wiki\/Interface\">Go Wiki &#8211; \u63a5\u53e3<\/a>\uff01<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u63a5\u53e3\uff08Interface\uff09\u662f\u4e00\u79cd\u7c7b\u578b\uff0c\u6307\u5b9a\u4e86\u5bf9\u8c61\u7684\u884c\u4e3a\uff0c\u800c\u4e0d\u6d89\u53ca\u5bf9\u8c61\u7684\u5177\u4f53\u5b9e\u73b0\u3002Go \u8bed\u8a00\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3209,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3208","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3208"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3208\/revisions"}],"predecessor-version":[{"id":3210,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3208\/revisions\/3210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3209"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}