{"id":208257,"date":"2025-07-08T15:50:38","date_gmt":"2025-07-08T07:50:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208257\/"},"modified":"2025-07-08T15:50:38","modified_gmt":"2025-07-08T07:50:38","slug":"golang-%e6%8e%a5%e5%8f%a3%e4%b8%ad%e6%98%af%e5%90%a6%e5%8f%af%e4%bb%a5%e6%9c%89%e5%8f%af%e9%80%89%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208257\/","title":{"rendered":"golang \u63a5\u53e3\u4e2d\u662f\u5426\u53ef\u4ee5\u6709\u53ef\u9009\u65b9\u6cd5\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>golang \u63a5\u53e3\u4e2d\u662f\u5426\u53ef\u4ee5\u6709\u53ef\u9009\u65b9\u6cd5\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 21:18:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2aGolang\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300agolang \u63a5\u53e3\u4e2d\u662f\u5426\u53ef\u4ee5\u6709\u53ef\u9009\u65b9\u6cd5\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u4e3a\u754c\u9762\u521b\u5efa\u53ef\u9009\u7684 perim \u65b9\u6cd5\u3002\u6709\u53ef\u80fd\u5417\uff1f\u5c31\u50cf\u6211\u4e0d\u60f3\u4e3a\u4e09\u89d2\u5f62\u521b\u5efa perim \u65b9\u6cd5\uff0c\u4f46\u5b83\u7ed9\u4e86\u6211\u7f3a\u5c11\u4e00\u79cd\u65b9\u6cd5\u7684\u9519\u8bef\u3002\u63a5\u53e3\u4e2d\u662f\u5426\u53ef\u4ee5\u6709\u53ef\u9009\u65b9\u6cd5\uff1f<\/p>\n<p>\u8bf7\u544a\u8bc9\u6211\u5b83\u7684\u66ff\u4ee3\u65b9\u6848\u6216\u67d0\u79cd\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<pre>type geometry interface {\n    area() float64\n    perim() float64\n}\n\ntype rect struct {\n    width, height float64\n}\n\ntype triangle struct {\n    base, height float64\n}\n\ntype circle struct {\n    radius float64\n}\n\ntype square struct {\n    side float64\n}\n\nfunc (r rect) area() float64 {\n    return r.width * r.height\n}\n\nfunc (r rect) perim() float64 {\n    return 2*r.width + 2*r.height\n}\n\nfunc (c circle) area() float64 {\n    return math.Pi * c.radius * c.radius\n}\n\nfunc (c circle) perim() float64 {\n    return 2 * math.Pi * c.radius\n}\n\nfunc (t triangle) area() float64 {\n    return 1 \/ 2 * t.base * t.height\n}\n\nfunc measure(g geometry) {\n    fmt.Println(g)\n    switch g.(type) {\n    case rect:\n        fmt.Println(\"Rectangles area :\", g.area())\n        fmt.Println(\"Rectangle perimeter: \", g.perim())\n    case circle:\n        fmt.Printf(\"Circles Area: %.2f\\n\", g.area())\n        fmt.Printf(\"Circles Perimeter: %.2f\\n\", g.perim())\n    case square:\n        fmt.Printf(\"Area of square: %.2f\\n\", g.area())\n        fmt.Printf(\"Perimeters of area: %.2f\\n\", g.perim())\n    case triangle:\n        fmt.Printf(\"Area of  triangle: %.2f\\n\", g.area())\n    }\n}\n\nfunc main() {\n    r := rect{width: 3, height: 4}\n    c := circle{radius: 5}\n    s := square{side: 7}\n    t := triangle{base: 3, height: 4}\n    measure(r)\n    measure(c)\n    measure(s)\n    measure(t)\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u89c4\u8303\u4e0d\u5141\u8bb8\u201c\u6807\u8bb0\u201d\u65b9\u6cd5\u5728 \u7c7b\u578b\u4e2d\u53ef\u9009\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u60a8\u7684\u5b9e\u73b0\u53ef\u80fd\u4f1a\u63d0\u4f9b\u5176\u4ed6\u65b9\u6cd5\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u60a8\u8981\u5b9e\u73b0\u7684\u63a5\u53e3\u7684\u4e00\u90e8\u5206\u7684\u65b9\u6cd5\u3002 \u53ef\u7528\u4e8e\u68c0\u67e5\u503c\u7684\u5177\u4f53\u7c7b\u578b\u662f\u5426\u5177\u6709\u201c\u9644\u52a0\u201d\u65b9\u6cd5\uff0c\u65b9\u6cd5\u662f\u68c0\u67e5\u5b83\u4eec\u662f\u5426\u5b9e\u73b0\u5177\u6709\u8fd9\u4e9b\u9644\u52a0\u65b9\u6cd5\u7684\u63a5\u53e3\u7c7b\u578b\u3002<\/p>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>fooimpl<\/code> \u4ec5\u5177\u6709\u65b9\u6cd5 <code>one()<\/code>\uff0c\u4f46 <code>foo2impl<\/code> \u5177\u6709\u65b9\u6cd5 <code>one()<\/code> \u548c <code>two()<\/code>\uff1a<\/p>\n<pre>type foo interface {\n    one()\n}\n\ntype fooimpl int\n\nfunc (fi fooimpl) one() {}\n\ntype foo2impl int\n\nfunc (fi foo2impl) one() {}\nfunc (fi foo2impl) two() {}\n\nfunc check(f foo) {\n    if ft, ok := f.(interface{ two() }); ok {\n        fmt.printf(\"%t(%v) has method two()\\n\", f, f)\n        ft.two() \/\/ you can call it\n    } else {\n        fmt.printf(\"%t(%v) doesn't have method two()\\n\", f, f)\n    }\n}\n\nfunc main() {\n    check(fooimpl(1))\n    check(foo2impl(2))\n}<\/pre>\n<p>\u5b83\u7684\u8f93\u51fa\uff08\u5728\u4e0a\u8bd5\u8bd5\uff09\uff1a<\/p>\n<pre>main.fooimpl(1) doesn't have method two()\nmain.foo2impl(2) has method two()<\/pre>\n<p>\u60a8\u5f53\u7136\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u9644\u52a0\u65b9\u6cd5\u521b\u5efa\u63a5\u53e3\u7c7b\u578b\uff1a<\/p>\n<pre>type bar interface {\n    two()\n}<\/pre>\n<p>\u7136\u540e\u68c0\u67e5\u5b83\uff1a<\/p>\n<pre>if ft, ok := f.(Bar); ok {\n    fmt.Printf(\"%T(%v) has method Two()\\n\", f, f)\n    ft.Two() \/\/ You can call it\n} else {\n    fmt.Printf(\"%T(%v) doesn't have method Two()\\n\", f, f)\n}<\/pre>\n<p>\u62e8\u6253 \u8bd5\u8bd5\u8fd9\u4e2a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-208257","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208257","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=208257"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208257\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}