{"id":168638,"date":"2020-01-09T09:09:23","date_gmt":"2020-01-09T01:09:23","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=168638"},"modified":"2019-12-30T11:12:00","modified_gmt":"2019-12-30T03:12:00","slug":"linux-go-defer","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-go-defer.html","title":{"rendered":"\u6559\u4f60\u4e00\u4e2ago\u4e2ddefer\u7684\u9690\u85cf\u529f\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u6d45\u8c08go\u4e2ddefer\u7684\u4e00\u4e2a\u9690\u85cf\u529f\u80fd\uff0c\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u968f\u7740\u5c0f\u7f16\u6765\u4e00\u8d77\u5b66\u4e60\u5b66\u4e60\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728\u5f00\u59cb\u4f7f\u7528Go\u8fdb\u884c\u7f16\u7801\u65f6\uff0cDefer\u662f\u8981\u5173\u6ce8\u7684\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u7279\u6027\u3002\u5b83\u975e\u5e38\u7b80\u5355\uff1a\u5728\u4efb\u4f55\u51fd\u6570\u4e2d\uff0c\u7ed9\u5176\u4ed6\u51fd\u6570\u7684\u8c03\u7528\u52a0\u4e0a\u524d\u7f00 defer\u4ee5\u786e\u4fdd\u8be5\u51fd\u6570\u5728\u5916\u90e8\u51fd\u6570\u9000\u51fa\u4e4b\u524d\u7acb\u5373\u6267\u884c\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u51fa\u73b0\u5f02\u5e38\u88ab\u4e2d\u65ad\uff0c\u8be5\u5ef6\u8fdf\u51fd\u6570\u4e5f\u5c06\u8fd0\u884c\u3002<\/p>\n

\u4f46\u662f\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528defer\u5728\u4efb\u4f55\u51fd\u6570\u5f00\u59cb\u540e\u548c\u7ed3\u675f\u524d\u6267\u884c\u914d\u5bf9\u7684\u4ee3\u7801\u3002\u8fd9\u4e2a\u9690\u85cf\u7684\u529f\u80fd\u5728\u7f51\u4e0a\u7684\u6559\u7a0b\u548c\u4e66\u7c4d\u4e2d\u5f88\u5c11\u63d0\u5230\u3002\u8981\u4f7f\u7528\u6b64\u529f\u80fd\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u5e76\u4f7f\u5b83\u672c\u8eab\u8fd4\u56de\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd4\u56de\u7684\u51fd\u6570\u5c06\u4f5c\u4e3a\u771f\u6b63\u7684\u5ef6\u8fdf\u51fd\u6570\u3002\u5728 defer \u8bed\u53e5\u8c03\u7528\u7236\u51fd\u6570\u540e\u5728\u5176\u4e0a\u6dfb\u52a0\u989d\u5916\u7684\u62ec\u53f7\u6765\u5ef6\u8fdf\u6267\u884c\u8fd4\u56de\u7684\u5b50\u51fd\u6570\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

func main() {\r\n  defer greet()() \r\n  fmt.Println(\"Some code here...\")\r\n}\r\n \r\nfunc greet() func() {\r\n  fmt.Println(\"Hello!\")\r\n  return func() { fmt.Println(\"Bye!\") } \/\/ this will be deferred\r\n}<\/pre>\n

\u8f93\u51fa\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n

\r\nHello!\r\nSome code here...\r\nBye!<\/pre>\n

\u7236\u51fd\u6570\u8fd4\u56de\u7684\u51fd\u6570\u5c06\u662f\u5b9e\u9645\u7684\u5ef6\u8fdf\u51fd\u6570\u3002\u7236\u51fd\u6570\u4e2d\u7684\u5176\u4ed6\u4ee3\u7801\u5c06\u5728\u51fd\u6570\u5f00\u59cb\u65f6\uff08\u7531 defer \u8bed\u53e5\u653e\u7f6e\u7684\u4f4d\u7f6e\u51b3\u5b9a\uff09\u7acb\u5373\u6267\u884c\u3002<\/p>\n

\u8fd9\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4ec0\u4e48\u80fd\u529b\uff1f\u56e0\u4e3a\u5728\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u533f\u540d\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5b8c\u6574\u7684\u8bcd\u6cd5\u73af\u5883\uff08lexical environment\uff09\uff0c\u8fd9\u610f\u5473\u7740\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u5f15\u7528\u8be5\u51fd\u6570\u7684\u53d8\u91cf\u3002\u5728\u4e0b\u4e00\u4e2a\u793a\u4f8b\u4e2d\u770b\u5230\u7684\uff0c\u53c2\u6570\u53d8\u91cf\u5728measure\u51fd\u6570\u7b2c\u4e00\u6b21\u6267\u884c\u548c\u5176\u5ef6\u8fdf\u6267\u884c\u7684\u5b50\u51fd\u6570\u5185\u90fd\u80fd\u8bbf\u95ee\u5230\uff1a<\/p>\n

func main() {\r\n  example()\r\n  otherExample()\r\n}\r\n \r\nfunc example(){\r\n  defer measure(\"example\")()\r\n  fmt.Println(\"Some code here\")\r\n}\r\n \r\nfunc otherExample(){\r\n  defer measure(\"otherExample\")()\r\n  fmt.Println(\"Some other code here\")\r\n}\r\n \r\nfunc measure(name string) func() {\r\n  start := time.Now()\r\n  fmt.Printf(\"Starting function %s\\n\", name)\r\n  return func(){ fmt.Printf(\"Exiting function %s after %s\\n\", name, time.Since(start)) }\r\n}<\/pre>\n

\u8f93\u51fa\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n

Starting example\r\nSome code here\r\nExiting example after 0s\r\nStarting otherExample\r\nSome other code here\r\nExiting otherExample after 0s<\/pre>\n

\u6b64\u5916\u51fd\u6570\u547d\u540d\u7684\u8fd4\u56de\u503c\u4e5f\u662f\u51fd\u6570\u5185\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u6240\u4ee5\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684measure\u51fd\u6570\u5982\u679c\u63a5\u6536\u547d\u540d\u8fd4\u56de\u503c\u4f5c\u4e3a\u53c2\u6570\u7684\u8bdd\uff0c\u90a3\u4e48\u547d\u540d\u8fd4\u56de\u503c\u5728\u5ef6\u8fdf\u6267\u884c\u7684\u51fd\u6570\u4e2d\u8bbf\u95ee\u5230\uff0c\u8fd9\u6837\u5c31\u80fd\u5c06measure\u51fd\u6570\u6539\u9020\u6210\u8bb0\u5f55\u5165\u53c2\u548c\u8fd4\u56de\u503c\u7684\u5de5\u5177\u51fd\u6570\u3002<\/p>\n

\u4e0b\u9762\u7684\u793a\u4f8b\u662f\u5f15\u7528\u300ago \u8bed\u8a00\u7a0b\u5e8f\u8bbe\u8ba1\u300b\u4e2d\u7684\u4ee3\u7801\u6bb5:<\/p>\n

\r\nfunc bigSlowOperation() {\r\n  defer trace(\"bigSlowOperation\")() \/\/ don't forget the extra parentheses\r\n  \/\/ ...lots of work\u2026\r\n  time.Sleep(10 * time.Second) \/\/ simulate slow\r\n  operation by sleeping\r\n}\r\nfunc trace(msg string) func() {\r\n  start := time.Now()\r\n  log.Printf(\"enter %s\", msg)\r\n  return func() { \r\n    log.Printf(\"exit %s (%s)\", msg,time.Since(start)) \r\n  }\r\n}<\/pre>\n

\u53ef\u4ee5\u60f3\u8c61\uff0c\u5c06\u4ee3\u7801\u5ef6\u8fdf\u5728\u51fd\u6570\u7684\u5165\u53e3\u548c\u51fa\u53e3\u4f7f\u7528\u662f\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u5c24\u5176\u662f\u5728\u8c03\u8bd5\u4ee3\u7801\u7684\u65f6\u5019\u3002<\/p>\n

\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u5f00\u59cb\u4f7f\u7528Go\u8fdb\u884c\u7f16\u7801\u65f6\uff0cDefer\u662f\u8981\u5173\u6ce8\u7684\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u7279\u6027\u3002\u5b83\u975e\u5e38\u7b80\u5355\uff1a\u5728\u4efb\u4f55\u51fd\u6570\u4e2d\uff0c\u7ed9\u5176\u4ed6\u51fd\u6570\u7684\u8c03\u7528\u52a0\u4e0a […]<\/p>\n","protected":false},"author":1482,"featured_media":153204,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-168638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/168638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=168638"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/168638\/revisions"}],"predecessor-version":[{"id":168640,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/168638\/revisions\/168640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/153204"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=168638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=168638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=168638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}