{"id":1616,"date":"2023-03-08T10:11:36","date_gmt":"2023-03-08T10:11:36","guid":{"rendered":"https:\/\/www.liutianfeng.com\/?p=1616"},"modified":"2023-05-05T03:43:45","modified_gmt":"2023-05-05T03:43:45","slug":"%e5%87%bd%e6%95%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.liutianfeng.com\/?p=1616","title":{"rendered":"\u51fd\u6570 &#038; \u65b9\u6cd5 &#038; \u63a5\u53e3"},"content":{"rendered":"<h2>\u51fd\u6570<\/h2>\n<p>\u51fd\u6570\u5b9a\u4e49\u4e86\u540d\u79f0name\uff0c\u4f20\u5165\u53c2\u6570(parameter-list)\uff0c\u901a\u8fc7\u51fd\u6570\u4f53(body)\u7684\u5904\u7406\uff0c\u8fd4\u56de\u7ed3\u679c(result-list)\uff0c\u4f20\u5165\u53c2\u6570\u548c\u8fd4\u56de\u7ed3\u679c\u90fd\u53ef\u4ee5\u591a\u4e2a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">func name(parameter-list) (result-list) { body }<\/code><\/pre>\n<h2>\u65b9\u6cd5<\/h2>\n<p>\u65b9\u6cd5\u7684\u58f0\u540d\u548c\u51fd\u6570\u7c7b\u4f3c\uff0c\u53ea\u662f\u5728\u51fd\u6570\u540d\u5b57\u524d\u9762\u52a0\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u628a\u65b9\u6cd5\u7ed1\u5b9a\u5230\u8be5\u7c7b\u578b\u4e0a\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">func (defined-type) name(parameter-list) (result-list) { body }<\/code><\/pre>\n<p>\u4e00\u4e2a\u7b80\u5355\u548c\u4f8b\u5b50\u8bf4\u660e\u8bf4\u660e\u4e00\u4e0b\u4e24\u8005\u7684\u4e0d\u540c\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">package main\n\nimport (\n    \"fmt\"\n    \"math\"\n)\n\ntype Point struct{ X, Y float64 }\n\n\/\/ \u666e\u901a\u51fd\u6570\nfunc Distance(p, q Point) float64 {\n    return math.Hypot(q.X-p.X, q.Y-p.Y)\n}\n\n\/\/ Point\u7c7b\u578b\u7684\u65b9\u6cd5\nfunc (p Point) Distance(q Point) float64 {\n    return math.Hypot(q.X-p.X, q.Y-p.Y)\n}\n\ntype Path []Point\n\nfunc (path Path) Distance() float64 {\n    sum := 0.0\n    for i := range path {\n        fmt.Println(i, path[i])\n        if i &gt; 0 {\n            sum += path[i-1].Distance(path[i]) \/\/ \u8c03\u7528Point\u7c7b\u578b\u7684\u65b9\u6cd5\n        }\n    }\n    return sum\n}\n\nfunc main() {\n    a := Point{1, 2}\n    b := Point{4, 6}\n    perim := Path{\n        {1, 1},\n        {5, 1},\n        {5, 4},\n        {1, 1},\n    }\n    fmt.Println(\"\u51fd\u6570\u8c03\u7528:\", Distance(a, b)) \/\/ \u51fd\u6570\u8c03\u7528\n    fmt.Println(\"\u65b9\u6cd5\u8c03\u7528:\", a.Distance(b))  \/\/ \u5355\u6b21Point\u65b9\u6cd5\u8c03\u7528, \u8ba1\u7b97\u7ebf\u6bb5\u7684\u957f\n    fmt.Println(\"\u5468\u957f:\", perim.Distance())   \/\/ \u591a\u6b21Point\u65b9\u6cd5\u8c03\u7528, \u8ba1\u7b97\u5468\u957f\n}\n\n# .\/main.exe \n\u51fd\u6570\u8c03\u7528: 5\n\u65b9\u6cd5\u8c03\u7528: 5\n0 {1 1} \/\/ 0 &lt; 1, \u7565\u8fc7\n1 {5 1} \/\/ \u8ba1\u7b97{5, 1}.Distance({1, 1})\u7684\u957f\u5ea6\u540esum+=\n2 {5 4} \/\/ \u8ba1\u7b97{5, 4}.Distance({5, 1})\u7684\u957f\u5ea6\u540esum+=\n3 {1 1} \/\/ \u8ba1\u7b97{1, 1}.Distance({5, 4})\u7684\u957f\u5ea6\u540esum+=\n\u5468\u957f: 12<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684\u4e24\u4e2aDistance\u4e0d\u51b2\u7a81: \u4e00\u4e2a\u662f\u5305\u7ea7\u522b\u7684\u51fd\u6570(geometry.Distance); \u4e00\u4e2a\u662f\u7c7b\u578bPoint\u7684\u65b9\u6cd5(Point.Distance)<br \/>\u5305\u5916\u5f15\u7528\u7684\u65f6\u5019\uff0c\u51fd\u6570\u9700\u8981\u5e26\u5305\u7684\u540d\u79f0\uff0c\u800c\u65b9\u6cd5\u4e0d\u9700\u8981\u3002<\/p>\n<h2>\u63a5\u53e3<\/h2>\n<p>\u63a5\u53e3\u7c7b\u578b\u662f\u5bf9\u5176\u5b83\u7c7b\u578b\u7684\u6982\u62ec\u4e0e\u62bd\u8c61\u3002Go\u8bed\u8a00\u7684\u63a5\u53e3\u662f\u9690\u5f0f\u5b9e\u73b0\u7684\uff0c\u5373\uff1a\u5177\u4f53\u7684\u7c7b\u578b\u65e0\u9700\u58f0\u660e\u5b83\u5b9e\u73b0\u4e86\u54ea\u4e9b\u63a5\u53e3\uff0c\u53ea\u9700\u63d0\u4f9b\u6240\u9700\u7684\u65b9\u6cd5\u5373\u53ef\u3002\u5982\u6b64\uff0c\u65e0\u9700\u4fee\u6539\u5df2\u6709\u7c7b\u578b\u7684\u5b9e\u73b0\uff0c\u5c31\u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u63a5\u53e3\uff0c\u5bf9\u4e8e\u4e0d\u80fd\u4fee\u6539\u7684\u5305\u7c7b\u578b\uff0c\u8fd9\u70b9\u975e\u5e38\u6709\u7528\u3002<br \/><strong>\u5177\u4f53\u7c7b\u578b\u548c\u63a5\u53e3\u7c7b\u578b\u7684\u533a\u522b\uff1a<\/strong><\/p>\n<ul>\n<li>\u5177\u4f53\u7c7b\u578b\uff1a\u5b83\u7684\u6570\u636e\u6709\u7cbe\u786e\u7684\u5e03\u5c40\uff0c\u4ee5\u53ca\u57fa\u4e8e\u8fd9\u4e2a\u7cbe\u786e\u5e03\u5c40\u7684\u5185\u90e8\u64cd\u4f5c\u3002<\/li>\n<li>\u63a5\u53e3\u7c7b\u578b\uff1a\u5b83\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u7c7b\u578b\uff0c\u5e76\u6ca1\u6709\u66b4\u9732\u51fa\u6570\u636e\u7684\u5e03\u5c40\u3001\u5185\u90e8\u7ed3\u6784\u548c\u57fa\u672c\u64cd\u4f5c\uff0c\u800c\u662f\u4ec5\u4ec5\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u3002\u63a5\u53e3\u7c7b\u578b\u4e0d\u80fd\u77e5\u9053\u5b83\u662f\u4ec0\u4e48\uff0c\u53ea\u77e5\u9053\u5b83\u53ef\u4ee5\u505a\u4ec0\u4e48\u3010\u63d0\u4f9b\u4e86\u54ea\u4e9b\u65b9\u6cd5\u3011\u3002<\/li>\n<\/ul>\n<pre class=\"pure-highlightjs\"><code class=\"\">package io\n\ntype Reader interface {\n    Read(p []byte) (n int, err error)\n}\n\ntype Writer interface {\n    Write(p []byte) (n int, err error)\n}\n\ntype Closer interface {\n    Close() error\n}\n\n\/\/ \u5d4c\u5165\u5f0f\u63a5\u53e3-1: \u7528\u4e24\u4e2a\u5c01\u88c5\u597d\u7684\u63a5\u53e3\ntype ReadWriter interface {\n    Reader\n    Writer\n}\n\u6216\n\/\/ \u5d4c\u5165\u5f0f\u63a5\u53e3-1: \u7528\u6df7\u5408\u6216\u8005\u539f\u59cb\u65b9\u6cd5\u7684\u63a5\u53e3\ntype ReadWriter interface {\n    Read(p []byte) (n int, err error)\n    Writer\n}\n\ntype ReadWriteCloser interface {\n    Reader\n    Writer\n    closer\n}<\/code><\/pre>\n<p>\u5d4c\u5165\u5f0f\u63a5\u53e3\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\uff0c\u4e0d\u7528\u5199\u51fa\u6240\u6709\u7684\u65b9\u6cd5\u3002\u5982\uff1aio.ReadWriter<\/p>\n<p>\u00a0<\/p>\n\n\n<p><\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.liutianfeng.com\">liutianfeng.com<\/a> &raquo; <a href=\"https:\/\/www.liutianfeng.com\/?p=1616\">\u51fd\u6570 &#038; \u65b9\u6cd5 &#038; \u63a5\u53e3<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570 \u51fd\u6570\u5b9a\u4e49\u4e86\u540d\u79f0name\uff0c\u4f20\u5165\u53c2\u6570(parameter-list)\uff0c\u901a\u8fc7\u51fd\u6570\u4f53(body)\u7684\u5904\u7406\uff0c\u8fd4\u56de\u7ed3\u679c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[70],"tags":[],"_links":{"self":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1616"}],"collection":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1616"}],"version-history":[{"count":6,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1616\/revisions"}],"predecessor-version":[{"id":1685,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/1616\/revisions\/1685"}],"wp:attachment":[{"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liutianfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}