{"id":1918,"date":"2021-08-03T14:13:21","date_gmt":"2021-08-03T06:13:21","guid":{"rendered":"https:\/\/egonlin.com\/?p=1918"},"modified":"2022-02-07T16:44:02","modified_gmt":"2022-02-07T08:44:02","slug":"%e5%87%bd%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=1918","title":{"rendered":"\u51fd\u6570"},"content":{"rendered":"<h1>\u51fd\u6570<\/h1>\n<p>\u200b   \u51fd\u6570\u5c31\u662f\u76db\u653e\u4e00\u7ec4\u4ee3\u7801\u5757\u7684\u201d\u5bb9\u5668\u201d\uff0c\u8be5\u4ee3\u7801\u5757\u5b9e\u73b0\u67d0\u4e2a\u7279\u5b9a\u529f\u80fd\uff0c\u88ab\u7ec4\u7ec7\u5230\u51fd\u6570\u5185\u540e\u53ef\u4ee5\u91cd\u590d\u8c03\u7528<\/p>\n<p>\u200b   Go\u8bed\u8a00\u4e2d\u652f\u6301\u51fd\u6570\u3001\u533f\u540d\u51fd\u6570\u548c\u95ed\u5305\uff0c\u5e76\u4e14\u51fd\u6570\u5728Go\u8bed\u8a00\u4e2d\u5c5e\u4e8e\u201c\u4e00\u7b49\u516c\u6c11\u201d\u3002<\/p>\n<h2>\u4e00 \u51fd\u6570\u7684\u4f7f\u7528<\/h2>\n<h3>1.1 \u5b9a\u4e49<\/h3>\n<p>\u8bed\u6cd5<\/p>\n<pre><code class=\"language-go\">func \u51fd\u6570\u540d(\u53c2\u6570)(\u8fd4\u56de\u503c){\n    \u51fd\u6570\u4f53\n}<\/code><\/pre>\n<p>\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li>1\u3001\u51fd\u6570\u540d\uff1a\u547d\u540d\u89c4\u8303\u540c\u53d8\u91cf\u540d\uff0c\u540c\u4e00\u4e2a\u5305\u5185\u4e0d\u80fd\u91cd\u590d<\/li>\n<li>2\u3001\u53c2\u6570\uff1a\u7531\u53c2\u6570\u540d\u548c\u7c7b\u578b\u7ec4\u6210\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u7528\u9017\u53f7\u5206\u9694\u5f00\u5373\u53ef<\/li>\n<li>3\u3001\u8fd4\u56de\u503c\uff1a\u7531\u53d8\u91cf\u540d\u548c\u7c7b\u578b\u7ec4\u6210\uff0c\u53ef\u4ee5\u53ea\u5199\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u4f46\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u591a\u4e2a\u8fd4\u56de\u503c\u5fc5\u987b\u7528\u9017\u53f7\u5206\u9694\u5f00\u5e76\u4e14\u7528()\u5305\u88f9<\/li>\n<li>4\u3001\u51fd\u6570\u4f53\uff1a\u5177\u4f53\u7684\u529f\u80fd\u5b9e\u73b0<\/li>\n<\/ul>\n<p>\u4f8b1<\/p>\n<pre><code class=\"language-go\">func add(x int,y int) int{\n    return x + y\n}<\/code><\/pre>\n<p>\u4f8b2\uff1a\u53c2\u6570\u4e0e\u8fd4\u56de\u503c\u90fd\u662f\u53ef\u9009\u7684<\/p>\n<pre><code class=\"language-go\">func sayHi(){\n    fmt.Println(&quot;hello egon&quot;)\n}<\/code><\/pre>\n<h3>1.2 \u8c03\u7528<\/h3>\n<p>\u8bed\u6cd5<\/p>\n<pre><code class=\"language-go\">\u51fd\u6570\u540d(\u503c1,\u503c2,\u503c3,...)<\/code><\/pre>\n<p>\u4f8b<\/p>\n<pre><code class=\"language-go\">func main()  {\n    res:=add(1,2)\n    fmt.Println(res)\n\n    add(1,2) \/\/ \u8c03\u7528\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4e0d\u63a5\u6536\u5176\u8fd4\u56de\u503c\u3002\n    sayHi()\n}<\/code><\/pre>\n<h2>\u4e8c \u53c2\u6570<\/h2>\n<p>\u53c2\u6570\u5206\u4e3a\u5f62\u53c2\u4e0e\u5b9e\u53c2\uff0c\u5b9e\u53c2\u6307\u7684\u662f\u8c03\u7528\u51fd\u6570\u65f6\u4f20\u5165\u7684\u503c\uff08\u76f8\u5f53\u4e8e\u53d8\u91cf\u503c\uff09\uff0c\u5f62\u53c2\u6307\u7684\u662f\u5b9a\u4e49\u51fd\u6570\u662f\u6307\u5b9a\u53c2\u6570\uff08\u76f8\u5f53\u4e8e\u53d8\u91cf\u540d\uff09\uff0c\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4f1a\u5c06\u5b9e\u53c2\u503c\u8d4b\u503c\u7ed9\u5f62\u53c2<\/p>\n<h3>2.1 \u7c7b\u578b\u7b80\u5199<\/h3>\n<p>\u51fd\u6570\u7684\u5f62\u53c2\u4e2d\u5982\u679c\u76f8\u90bb\u53d8\u91cf\u7684\u7c7b\u578b\u76f8\u540c\uff0c\u5219\u53ef\u4ee5\u7701\u7565\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-go\">func add(x,y int) int{\n    return x + y\n}<\/code><\/pre>\n<h3>2.2 \u53ef\u53d8\u957f\u53c2\u6570<\/h3>\n<p>\u53ef\u53d8\u957f\u6307\u7684\u5c31\u662f\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4f20\u5165\u7684\u5b9e\u53c2\u503c\u4e2a\u6570\u4e0d\u56fa\u5b9a\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5f62\u53c2\u540dor\u5176\u7c7b\u578b\u524d\u52a0\u2026\uff0c\u8be5\u5f62\u53c2\u5c31\u53ef\u4ee5\u7528\u6765\u63a5\u6536\u6ea2\u51fa\u7684\u5b9e\u53c2\u503c\uff0c\u5e76\u628a\u5b83\u4eec\u5b58\u6210\u4e00\u4e2a\u5207\u7247\uff0c\u79f0\u4e4b\u4e3a\u53ef\u53d8\u957f\u53c2\u6570\uff0c\u672c\u8d28\u4e0a\uff0c\u51fd\u6570\u7684\u53ef\u53d8\u53c2\u6570\u662f\u901a\u8fc7\u5207\u7247\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u53ef\u53d8\u53c2\u6570\u901a\u5e38\u8981\u4f5c\u4e3a\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u4f8b<\/p>\n<pre><code class=\"language-go\">func add(x ...int) int {\n    fmt.Println(x)  \/\/ x\u662f\u4e00\u4e2a\u5207\u7247\n    sum := 0\n    for _, v := range x {\n        sum += v\n    }\n    return sum\n}<\/code><\/pre>\n<p>\u8c03\u7528<\/p>\n<pre><code class=\"language-go\">func main() {\n    res1 := add(1, 2, 3)\n    res2 := add(1, 2, 3, 4, 5)\n    res3 := add(1, 2, 3, 4, 5, 6)\n    fmt.Println(res1, res2, res3)  \/\/ 6 15 21\n\n}<\/code><\/pre>\n<p>\u56fa\u5b9a\u53c2\u6570\u642d\u914d\u53ef\u53d8\u53c2\u6570\u4f7f\u7528\u65f6\uff0c\u53ef\u53d8\u53c2\u6570\u8981\u653e\u5728\u56fa\u5b9a\u53c2\u6570\u7684\u540e\u9762\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-go\">func add(x int,y ...int) int {\n    fmt.Println(x,y)\n    sum := 0\n    for _, v := range y {\n        sum += v\n    }\n    return sum\n}<\/code><\/pre>\n<h2>\u4e09 \u8fd4\u56de\u503c<\/h2>\n<h3>3.1 \u591a\u4e2a\u8fd4\u56de\u503c<\/h3>\n<p>Go\u8bed\u8a00\u4e2d\u51fd\u6570\u652f\u6301\u591a\u8fd4\u56de\u503c\uff0c\u51fd\u6570\u5982\u679c\u9700\u8981\u6709\u591a\u4e2a\u8fd4\u56de\u503c\uff0c\u6211\u4eec\u5728\u5b9a\u4e49\u51fd\u6570\u7684\u8fd4\u56de\u503c\u65f6\uff0c\u5fc5\u987b\u7528<code>()<\/code>\u5c06\u6240\u6709\u8fd4\u56de\u503c\u5305\u88f9\u8d77\u6765\u3002<\/p>\n<pre><code class=\"language-go\">func divmod(x, y int) (int, int) {\n    res1 := x \/ y\n    res2 := x % y\n    return res1, res2\n}<\/code><\/pre>\n<h3>3.2 \u547d\u540d\u8fd4\u56de\u503c<\/h3>\n<pre><code class=\"language-go\">func divmod(x, y int) (res1,res2 int) {\n    res1 = x \/ y  \/\/ \u6b64\u5904\u7528\u7684\u662f\u8d4b\u503c\u64cd\u4f5c\uff0c\u800c\u975e\u58f0\u660eres1\u7684\u64cd\u4f5c\n    res2 = x % y\n    return\n}<\/code><\/pre>\n<h3>3.3 \u8fd4\u56de\u503c\u8865\u5145<\/h3>\n<p>\u5f53\u6211\u4eec\u7684\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e3aslice\u65f6\uff0cnil\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u6709\u6548\u7684slice\uff0c\u6ca1\u5fc5\u8981\u663e\u793a\u8fd4\u56de\u4e00\u4e2a\u957f\u5ea6\u4e3a0\u7684\u5207\u7247\u3002<\/p>\n<pre><code class=\"language-go\">func someFunc(x string) []int {\n    if x == &quot;&quot; {\n        return nil \/\/ \u6ca1\u5fc5\u8981\u8fd4\u56de[]int{}\n    }\n    ...\n}<\/code><\/pre>\n<h2>\u56db \u4f5c\u7528\u57df<\/h2>\n<h3>4.1 \u4ec0\u4e48\u662f\u4f5c\u7528\u57df<\/h3>\n<p>\u4f5c\u7528\u57df\u6307\u7684\u662f\u58f0\u660e\u8bed\u53e5\u7684\u4f5c\u7528\u57df\u3002\u58f0\u660e\u8bed\u53e5\u5982\u53d8\u91cf\u7684\u58f0\u660e\u3001\u51fd\u6570\u7684\u58f0\u660e\u7b49\uff0c\u90fd\u662f\u5c06\u4e00\u4e2a\u5b9e\u4f53\u7ed1\u5b9a\u7ed9\u4e00\u4e2a\u540d\u5b57\uff0c\u800c\u58f0\u660e\u8bed\u53e5\u7684\u4f5c\u7528\u57df\u6307\u7684\u5c31\u662f\u5728\u6e90\u4ee3\u7801\u4e2d\u53ef\u4ee5\u6709\u6548\u4f7f\u7528\u8be5\u540d\u5b57\u7684\u8303\u56f4<\/p>\n<p>\u4f5c\u7528\u57df VS \u58f0\u660e\u5468\u671f<\/p>\n<ul>\n<li>1\u3001\u58f0\u660e\u8bed\u53e5\u7684\u4f5c\u7528\u57df\u5bf9\u5e94\u7684\u662f\u4e00\u4e2a\u6e90\u4ee3\u7801\u7684\u6587\u672c\u533a\u57df\uff0c\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u7684\u5c5e\u6027\u3002<\/li>\n<li>2\u3001\u58f0\u660e\u8bed\u53e5\u521b\u9020\u4e00\u4e2a\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u6307\u7684\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u53d8\u91cf\u5b58\u5728\u7684\u6709\u6548\u65f6\u95f4\u6bb5\uff0c\u5728\u6b64\u65f6\u95f4\u6bb5\u5185\u8be5\u53d8\u91cf\u662f\u53ef\u4ee5\u88ab\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u5f15\u7528\u7684\uff0c\u6240\u4ee5\u8bf4\u58f0\u660e\u5468\u671f\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\u7684\u6982\u5ff5<\/li>\n<\/ul>\n<h3>4.2 \u5982\u4f55\u533a\u5206\u4f5c\u7528\u57df\u8303\u56f4<\/h3>\n<h4>\uff08\u4e00\uff09\u8bed\u6cd5\u5757\u4e0e\u8bcd\u6cd5\u57df<\/h4>\n<p>\u72ed\u4e49\u5730\u8bb2\uff0c\u6211\u4eec\u901a\u5e38\u8ba4\u4e3a\u7528\u82b1\u62ec\u53f7{}\u5305\u542b\u7684\u4e00\u7cfb\u5217\u8bed\u53e5\u79f0\u4e4b\u4e3a\u4e00\u4e2a\u8bed\u6cd5\u5757\uff0c\u5e7f\u4e49\u5730\u8bb2\uff0cGo\u8bed\u8a00\u4e2d\u6709\u4ee5\u4e0b\u51e0\u79cd\u8bed\u6cd5\u5757<\/p>\n<ul>\n<li>1\u3001\u6574\u4e2a\u6e90\u4ee3\u7801\u5c31\u662f\u4e00\u4e2a\u5927\u7684\u4ee3\u7801\u5757-\u300b\u5168\u5c40\u8bed\u6cd5\u5757<\/li>\n<li>2\u3001\u4e00\u4e2a\u5305\u7684\u6240\u6709\u4ee3\u7801=\u300b\u5305\u8bed\u6cd5\u5757<\/li>\n<li>3\u3001\u6bcf\u4e2afor\u3001if\u548cswitch\u8bed\u53e5\u7684\u8bed\u6cd5\u51b3<\/li>\n<li>4\u3001\u6bcf\u4e2aswitch\u6216select\u7684\u5206\u652f\u4e5f\u6709\u72ec\u7acb\u7684\u8bed\u6cd5\u51b3\uff1b<\/li>\n<li>5\u3001\u5f53\u7136\u4e5f\u5305\u62ec\u663e\u5f0f\u4e66\u5199\u7684\u8bed\u6cd5\u5757\uff08\u82b1\u62ec\u5f27\u5305\u542b\u7684\u8bed\u53e5\uff09\u3002<\/li>\n<\/ul>\n<p>\u8bed\u6cd5\u51b3\u5b9a\u4e86\u5176\u5185\u90e8\u58f0\u660e\u7684\u540d\u5b57\u6240\u5728\u7684\u4f5c\u7528\u57df\uff0c\u8fd9\u4e0e\u540d\u5b57\u7684\u67e5\u627e\u4f18\u5148\u7ea7\u7d27\u5bc6\u76f8\u5173\uff0cwhat\uff1f\uff1f\uff1f<\/p>\n<p>\u58f0\u660e\u8bed\u53e5\u6240\u5728\u7684\u8bed\u6cd5\u5757\u79f0\u4e4b\u4e3a\u5b83\u7684\u8bcd\u6cd5\u57df\uff0c\u5373\u540d\u5b57\u5b9a\u4e49\u7684\u5730\u65b9\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5c42\u5c42\u5d4c\u5957\u7684\u8bcd\u6cd5\u57df\u662f\u4ee5\u5b9a\u4e49\u9636\u6bb5\u4e3a\u51c6\u751f\u6210\u7684\uff0c\u800c\u5728Go\u8bed\u8a00\u4e2d\uff0c\u91c7\u7528\u7684\u5c31\u662f\u8bcd\u6cd5\u4f5c\u7528\u57df\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u5b9a\u4e49\u4f4d\u7f6e\u51b3\u5b9a\u4e86\u4f5c\u7528\u57df\u7684\u5d4c\u5957\u5173\u7cfb\u800c\u4e0e\u5f15\u7528\u4f4d\u7f6e\u65e0\u5173\uff0c\u5373\u65e0\u8bba\u5728\u4f55\u5904\u5f15\u7528\/\u67e5\u627e\u540d\u5b57\uff0c\u90fd\u9700\u8981\u8001\u8001\u8001\u5b9e\u5b9e\u5730\u56de\u5230\u8be5\u540d\u5b57\u5f53\u521d\u5b9a\u4e49\u7684\u5730\u65b9\u53bb\u627e\u4f5c\u7528\u57df\u5173\u7cfb\uff0c\u7531\u5185\u5411\u5916\u4e00\u5c42\u5c42\u67e5\u627e<\/p>\n<p>\u4e86\u89e3\uff1a\u8bcd\u6cd5\u57df\u4e0e\u52a8\u6001\u4f5c\u7528\u57df<\/p>\n<pre><code class=\"language-go\">\u7f16\u8bd1\u5668\u7684\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u9636\u6bb5\u53eb\u505a\u8bcd\u6cd5\u5316\uff0c\u4e5f\u53eb\u5355\u8bcd\u5316\uff0c\u6240\u4ee5\u8bf4\uff0c\u8bcd\u6cd5\u4f5c\u7528\u57df\u662f\u5728\u8bcd\u6cd5\u9636\u6bb5\u751f\u6210\u7684\u4f5c\u7528\u57df\uff0c\u662f\u4e00\u4e2a\u7f16\u8bd1\u65f6\u7684\u6982\u5ff5\u3002\u4e0e\u8bcd\u6cd5\u57df\u76f8\u5bf9\u5e94\u7684\u662f\u52a8\u6001\u4f5c\u7528\u57df\uff0c\u4e8c\u8005\u5bf9\u6bd4\u5982\u4e0b\n\/\/1\u3001\u8bcd\u6cd5\u4f5c\u7528\u57df\uff08\u53c8\u79f0\u9759\u6001\u4f5c\u7528\u57df\uff09\u662f\u5728\u4e66\u5199\u4ee3\u7801\u6216\u8005\u8bf4\u5b9a\u4e49\u65f6\u786e\u5b9a\u7684\uff0c\n\/\/2\u3001\u800c\u52a8\u6001\u4f5c\u7528\u57df\u662f\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u7684\u3002\n\/\/3\u3001\u8bcd\u6cd5\u4f5c\u7528\u57df\u5173\u6ce8\u51fd\u6570\u5728\u4f55\u5904\u58f0\u660e\uff0c\u800c\u52a8\u6001\u4f5c\u7528\u57df\u5173\u6ce8\u51fd\u6570\u4ece\u4f55\u5904\u8c03\u7528\uff0c\u5176\u4f5c\u7528\u57df\u94fe\u662f\u57fa\u4e8e\u8fd0\u884c\u65f6\u7684\u8c03\u7528\u6808\u7684<\/code><\/pre>\n<h4>\uff08\u4e8c\uff09\u8bcd\u6cd5\u4f5c\u7528\u57df<\/h4>\n<p>\u8bcd\u6cd5\u4f5c\u7528\u57df\uff0c\u6216\u7b80\u79f0\u4f5c\u7528\u57df\uff0c\u6709\u5982\u4e0b\u51e0\u79cd<\/p>\n<p><strong>\uff081\uff09\u5168\u5c40\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5185\u7f6e\u7684\u7c7b\u578b\u3001\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u6bd4\u5982int\u3001len\u548ctrue\u7b49\u662f\u5728\u5168\u5c40\u4f5c\u7528\u57df\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p><strong>\uff082\uff09\u5305\u7ea7\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u4efb\u4f55\u5728\u5728\u51fd\u6570\u5916\u90e8\uff08\u4e5f\u5c31\u662f\u5305\u7ea7\u8bcd\u6cd5\u57df\uff09\u58f0\u660e\u7684\u540d\u5b57\u662f\u5728\u5305\u7ea7\u4f5c\u7528\u57df\uff0c\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u5305\u7684\u4efb\u4f55\u6e90\u6587\u4ef6\u4e2d\u8bbf\u95ee<\/p>\n<p><strong>\uff083\uff09\u6e90\u6587\u4ef6\u7ea7\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5bfc\u5165\u7684\u5305\uff0c\u4f8b\u5982\u5728\u5f53\u524d\u6587\u4ef6\u4e2d\u5bfc\u5165\u7684fmt\u5305\uff0c\u5219\u662f\u5bf9\u5e94\u6e90\u6587\u4ef6\u7ea7\u7684\u4f5c\u7528\u57df\uff0c\u56e0\u6b64\u53ea\u80fd\u5728\u5f53\u524d\u7684\u6587\u4ef6\u4e2d\u8bbf\u95ee\u5bfc\u5165\u7684fmt\u5305\uff0c\u5373\u4fbf\u662f\u5f53\u524d\u5305\u7684\u5176\u5b83\u6e90\u6587\u4ef6\u4e5f\u65e0\u6cd5\u8bbf\u95ee\u5728\u5f53\u524d\u6e90\u6587\u4ef6\u5bfc\u5165\u7684\u5305\uff0c\u82e5\u4e5f\u60f3\u4f7f\u7528\u5305fmt\uff0c\u9700\u8981\u91cd\u65b0\u5bfc\u5165\u624d\u884c<\/p>\n<p><strong>\uff084\uff09\u5c40\u90e8\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u51fd\u6570\u5185\u90e8\u7684\u540d\u5b57\uff0c\u53ea\u80fd\u5728\u51fd\u6570\u5185\u4f7f\u7528<\/p>\n<p><strong>\uff085\uff09\u51fd\u6570\u5185\u5d4c\u5957\u7684\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u51fd\u6570\u5185\u53ef\u4ee5\u518d\u6709{}\uff0c\u53ef\u4ee5\u662ffor\u3001if\u3001switch\u7b49{}\uff0c{}\u5185\u5b9a\u4e49\u7684\u540d\u5b57\u53ea\u80fd\u5728{}\u5185\u4f7f\u7528\uff0c<\/p>\n<p>\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u540c\u540d\u7684\u58f0\u660e\uff0c\u53ea\u8981\u5b83\u4eec\u5728\u4e0d\u540c\u7684\u8bcd\u6cd5\u57df\u5c31\u6ca1\u6709\u5173\u7cfb\uff0c\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u548c\u5305\u7ea7\u7684\u53d8\u91cf\u540c\u540d\u3002\u5f53\u7f16\u8bd1\u5668\u9047\u5230\u4e00\u4e2a\u540d\u5b57\u5f15\u7528\u65f6\uff0c\u5982\u679c\u5b83\u770b\u8d77\u6765\u50cf\u4e00\u4e2a\u58f0\u660e\uff0c\u5b83\u9996\u5148\u4ece\u6700\u5185\u5c42\u7684\u8bcd\u6cd5\u57df\u5411\u5168\u5c40\u7684\u4f5c\u7528\u57df\u67e5\u627e\u3002\u5982\u679c\u67e5\u627e\u5931\u8d25\uff0c\u5219\u62a5\u544a\u201c\u672a\u58f0\u660e\u7684\u540d\u5b57\u201d\u8fd9\u6837\u7684\u9519\u8bef\u3002\u5982\u679c\u8be5\u540d\u5b57\u5728\u5185\u90e8\u548c\u5916\u90e8\u7684\u5757\u5206\u522b\u58f0\u660e\u8fc7\uff0c\u5219\u5185\u90e8\u5757\u7684\u58f0\u660e\u9996\u5148\u88ab\u627e\u5230\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5185\u90e8\u58f0\u660e\u5c4f\u853d\u4e86\u5916\u90e8\u540c\u540d\u7684\u58f0\u660e\uff0c\u8ba9\u5916\u90e8\u7684\u58f0\u660e\u7684\u540d\u5b57\u65e0\u6cd5\u88ab\u8bbf\u95ee\u3002<\/p>\n<p>\u4f46\u662f\u7269\u6781\u5fc5\u53cd\uff0c\u5982\u679c\u6ee5\u7528\u4e0d\u540c\u8bcd\u6cd5\u57df\u53ef\u91cd\u540d\u7684\u7279\u6027\u7684\u8bdd\uff0c\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5f88\u96be\u9605\u8bfb\uff0cgo\u8bed\u8a00\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a\u3002<\/p>\n<h4>\uff08\u4e09\uff09\u9690\u5f0f\u8bcd\u6cd5\u4f5c\u7528\u57df\u4e0e\u663e\u5f0f\u8bcd\u6cd5\u4f5c\u7528\u57df<\/h4>\n<pre><code class=\"language-go\">func main() {\n    \/\/ 1\u3001\u6b64\u5904\u662f\u51fd\u6570\u4f53\u8bcd\u6cd5\u57df=&gt;\u663e\u5f0f\u7684\n    for i := 0; i &lt; len(x); i++ { \/\/ 2\u3001\u672c\u884c\u4ee3\u7801\u662f\u521d\u59cb\u5316\u8bcd\u6cd5\u57df=&gt;\u9690\u5f0f\u7684\n        \/\/ 3\u3001\u6b64\u5904\u662f\u5faa\u73af\u4f53\u8bcd\u6cd5\u57df=&gt;\u663e\u5f0f\u7684\n    }\n}<\/code><\/pre>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u540c\u6837\u6709\u4e09\u4e2a\u4e0d\u540c\u7684x\u53d8\u91cf\uff0c\u6bcf\u4e2a\u58f0\u660e\u5728\u4e0d\u540c\u7684\u8bcd\u6cd5\u57df\uff0c\u4e00\u4e2a\u5728\u51fd\u6570\u4f53\u8bcd\u6cd5\u57df\uff0c\u4e00\u4e2a\u5728for\u9690\u5f0f\u7684\u521d\u59cb\u5316\u8bcd\u6cd5\u57df\uff0c\u4e00\u4e2a\u5728for\u5faa\u73af\u4f53\u8bcd\u6cd5\u57df\uff1b\u53ea\u6709\u4e24\u4e2a\u5757\u662f\u663e\u5f0f\u521b\u5efa\u7684\uff1a<\/p>\n<pre><code class=\"language-go\">func main() {\n    x := &quot;hello&quot;  \/\/ 1\u3001\u51fd\u6570\u4f53\u8bcd\u6cd5\u57df=&gt;\u663e\u5f0f\u7684\n    for _, x := range x {  \/\/ 2\u3001\u672c\u884c\u4ee3\u7801\u662f\u521d\u59cb\u5316\u8bcd\u6cd5\u57df=&gt;\u9690\u5f0f\u7684\n        \/\/ 3\u3001\u6b64\u5904\u662f\u5faa\u73af\u4f53\u8bcd\u6cd5\u57df=&gt;\u663e\u5f0f\u7684\n        x := x + &#039;A&#039; - &#039;a&#039;\n        fmt.Printf(&quot;%c&quot;, x) \/\/ &quot;HELLO&quot; (one letter per iteration)\n    }\n}<\/code><\/pre>\n<p>\u548cfor\u5faa\u73af\u7c7b\u4f3c\uff0cif\u548cswitch\u8bed\u53e5\u4e5f\u4f1a\u5728\u6761\u4ef6\u90e8\u5206\u521b\u5efa\u9690\u5f0f\u8bcd\u6cd5\u57df\uff0c\u8fd8\u6709\u5b83\u4eec\u5bf9\u5e94\u7684\u6267\u884c\u4f53\u8bcd\u6cd5\u57df\u3002\u4e0b\u9762\u7684if-else\u6d4b\u8bd5\u94fe\u6f14\u793a\u4e86x\u548cy\u7684\u6709\u6548\u4f5c\u7528\u57df\u8303\u56f4\uff1a<\/p>\n<pre><code class=\"language-go\">if x := 111; x == 0 {\n    fmt.Println(x)\n} else if y := g(x); x == y {\n    fmt.Println(x, y)\n} else {\n    fmt.Println(x, y)\n}\nfmt.Println(x, y) \/\/ compile error: x and y are not visible here<\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2aif\u8bed\u53e5\u5d4c\u5957\u5728\u7b2c\u4e00\u4e2a\u5185\u90e8\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2aif\u8bed\u53e5\u6761\u4ef6\u521d\u59cb\u5316\u8bcd\u6cd5\u57df\u58f0\u660e\u7684\u53d8\u91cf\u5728\u7b2c\u4e8c\u4e2aif\u4e2d\u4e5f\u53ef\u4ee5\u8bbf\u95ee\u3002<\/p>\n<p>\u61c2\u4e86\u4e0a\u4f8b\u4e4b\u540e\uff0c\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<pre><code class=\"language-Go\">if f, err := os.Open(fname); err != nil { \/\/ compile error: unused: f\n    return err\n}\nf.ReadByte() \/\/ compile error: undefined f\nf.Close()    \/\/ compile error: undefined f\n\n\/\/ \u53d8\u91cff\u7684\u4f5c\u7528\u57df\u53ea\u6709\u5728if\u8bed\u53e5\u5185\uff0c\u56e0\u6b64\u540e\u9762\u7684\u8bed\u53e5\u5c06\u65e0\u6cd5\u5f15\u5165\u5b83\uff0c\u8fd9\u5c06\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef<\/code><\/pre>\n<p>\u901a\u5e38\u9700\u8981\u5728if\u4e4b\u524d\u58f0\u660e\u53d8\u91cf\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u540e\u9762\u7684\u8bed\u53e5\u4f9d\u7136\u53ef\u4ee5\u8bbf\u95ee\u53d8\u91cf\uff1a<\/p>\n<pre><code class=\"language-Go\">f, err := os.Open(fname)\nif err != nil {\n    return err\n}\nf.ReadByte()\nf.Close()<\/code><\/pre>\n<p>\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u901a\u8fc7\u5c06ReadByte\u548cClose\u79fb\u52a8\u5230if\u7684else\u5757\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<pre><code class=\"language-Go\">if f, err := os.Open(fname); err != nil {\n    return err\n} else {\n    \/\/ f and err are visible here too\n    f.ReadByte()\n    f.Close()\n}<\/code><\/pre>\n<p>\u4f46\u8fd9\u4e0d\u662fGo\u8bed\u8a00\u63a8\u8350\u7684\u505a\u6cd5\uff0cGo\u8bed\u8a00\u7684\u4e60\u60ef\u662f\u5728if\u4e2d\u5904\u7406\u9519\u8bef\u7136\u540e\u76f4\u63a5\u8fd4\u56de\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6b63\u5e38\u6267\u884c\u7684\u8bed\u53e5\u4e0d\u9700\u8981\u4ee3\u7801\u7f29\u8fdb\u3002<\/p>\n<p>\u6b64\u5916\uff0cswitch\u8bed\u53e5\u7684\u6bcf\u4e2a\u5206\u652f\u4e5f\u6709\u7c7b\u4f3c\u7684\u8bcd\u6cd5\u57df\u89c4\u5219\uff1a\u6761\u4ef6\u90e8\u5206\u4e3a\u4e00\u4e2a\u9690\u5f0f\u8bcd\u6cd5\u57df\uff0c\u7136\u540e\u6bcf\u4e2a\u662f\u6bcf\u4e2a\u5206\u652f\u7684\u8bcd\u6cd5\u57df\u3002<\/p>\n<h4>\uff08\u56db\uff09\u5305\u7ea7\u4f5c\u7528\u57df\u4e0e\u58f0\u660e\u987a\u5e8f<\/h4>\n<p>\u5728\u5305\u7ea7\u522b\uff0c\u58f0\u660e\u7684\u987a\u5e8f\u5e76\u4e0d\u4f1a\u5f71\u54cd\u4f5c\u7528\u57df\u8303\u56f4\uff0c\u56e0\u6b64\u4e00\u4e2a\u5148\u58f0\u660e\u7684\u53ef\u4ee5\u5f15\u7528\u5b83\u81ea\u8eab\u6216\u8005\u662f\u5f15\u7528\u540e\u9762\u7684\u4e00\u4e2a\u58f0\u660e\uff0c\u8fd9\u53ef\u4ee5\u8ba9\u6211\u4eec\u5b9a\u4e49\u4e00\u4e9b\u76f8\u4e92\u5d4c\u5957\u6216\u9012\u5f52\u7684\u7c7b\u578b\u6216\u51fd\u6570\u3002\u4f46\u662f\u5982\u679c\u4e00\u4e2a\u53d8\u91cf\u6216\u5e38\u91cf\u9012\u5f52\u5f15\u7528\u4e86\u81ea\u8eab\uff0c\u5219\u4f1a\u4ea7\u751f\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n<pre><code class=\"language-go\">func main() {\n    sayHi()\n}\n\n\/\/ \u540e\u5b9a\u4e49\u7684\u51fd\u6570\nfunc sayHi() {\n    fmt.Println(&quot;hello&quot;, name)\n}\n\n\/\/ \u540e\u5b9a\u4e49\u7684\u53d8\u91cf\nvar name = &quot;egon&quot;\n\n\/\/ \u9012\u5f52\u5f15\u7528\u4e86\u81ea\u8eab\n\/\/ var x = x  \/\/ \u7f16\u8bd1\u9519\u8bef<\/code><\/pre>\n<h4>\uff08\u4e94\uff09\u6ce8\u610f\u77ed\u53d8\u91cf\u58f0\u660e\u8bed\u53e5\u7684\u4f5c\u7528\u8303\u56f4<\/h4>\n<p>\u8981\u7279\u522b\u6ce8\u610f\u77ed\u53d8\u91cf\u58f0\u660e\u8bed\u53e5\u7684\u4f5c\u7528\u57df\u8303\u56f4\uff0c\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e86\uff1a\u83b7\u53d6\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u7136\u540e\u4fdd\u5b58\u5230\u4e00\u4e2a\u5305\u7ea7\u7684\u53d8\u91cf\u4e2d\u3002\u8fd9\u53ef\u4ee5\u672c\u6765\u53ef\u4ee5\u5728\u5305\u7ea7\u76f4\u63a5\u8c03\u7528os.Getwd\u5b8c\u6210\uff0c\u4f46\u662f\u5c06\u8fd9\u4e2a\u4ece\u4e3b\u903b\u8f91\u4e2d\u5206\u79bb\u51fa\u6765\u653e\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u5185\u53ef\u80fd\u4f1a\u66f4\u597d\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5904\u7406\u9519\u8bef\u7684\u65f6\u5019\uff0c\u4e8e\u662f\u6211\u4eec\u7f16\u5199\u4e86\u4e0b\u8ff0\u4ee3\u7801<\/p>\n<pre><code class=\"language-go\">var cwd string\n\nfunc init() {\n    cwd, err := os.Getwd() \/\/ \u6b64\u65f6\u662f\u58f0\u660e\u8bed\u53e5\uff0ccompile error: unused: cwd\n    if err != nil {\n        log.Fatalf(&quot;os.Getwd failed: %v&quot;, err)\n    }\n}<\/code><\/pre>\n<p>\u867d\u7136cwd\u5728\u5916\u90e8\u5df2\u7ecf\u58f0\u660e\u8fc7\uff0c\u4f46\u662f<code>:=<\/code>\u8bed\u53e5\u8fd8\u662f\u5c06cwd\u548cerr\u91cd\u65b0\u58f0\u660e\u4e3a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u56e0\u4e3a\u5185\u90e8\u58f0\u660e\u7684cwd\u5c06\u5c4f\u853d\u5916\u90e8\u7684\u58f0\u660e\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u4ee3\u7801\u5e76\u4e0d\u4f1a\u6b63\u786e\u66f4\u65b0\u5305\u7ea7\u58f0\u660e\u7684cwd\u53d8\u91cf\u3002<\/p>\n<p>\u7531\u4e8e\u5f53\u524d\u7684\u7f16\u8bd1\u5668\u4f1a\u68c0\u6d4b\u5230\u5c40\u90e8\u58f0\u660e\u7684cwd\u5e76\u6ca1\u6709\u672c\u4f7f\u7528\uff0c\u7136\u540e\u62a5\u544a\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u9519\u8bef\uff0c\u4f46\u662f\u8fd9\u79cd\u68c0\u6d4b\u5e76\u4e0d\u53ef\u9760\u3002\u56e0\u4e3a\u4e00\u4e9b\u5c0f\u7684\u4ee3\u7801\u53d8\u66f4\uff0c\u4f8b\u5982\u589e\u52a0\u4e00\u4e2a\u5c40\u90e8cwd\u7684\u6253\u5370\u8bed\u53e5\uff0c\u5c31\u53ef\u80fd\u5bfc\u81f4\u8fd9\u79cd\u68c0\u6d4b\u5931\u6548\u3002<\/p>\n<pre><code class=\"language-go\">var cwd string\n\nfunc init() {\n    cwd, err := os.Getwd() \/\/ \u6b64\u65f6\u662f\u58f0\u660e\u8bed\u53e5\uff0ccompile error: unused: cwd\n    if err != nil {\n        log.Fatalf(&quot;os.Getwd failed: %v&quot;, err)\n    }\n    log.Printf(&quot;Working directory = %s&quot;, cwd)\n}<\/code><\/pre>\n<p>\u4f46\u6b64\u65f6\u5168\u5c40\u7684cwd\u53d8\u91cf\u4f9d\u7136\u662f\u6ca1\u6709\u88ab\u6b63\u786e\u521d\u59cb\u5316\u7684\uff0c\u800c\u4e14\u770b\u4f3c\u6b63\u5e38\u7684\u65e5\u5fd7\u8f93\u51fa\u66f4\u662f\u8ba9\u8fd9\u4e2aBUG\u66f4\u52a0\u9690\u6666\u3002<\/p>\n<p>\u6709\u8bb8\u591a\u65b9\u5f0f\u53ef\u4ee5\u907f\u514d\u51fa\u73b0\u7c7b\u4f3c\u6f5c\u5728\u7684\u95ee\u9898\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u5355\u72ec\u58f0\u660eerr\u53d8\u91cf\uff0c\u6765\u907f\u514d\u4f7f\u7528<code>:=<\/code>\u7684\u7b80\u77ed\u58f0\u660e\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-go\">var cwd string\n\nfunc init() {\n    var err error\n    cwd, err = os.Getwd()  \/\/ \u6b64\u65f6\u5c31\u5b8c\u5168\u6210\u4e86\u8d4b\u503c\u8bed\u53e5\uff0c\u5c31\u662f\u66f4\u65b0\u4e86\u5305\u7ea7\u7684cwd\n    if err != nil {\n        log.Fatalf(&quot;os.Getwd failed: %v&quot;, err)\n    }\n}<\/code><\/pre>\n<h4>\uff08\u516d\uff09\u7ec3\u4e60<\/h4>\n<p>\u7ec3\u4e601\uff1a\u4e0b\u8ff0\u4ee3\u7801\u7ed3\u679c\u4e3a\uff1f<\/p>\n<pre><code class=\"language-go\">var x int = 111\n\nfunc main() {\n    x=3333\n    say()\n}\n\nfunc say() {\n    fmt.Println(x)\n}<\/code><\/pre>\n<p>\u7ec3\u4e602\uff1a\u4e0b\u8ff0\u4ee3\u7801\u7ed3\u679c\u4e3a\uff1f<\/p>\n<pre><code class=\"language-go\">var x int = 111\n\nfunc main() {\n    x:=222\n    {\n        x:=333\n        {\n            x:=444\n            fmt.Println(x)\n        }\n        fmt.Println(x)\n    }\n    fmt.Println(x)\n\n    say()\n}\n\nfunc say() {\n    fmt.Println(x)\n}<\/code><\/pre>\n<h2>\u4e94 \u51fd\u6570\u7c7b\u578b\u4e0e\u53d8\u91cf<\/h2>\n<h3>5.1 \u5b9a\u4e49\u51fd\u6570\u7c7b\u578b<\/h3>\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<h3>5.2 \u51fd\u6570\u7c7b\u578b\u7684\u53d8\u91cf<\/h3>\n<p>\u6211\u4eec\u53ef\u4ee5\u58f0\u660e\u51fd\u6570\u7c7b\u578b\u7684\u53d8\u91cf\u5e76\u4e14\u4e3a\u8be5\u53d8\u91cf\u8d4b\u503c\uff1a<\/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<h2>\u516d \u9ad8\u9636\u51fd\u6570<\/h2>\n<p>\u9ad8\u9636\u51fd\u6570\u6307\u7684\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<h3>6.1 \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/h3>\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<h3>6.2 \u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c<\/h3>\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>\u4e03 \u533f\u540d\u51fd\u6570\u548c\u95ed\u5305<\/h2>\n<h3>7.1 \u533f\u540d\u51fd\u6570<\/h3>\n<p>\u533f\u540d\u51fd\u6570\u5c31\u662f\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u4e34\u65f6\u4f7f\u7528\u4e00\u6b21\u7684\u573a\u666f<\/p>\n<pre><code class=\"language-go\">func(\u53c2\u6570)(\u8fd4\u56de\u503c){\n    \u51fd\u6570\u4f53\n}<\/code><\/pre>\n<p>\u533f\u540d\u51fd\u6570\u56e0\u4e3a\u6ca1\u6709\u51fd\u6570\u540d\uff0c\u6240\u4ee5\u6ca1\u529e\u6cd5\u50cf\u666e\u901a\u51fd\u6570\u90a3\u6837\u8c03\u7528\uff0c\u533f\u540d\u51fd\u6570\u9700\u8981\u4fdd\u5b58\u5230\u67d0\u4e2a\u53d8\u91cf\u6216\u8005\u4f5c\u4e3a\u7acb\u5373\u6267\u884c\u51fd\u6570:<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/ \u5c06\u533f\u540d\u51fd\u6570\u4fdd\u5b58\u5230\u53d8\u91cf\n    add := func(x, y int) {\n        fmt.Println(x + y)\n    }\n    add(10, 20) \/\/ \u901a\u8fc7\u53d8\u91cf\u8c03\u7528\u533f\u540d\u51fd\u6570\n\n    \/\/\u81ea\u6267\u884c\u51fd\u6570\uff1a\u533f\u540d\u51fd\u6570\u5b9a\u4e49\u5b8c\u52a0()\u76f4\u63a5\u6267\u884c\n    func(x, y int) {\n        fmt.Println(x + y)\n    }(10, 20)\n}<\/code><\/pre>\n<p>\u533f\u540d\u51fd\u6570\u591a\u7528\u4e8e\u5b9e\u73b0\u56de\u8c03\u51fd\u6570\u548c\u95ed\u5305\u3002<\/p>\n<h3>7.2 \u95ed\u5305<\/h3>\n<p>\u95ed\u5305\u51fd\u6570=\u95ed\u51fd\u6570+\u5305\u51fd\u6570<\/p>\n<p>\u95ed\uff1a\u6307\u7684\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u51fd\u6570\uff0c\u6ce8\u610f\uff0c\u5728Go\u8bed\u8a00\u4e2d\uff0c\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u51fd\u6570\u5fc5\u987b\u662f\u533f\u540d\u51fd\u6570<\/p>\n<p>\u5305\uff1a\u95ed\u51fd\u6570\u5305\u542b\u4e86\u5bf9\u5916\u5c42\u51fd\u6570\u540d\u5b57\u7684\u5f15\u7528<\/p>\n<pre><code class=\"language-go\">func counter(start int) func(int) int{\n    f:= func(n int) int{\n        start += n  \/\/ \u4fee\u6539\u5916\u5c42\u51fd\u6570\u7684\u53d8\u91cfstart\n        return start\n    }\n    return f\n}\n\nfunc main() {\n    c:=counter(0)\n    fmt.Println(c(1)) \/\/ 1\n    fmt.Println(c(1)) \/\/ 2\n    fmt.Println(c(1)) \/\/ 3\n}<\/code><\/pre>\n<p>\u95ed\u5305\u5e94\u7528\u793a\u4f8b1<\/p>\n<pre><code class=\"language-go\">func makeSuffixFunc(suffix string) func(string) string {\n    return func(name string) string {\n        if !strings.HasSuffix(name, suffix) {\n            return name + suffix\n        }\n        return name\n    }\n}\n\nfunc main() {\n    jpgFunc := makeSuffixFunc(&quot;.jpg&quot;)\n    txtFunc := makeSuffixFunc(&quot;.txt&quot;)\n    fmt.Println(jpgFunc(&quot;test&quot;)) \/\/test.jpg\n    fmt.Println(txtFunc(&quot;test&quot;)) \/\/test.txt\n}<\/code><\/pre>\n<p>\u95ed\u5305\u5e94\u7528\u793a\u4f8b2<\/p>\n<pre><code class=\"language-go\">func circle(radius float64) (func(), func()) {\n    perimeter := func() {\n        fmt.Println(&quot;\u5468\u957f\u662f\uff1a&quot;,2 * math.Pi * radius)\n    }\n    area := func() {\n        fmt.Println(&quot;\u9762\u79ef\u662f\uff1a&quot;,math.Pi * (radius * radius))\n    }\n    return perimeter, area\n}\n\nfunc main() {\n    f1,f2 := circle(3.3)\n    f1()  \/\/ \u5468\u957f\u662f\uff1a 20.734511513692635\n    f2()  \/\/ \u9762\u79ef\u662f\uff1a 34.21194399759284\n}<\/code><\/pre>\n<h2>\u516b defer\u8bed\u53e5<\/h2>\n<h3>8.1 defer\u4ecb\u7ecd<\/h3>\n<p>Go\u8bed\u8a00\u4e2d\uff0c\u51fd\u6570\u5185\u53ef\u4ee5\u4f9d\u6b21\u5305\u542b\u591a\u4e2adefer\u8bed\u53e5\uff0c\u5728\u51fd\u6570\u7ed3\u675f\u65f6\uff0cdefer\u8bed\u53e5\u4f1a\u9006\u5e8f\u6267\u884c<\/p>\n<pre><code class=\"language-go\">func main() {\n    fmt.Println(&quot;start...&quot;)\n    defer fmt.Println(1)\n    defer fmt.Println(2)\n    defer fmt.Println(3)\n    fmt.Println(&quot;end...&quot;)\n}<\/code><\/pre>\n<p>\u8f93\u51fa<\/p>\n<pre><code class=\"language-go\">start...\nend...\n3\n2\n1<\/code><\/pre>\n<p>\u7531\u4e8e<code>defer<\/code>\u8bed\u53e5\u5ef6\u8fdf\u8c03\u7528\u7684\u7279\u6027\uff0c\u6240\u4ee5<code>defer<\/code>\u8bed\u53e5\u80fd\u975e\u5e38\u65b9\u4fbf\u7684\u5904\u7406\u8d44\u6e90\u91ca\u653e\u95ee\u9898\u3002\u6bd4\u5982\uff1a\u8d44\u6e90\u6e05\u7406\u3001\u6587\u4ef6\u5173\u95ed\u3001\u89e3\u9501\u53ca\u8bb0\u5f55\u65f6\u95f4\u7b49\u3002<\/p>\n<h3>8.2 defer\u7684\u6267\u884c\u65f6\u673a<\/h3>\n<p>\u5728Go\u8bed\u8a00\u7684\u51fd\u6570\u4e2d<code>return<\/code>\u8bed\u53e5\u5728\u5e95\u5c42\u5e76\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u5b83\u5206\u4e3a\u7ed9\u8fd4\u56de\u503c\u8d4b\u503c\u548cRET\u6307\u4ee4\u4e24\u6b65\u3002\u800c<code>defer<\/code>\u8bed\u53e5\u6267\u884c\u7684\u65f6\u673a\u5c31\u5728\u8fd4\u56de\u503c\u8d4b\u503c\u64cd\u4f5c\u540e\uff0cRET\u6307\u4ee4\u6267\u884c\u524d\u3002\u5177\u4f53\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p><div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"1918\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"1918\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"d644b8937d\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"56d98ae6e2\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570 \u200b \u51fd\u6570\u5c31\u662f\u76db\u653e\u4e00\u7ec4\u4ee3\u7801\u5757\u7684\u201d\u5bb9\u5668\u201d\uff0c\u8be5\u4ee3\u7801\u5757\u5b9e\u73b0\u67d0\u4e2a\u7279\u5b9a\u529f\u80fd\uff0c\u88ab\u7ec4\u7ec7\u5230\u51fd\u6570\u5185\u540e\u53ef\u4ee5\u91cd\u590d\u8c03\u7528 \u200b Go\u8bed [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2711,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[243],"tags":[260,261,263,262],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/1918"}],"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=1918"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/1918\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/2711"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}