{"id":7936,"date":"2023-01-28T13:54:52","date_gmt":"2023-01-28T05:54:52","guid":{"rendered":"https:\/\/egonlin.com\/?p=7936"},"modified":"2023-01-28T13:54:52","modified_gmt":"2023-01-28T05:54:52","slug":"21-%e5%87%bd%e6%95%b0%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=7936","title":{"rendered":"21 \u51fd\u6570\u53d8\u91cf"},"content":{"rendered":"<h1>\u51fd\u6570\u53d8\u91cf<\/h1>\n<h2>\u4e00 \u51fd\u6570\u662f\u7b2c\u4e00\u7b49\u516c\u6c11<\/h2>\n<p>go\u4e2d\u51fd\u6570\u5728Go\u8bed\u8a00\u4e2d\u5c5e\u4e8e\u201c\u4e00\u7b49\u516c\u6c11\u201d\uff0c\u8bf4\u767d\u4e86\u5c31\u662f\u53ef\u4ee5\u628a\u51fd\u6570\u5f53\u53d8\u91cf\u4e00\u6837\u53bb\u7528\uff0c\u5177\u4f53\u5982\u4e0b<\/p>\n<p>1\u3001\u53ef\u4ee5\u58f0\u660e\u51fd\u6570\u7c7b\u578b\u7684\u53d8\u91cf<\/p>\n<p>2\u3001\u53ef\u4ee5\u8d4b\u503c<\/p>\n<p>3\u3001\u53ef\u4ee5\u5f53\u505a\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570<\/p>\n<p>4\u3001\u53ef\u4ee5\u5f53\u505a\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/p>\n<p>\u4e86\u89e3\uff1a\u9ad8\u9636\u51fd\u6570\u6307\u7684\u5c31\u662f<\/p>\n<p>\u200b   1\u3001\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570<\/p>\n<p>\u200b   2\u3001\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u8fd4\u56de\u503c<\/p>\n<h2>\u4e8c \u53ef\u4ee5\u58f0\u660e\u51fd\u6570\u7c7b\u578b\u7684\u53d8\u91cf<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>type<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7c7b\u578b\uff0c\u5177\u4f53\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-go\">type calculation func(int,int) int<\/code><\/pre>\n<p>\u4e0a\u9762\u8bed\u53e5\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>calculation<\/code>\u7c7b\u578b\uff0c\u5b83\u662f\u4e00\u79cd\u51fd\u6570\u7c7b\u578b\uff0c\u8fd9\u79cd\u51fd\u6570\u63a5\u6536\u4e24\u4e2aint\u7c7b\u578b\u7684\u53c2\u6570\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2aint\u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff0c\u51e1\u662f\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u51fd\u6570\u90fd\u662fcalculation\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u5982\u4e0badd\u548csub<\/p>\n<pre><code class=\"language-go\">func add(x, y int) int{\n    return x + y\n}\nfunc sub(x, y int) int{\n    return x - y\n}\n\nfunc main() {\n    fmt.Printf(&quot;%T\\n&quot;,add)  \/\/ func(int, int) int\n    fmt.Printf(&quot;%T\\n&quot;,sub)  \/\/ func(int, int) int\n}<\/code><\/pre>\n<h2>\u4e09 \u53ef\u4ee5\u628a\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf<\/h2>\n<p>\u53ef\u4ee5\u628a\u51fd\u6570\u8d4b\u503c\u53d8\u91cf\uff0c\u524d\u63d0\u662f\u8be5\u53d8\u91cf\u662f\u5bf9\u5e94\u7684\u51fd\u6570\u7c7b\u578b<\/p>\n<pre><code class=\"language-go\">func main() {\n    var c calculation     \/\/ \u58f0\u660e\u4e00\u4e2acalculation\u7c7b\u578b\u7684\u53d8\u91cfc\n    c = add               \/\/ \u628aadd\u8d4b\u503c\u7ed9\u53d8\u91cfc\n    fmt.Printf(&quot;%T\\n&quot;, c) \/\/ main.calculation\n    fmt.Println(c(1, 2))  \/\/ \u50cf\u8c03\u7528add\u4e00\u6837\u8c03\u7528c\n\n    f := add               \/\/ \u5c06\u51fd\u6570add\u8d4b\u503c\u7ed9\u53d8\u91cff\n    fmt.Printf(&quot;%T\\n&quot;, f)  \/\/ func(int, int) int\n    fmt.Println(f(10, 20)) \/\/ \u50cf\u8c03\u7528add\u4e00\u6837\u8c03\u7528f\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>\u51fd\u6570\u7684\u7c7b\u578b\u88ab\u79f0\u4e3a\u51fd\u6570\u7684\u6807\u8bc6\u7b26\u3002\u5982\u679c\u4e24\u4e2a\u51fd\u6570\u5f62\u5f0f\u53c2\u6570\u5217\u8868\u548c\u8fd4\u56de\u503c\u5217\u8868\u4e2d\u7684\u53d8\u91cf\u7c7b\u578b\u4e00\u4e00\u5bf9\u5e94\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u51fd\u6570\u88ab\u8ba4\u4e3a\u6709\u76f8\u540c\u7684\u7c7b\u578b\u548c\u6807\u8bc6\u7b26\u3002\u5f62\u53c2\u548c\u8fd4\u56de\u503c\u7684\u53d8\u91cf\u540d\u4e0d\u5f71\u54cd\u51fd\u6570\u6807\u8bc6\u7b26\uff0c\u4e5f\u4e0d\u5f71\u54cd\u5b83\u4eec\u662f\u5426\u53ef\u4ee5\u4ee5\u7701\u7565\u53c2\u6570\u7c7b\u578b\u7684\u5f62\u5f0f\u8868\u793a\u3002<\/p>\n<pre><code class=\"language-go\">\/\/ \u5982\u4e0btest1\u4e0etest2\u7684\u53c2\u6570\u540d\u4e0d\u540c\uff0c\u4f46\u5b83\u4eec\u662f\u76f8\u540c\u7684\u7c7b\u578b\nfunc test1(x int, y int) (z float64) {\n    fmt.Println(x,y)\n    return 1.1\n}\n\nfunc test2(xxx int, yyy int) (zzz float64) {\n    fmt.Println(xxx,yyy)\n    return 1.1\n}\n\nfunc main() {\n    fmt.Printf(&quot;%T\\n&quot;,test1)  \/\/ func(int, int) float64\n    fmt.Printf(&quot;%T\\n&quot;,test2)  \/\/ func(int, int) float64\n\n    var f func(int, int) float64\n    f = test1\n    f(1,2)\n\n    f=test2\n    f(10,20)\n}<\/code><\/pre>\n<h2>\u56db \u53ef\u4ee5\u628a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/h2>\n<pre><code class=\"language-go\">type calculation func(int, int) int\n\nfunc add(x, y int) int {\n    return x + y\n}\nfunc cal1(x, y int, op calculation) int {\n    return op(x, y)\n}\n\nfunc cal2(x, y int, op func(x, y int) int) int { \/\/ \u540c\u4e0a\u8ff0cal1\n    return op(x, y)\n}\n\nfunc main() {\n    res1 := cal1(10, 20, add)\n    fmt.Println(res1) \/\/ 30\n\n    res2 := cal1(10, 20, add)\n    fmt.Println(res2) \/\/ 30\n}\n<\/code><\/pre>\n<h2>\u4e94 \u53ef\u4ee5\u628a\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c<\/h2>\n<pre><code class=\"language-go\">func do(s string) (func(int, int) int, error) {\n    switch s {\n    case &quot;+&quot;:\n        return add, nil\n    case &quot;-&quot;:\n        return sub, nil\n    default:\n        err := errors.New(&quot;\u65e0\u6cd5\u8bc6\u522b\u7684\u8fd0\u7b97\u7b26&quot;)\n        return nil, err\n    }\n}\n\nfunc main() {\n    f,err:=do(&quot;-&quot;)\n    fmt.Println(f,err)\n    fmt.Println(f(111,222))\n}<\/code><\/pre>\n<h2>\u516d \u7ec3\u4e60<\/h2>\n<p>\u57fa\u4e8e\u51fd\u6570\u662f\u7b2c\u4e00\u7b49\u516c\u6c11\u8fd9\u4e00\u7279\u6027\uff0c\u4f18\u96c5\u5730\u53d6\u4ee3\u591a\u5206\u652fif<\/p>\n<pre><code class=\"language-go\">func login()  {\n    fmt.Println(&quot;\u8981\u60f3\u751f\u6d3b\u8fc7\u5f97\u53bb\uff0c\u8eab\u4e0a\u5fc5\u987b\u80cc\u70b9\u7eff&quot;)\n}\nfunc register()  {\n    fmt.Println(&quot;\u8981\u60f3\u751f\u6d3b\u8fc7\u5f97\u597d\uff0c\u5934\u9876\u5fc5\u987b\u957f\u70b9\u8349&quot;)\n}\nfunc withdraw()  {\n    fmt.Println(&quot;\u6a2a\u6279\uff1a\u7eff\u6bdb\u5c0f\u4e4c\u9f9f&quot;)\n}\n\nvar funcSlice map[string]func()= map[string]func(){\n    &quot;\u767b\u5f55&quot;:login,\n    &quot;\u6ce8\u518c&quot;:register,\n    &quot;\u63d0\u73b0&quot;:withdraw,\n}\n\nfunc main() {\n    \/\/var f func()\n    var fname string\n    fmt.Printf(&quot;\u8bf7\u8f93\u5165\u529f\u80fd\u540d: &quot;)\n    fmt.Scanln(&amp;fname)\n\n    f,ok:=funcSlice[fname]\n    if ok{\n        f()\n    }else {\n        fmt.Println(&quot;\u8f93\u5165\u7684\u529f\u80fd\u4e0d\u5b58\u5728&quot;)\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u53d8\u91cf \u4e00 \u51fd\u6570\u662f\u7b2c\u4e00\u7b49\u516c\u6c11 go\u4e2d\u51fd\u6570\u5728Go\u8bed\u8a00\u4e2d\u5c5e\u4e8e\u201c\u4e00\u7b49\u516c\u6c11\u201d\uff0c\u8bf4\u767d\u4e86\u5c31\u662f\u53ef\u4ee5\u628a\u51fd\u6570\u5f53\u53d8\u91cf\u4e00\u6837\u53bb\u7528\uff0c\u5177 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,450],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/7936"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7936"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/7936\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}