{"id":7990,"date":"2023-01-28T14:27:05","date_gmt":"2023-01-28T06:27:05","guid":{"rendered":"https:\/\/egonlin.com\/?p=7990"},"modified":"2023-01-28T14:27:05","modified_gmt":"2023-01-28T06:27:05","slug":"26-%e5%87%bd%e6%95%b0%e4%b9%8b%e5%86%85%e7%bd%ae%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=7990","title":{"rendered":"26 \u51fd\u6570\u4e4b\u5185\u7f6e\u51fd\u6570"},"content":{"rendered":"<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>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2023\/01\/defer\u6267\u884c\u65f6\u673a.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2023\/01\/defer\u6267\u884c\u65f6\u673a.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<h3>8.3 defer\u7ecf\u5178\u6848\u4f8b<\/h3>\n<p>\u4e0b\u8ff0\u4ee3\u7801\u8fd0\u884c\u7ed3\u679c\u662f\uff1f\uff1f\uff1f<\/p>\n<pre><code class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\n\/\/ \u5173\u952e\u770b\uff0cdefer\u5b9a\u4e49\u7684\u51fd\u6570\u5185\u90e8\u662f\u5426\u5f15\u7528\u7684\u662ffunc\u7684\u8fd4\u56de\u503c\uff0c\u5982\u679c\u662f\uff0c\u5219\u4fee\u6539\u7684\u5c31\u662f\u8fd4\u56de\u503c\nfunc f1() int {\n    x := 5\n    defer func() {\n        x++ \/\/ \u6b64\u5904\u7684x\u5f15\u7528\u7684\u662f\u54ea\u4e2a\u4f4d\u7f6e\u7684\uff1fx++\u662f\u5426\u5f71\u54cd\u8fd4\u56de\u503c\uff1f\n        fmt.Println(&quot;f1===&gt;&quot;,x) \/\/ 6\n    }()\n    return x\n}\nfunc f2() (y int) {\n    x := 5\n    defer func() {\n        x++ \/\/ \u6b64\u5904\u7684x\u5f15\u7528\u7684\u662f\u54ea\u4e2a\u4f4d\u7f6e\u7684\uff1fx++\u662f\u5426\u5f71\u54cd\u8fd4\u56de\u503c\uff1f\n        \/\/y++\n        fmt.Println(&quot;f2===&gt;&quot;,x)  \/\/ 6\n    }()\n    return x\n}\nfunc f3() (x int) {\n    defer func(x int) {\n        x++ \/\/ \u6b64\u5904\u7684x\u5f15\u7528\u7684\u662f\u54ea\u4e2a\u4f4d\u7f6e\u7684\uff1fx++\u662f\u5426\u5f71\u54cd\u8fd4\u56de\u503c\uff1f\n        fmt.Println(&quot;f3===&gt;&quot;,x)  \/\/ 1\n\n    }(x)\n    return 5\n}\nfunc f4() (x int) {\n    defer func() {\n        x++ \/\/ \u6b64\u5904\u7684x\u5f15\u7528\u7684\u662f\u54ea\u4e2a\u4f4d\u7f6e\u7684\uff1fx++\u662f\u5426\u5f71\u54cd\u8fd4\u56de\u503c\uff1f\n        fmt.Println(&quot;f4===&gt;&quot;,x)  \/\/ 6\n\n    }()\n    return 5\n}\n\nfunc main() {\n    fmt.Println(f1())\n    fmt.Println(f2())\n    fmt.Println(f3())\n    fmt.Println(f4())\n}<\/code><\/pre>\n<h3>8.4 defer\u9762\u8bd5\u9898<\/h3>\n<p>\u4e0b\u8ff0\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1f\uff1f\uff1f\u63d0\u793a\uff1a\u5728defer\u6ce8\u518c\u8981\u5ef6\u8fdf\u6267\u884c\u7684\u51fd\u6570\u65f6\uff0c\u8be5\u51fd\u6570\u6240\u6709\u7684\u53c2\u6570\u7684\u503c\u90fd\u4f1a\u88ab\u786e\u5b9a\u4e0b\u6765<\/p>\n<pre><code class=\"language-go\">func calc(index string, a, b int) int {\n    ret := a + b\n    fmt.Println(index, a, b, ret)\n    return ret\n}\n\nfunc main() {\n    x := 1\n    y := 2\n    defer calc(&quot;AA&quot;, x, calc(&quot;A&quot;, x, y))\n    x = 10\n    defer calc(&quot;BB&quot;, x, calc(&quot;B&quot;, x, y))\n    y = 20\n}<\/code><\/pre>\n<h2>\u4e5d \u5185\u7f6e\u51fd\u6570<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2023\/01\/\u5185\u7f6e\u51fd\u6570.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2023\/01\/\u5185\u7f6e\u51fd\u6570.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p><strong>panic\/recover<\/strong><\/p>\n<p>Go\u8bed\u8a00\u4f7f\u7528<code>panic\/recover<\/code>\u6a21\u5f0f\u6765\u5904\u7406\u9519\u8bef\u3002 <code>panic<\/code>\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u5f15\u53d1\uff0c\u4f46<code>recover<\/code>\u53ea\u6709\u5728<code>defer<\/code>\u8c03\u7528\u7684\u51fd\u6570\u4e2d\u6709\u6548\u3002 \u9996\u5148\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-go\">func funcA() {\n    fmt.Println(&quot;func A&quot;)\n}\n\nfunc funcB() {\n    panic(&quot;panic in B&quot;)  \/\/ \u4e3b\u52a8\u629b\u51fapanic\n}\n\nfunc funcC() {\n    fmt.Println(&quot;func C&quot;)\n}\nfunc main() {\n    funcA()\n    funcB()\n    funcC()\n}<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-bash\">func A\npanic: panic in B\n\ngoroutine 1 [running]:\nmain.funcB(...)\n        ...\/code\/func\/main.go:12\nmain.main()\n        ...\/code\/func\/main.go:20 +0x98<\/code><\/pre>\n<p>\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4<code>funcB<\/code>\u4e2d\u5f15\u53d1\u4e86<code>panic<\/code>\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u5f02\u5e38\u9000\u51fa\u4e86\u3002\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7<code>recover<\/code>\u5c06\u7a0b\u5e8f\u6062\u590d\u56de\u6765\uff0c\u7ee7\u7eed\u5f80\u540e\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-go\">func funcA() {\n    fmt.Println(&quot;func A&quot;)\n}\n\nfunc funcB() {\n    defer func() {\n        err := recover()\n        \/\/\u5982\u679c\u7a0b\u5e8f\u51fa\u51fa\u73b0\u4e86panic\u9519\u8bef,\u53ef\u4ee5\u901a\u8fc7recover\u6062\u590d\u8fc7\u6765\n        if err != nil {\n            fmt.Println(&quot;recover in B&quot;)\n        }\n    }()\n    panic(&quot;panic in B&quot;)\n}\n\nfunc funcC() {\n    fmt.Println(&quot;func C&quot;)\n}\nfunc main() {\n    funcA()\n    funcB()\n    funcC()\n}<\/code><\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ol>\n<li><code>recover()<\/code>\u5fc5\u987b\u642d\u914d<code>defer<\/code>\u4f7f\u7528\u3002<\/li>\n<li><code>defer<\/code>\u4e00\u5b9a\u8981\u5728\u53ef\u80fd\u5f15\u53d1<code>panic<\/code>\u7684\u8bed\u53e5\u4e4b\u524d\u5b9a\u4e49\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u516b defer\u8bed\u53e5 8.1 defer\u4ecb\u7ecd 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\uff0cd [&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\/7990"}],"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=7990"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/7990\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}