{"id":207320,"date":"2025-07-08T10:05:26","date_gmt":"2025-07-08T02:05:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207320\/"},"modified":"2025-07-08T10:05:26","modified_gmt":"2025-07-08T02:05:26","slug":"%e5%85%b3%e4%ba%8e-defer-%e8%a1%8c%e4%b8%ba%e7%9a%84-go-%e8%af%ad%e8%a8%80%e9%9d%a2%e8%af%95%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207320\/","title":{"rendered":"\u5173\u4e8e defer \u884c\u4e3a\u7684 Go \u8bed\u8a00\u9762\u8bd5\u95ee\u9898"},"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>\u5173\u4e8e defer \u884c\u4e3a\u7684 Go \u8bed\u8a00\u9762\u8bd5\u95ee\u9898<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 18:21:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5173\u4e8e defer \u884c\u4e3a\u7684 Go \u8bed\u8a00\u9762\u8bd5\u95ee\u9898\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709 2 \u4e2a\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>func test() int {\n    var x int\n    defer func() {\n\n        x++\n    }()\n    x = 1\n    return x\n}\n\nfunc main() {\n    fmt.println(test())\n}\n<\/pre>\n<p>\u5b83\u8fd4\u56de\uff1a<code>1<\/code>\u3002\u4f46\u662f\uff0c\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u7684\u884c\u4e3a\u6709\u6240\u4e0d\u540c\uff1a<\/p>\n<pre>func test() (x int) {\n    defer func() {\n\n        x++\n    }()\n    x = 1\n    return\n}\n\nfunc main() {\n    fmt.println(test())\n}\n<\/pre>\n<p>\u5b83\u8fd4\u56de <code>2<\/code>\u3002<\/p>\n<p>\u4e3a\u4e86\u4e86\u89e3\u53d1\u751f\u4e86\u4ec0\u4e48\uff0c\u6211\u53cd\u6c47\u7f16\u4e86\u4ee3\u7801\u3002<\/p>\n<p>\u5bf9\u4e8e\u7b2c\u4e00\u4e2a\u4ee3\u7801\u7247\u6bb5\uff08\u5176\u8f93\u51fa\u4e3a <code>1<\/code>\uff09\uff1a<\/p>\n<pre>main.go:19            0x48cf40                64488b0c25f8ffffff      movq fs:0xfffffff8, cx\n  main.go:19            0x48cf49                483b6110                cmpq 0x10(cx), sp\n  main.go:19            0x48cf4d                7678                    jbe 0x48cfc7\n  main.go:19            0x48cf4f                4883ec58                subq $0x58, sp\n  main.go:19            0x48cf53                48896c2450              movq bp, 0x50(sp)\n  main.go:19            0x48cf58                488d6c2450              leaq 0x50(sp), bp\n  main.go:20            0x48cf5d                e83effffff              call main.test(sb)\n  main.go:20            0x48cf62                e8c9bff7ff              call runtime.convt64(sb)\n  main.go:20            0x48cf67                488b442408              movq 0x8(sp), ax\n  main.go:20            0x48cf6c                0f57c0                  xorps x0, x0\n  main.go:20            0x48cf6f                0f11442440              movups x0, 0x40(sp)\n  main.go:20            0x48cf74                488d0d05090100          leaq 0x10905(ip), cx\n  main.go:20            0x48cf7b                48894c2440              movq cx, 0x40(sp)\n  main.go:20            0x48cf80                4889442448              movq ax, 0x48(sp)\n  print.go:274          0x48cf85                488b0524020d00          movq os.stdout(sb), ax\n  print.go:274          0x48cf8c                488d0d0dd70400          leaq go.itab.*os.file,io.writer(sb), cx\n  print.go:274          0x48cf93                48890c24                movq cx, 0(sp)\n  print.go:274          0x48cf97                4889442408              movq ax, 0x8(sp)\n  print.go:274          0x48cf9c                488d442440              leaq 0x40(sp), ax\n  print.go:274          0x48cfa1                4889442410              movq ax, 0x10(sp)\n  print.go:274          0x48cfa6                48c744241801000000      movq $0x1, 0x18(sp)\n  print.go:274          0x48cfaf                48c744242001000000      movq $0x1, 0x20(sp)\n  print.go:274          0x48cfb8                e84397ffff              call fmt.fprintln(sb)\n  print.go:274          0x48cfbd                488b6c2450              movq 0x50(sp), bp\n  print.go:274          0x48cfc2                4883c458                addq $0x58, sp\n  print.go:274          0x48cfc6                c3                      ret\n  main.go:19            0x48cfc7                e8d447fcff              call runtime.morestack_noctxt(sb)\n  main.go:19            0x48cfcc                e96fffffff              jmp main.main(sb)<\/pre>\n<p>\u5bf9\u4e8e\u7b2c\u4e8c\u4e2a\u4ee3\u7801\u7247\u6bb5\uff08\u5176\u8f93\u51fa\u4e3a <code>2<\/code>\uff09\uff1a<\/p>\n<pre>main.go:18            0x48cf30                64488b0c25f8ffffff      MOVQ FS:0xfffffff8, CX\n  main.go:18            0x48cf39                483b6110                CMPQ 0x10(CX), SP\n  main.go:18            0x48cf3d                7678                    JBE 0x48cfb7\n  main.go:18            0x48cf3f                4883ec58                SUBQ $0x58, SP\n  main.go:18            0x48cf43                48896c2450              MOVQ BP, 0x50(SP)\n  main.go:18            0x48cf48                488d6c2450              LEAQ 0x50(SP), BP\n  main.go:19            0x48cf4d                e84effffff              CALL main.test(SB)\n  main.go:19            0x48cf52                e8d9bff7ff              CALL runtime.convT64(SB)\n  main.go:19            0x48cf57                488b442408              MOVQ 0x8(SP), AX\n  main.go:19            0x48cf5c                0f57c0                  XORPS X0, X0\n  main.go:19            0x48cf5f                0f11442440              MOVUPS X0, 0x40(SP)\n  main.go:19            0x48cf64                488d0d15090100          LEAQ 0x10915(IP), CX\n  main.go:19            0x48cf6b                48894c2440              MOVQ CX, 0x40(SP)\n  main.go:19            0x48cf70                4889442448              MOVQ AX, 0x48(SP)\n  print.go:274          0x48cf75                488b0534020d00          MOVQ os.Stdout(SB), AX\n  print.go:274          0x48cf7c                488d0d1dd70400          LEAQ go.itab.*os.File,io.Writer(SB), CX\n  print.go:274          0x48cf83                48890c24                MOVQ CX, 0(SP)\n  print.go:274          0x48cf87                4889442408              MOVQ AX, 0x8(SP)\n  print.go:274          0x48cf8c                488d442440              LEAQ 0x40(SP), AX\n  print.go:274          0x48cf91                4889442410              MOVQ AX, 0x10(SP)\n  print.go:274          0x48cf96                48c744241801000000      MOVQ $0x1, 0x18(SP)\n  print.go:274          0x48cf9f                48c744242001000000      MOVQ $0x1, 0x20(SP)\n  print.go:274          0x48cfa8                e85397ffff              CALL fmt.Fprintln(SB)\n  print.go:274          0x48cfad                488b6c2450              MOVQ 0x50(SP), BP\n  print.go:274          0x48cfb2                4883c458                ADDQ $0x58, SP\n  print.go:274          0x48cfb6                c3                      RET\n  main.go:18            0x48cfb7                e8e447fcff              CALL runtime.morestack_noctxt(SB)\n  main.go:18            0x48cfbc                e96fffffff              JMP main.main(SB)<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>Golang \u89c4\u8303\u4e2d\u5173\u4e8e defer \u8bed\u53e5\u7684\u89c4\u5b9a\u4e3a \uff1a<\/p>\n<p>[&#8230;] \u5982\u679c\u5ef6\u8fdf\u51fd\u6570\u662f\u51fd\u6570\u6587\u5b57<strong>\u5e76\u4e14\u5468\u56f4\u51fd\u6570\u5177\u6709\u5728\u6587\u5b57\u8303\u56f4\u5185\u7684\u547d\u540d\u7ed3\u679c\u53c2\u6570<\/strong>\uff0c\u5219\u5ef6\u8fdf\u51fd\u6570\u53ef\u4ee5\u5728\u4e4b\u524d\u8bbf\u95ee\u548c\u4fee\u6539\u7ed3\u679c\u53c2\u6570\u5b83\u4eec\u88ab\u8fd4\u56de\u3002<\/p>\n<p>\uff08\u6211\u7684\u91cd\u70b9\uff09<\/p>\n<p>\u5728\u4e2d\uff0c\u51fd\u6570<code>test<\/code>\u6ca1\u6709\u547d\u540d\u8fd4\u56de\u53c2\u6570\uff1b <code>x<\/code> \u53ea\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u60a8\u7684 defer \u8bed\u53e5\u65e0\u6cd5\u4fee\u6539\u51fd\u6570 <code>test<\/code> \u7684\u7ed3\u679c\u3002<\/p>\n<p>\u5728 \u4e2d\uff0c\u51fd\u6570 <code>test<\/code> \u6709\u4e00\u4e2a\u547d\u540d\u8fd4\u56de\u53c2\u6570 <code>x<\/code>\uff0c\u8be5\u53c2\u6570\u4f4d\u4e8e\u51fd\u6570\u6587\u5b57\u7684\u8303\u56f4\u5185\u3002\u56e0\u6b64\uff0cdefer \u8bed\u53e5\u53ef\u4ee5\uff08\u5e76\u4e14\u786e\u5b9e\uff09\u4fee\u6539\u51fd\u6570 <code>test<\/code> \u7684\u7ed3\u679c\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5173\u4e8e defer \u884c\u4e3a\u7684 Go \u8bed\u8a00\u9762\u8bd5\u95ee\u9898\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/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-207320","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207320","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=207320"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207320\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}