{"id":2537,"date":"2015-11-21T23:40:19","date_gmt":"2015-11-21T15:40:19","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2537"},"modified":"2015-11-21T23:40:19","modified_gmt":"2015-11-21T15:40:19","slug":"go%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84defer%e3%80%81panic%e5%92%8crecover","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2537.html","title":{"rendered":"Go\u8bed\u8a00\u4e2d\u7684defer\u3001panic\u548crecover"},"content":{"rendered":"<p>=Start=<\/p>\n<p><span style=\"color: #ff0000;\"><strong>defer\u8bed\u53e5\u7528\u4e8e\u5ef6\u8fdf\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u65b9\u6cd5(\u6216\u8005\u5f53\u524d\u6240\u521b\u5efa\u7684\u533f\u540d\u51fd\u6570)\u7684\u6267\u884c<\/strong><\/span>\uff0c\u5b83\u4f1a\u5728\u5916\u56f4\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u8fd4\u56de\u4e4b\u524d\u4f46\u662f\u5176\u8fd4\u56de\u503c(\u5982\u679c\u6709\u7684\u8bdd)\u8ba1\u7b97\u4e4b\u540e\u6267\u884c\u3002<\/p>\n<p><span style=\"color: #ff0000;\">\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u4e2d\u6709\u591a\u4e2adefer\u8bed\u53e5\uff0c\u5b83\u4eec\u4f1a\u4ee5LIFO(Last in First out)\u7684\u987a\u5e8f\u6267\u884c<\/span>\u3002<\/p>\n<p>\u5e38\u89c1\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">var file *os.File\nvar err error\nif file, err = os.Open(filename); err != nil {\n    log.Println(\"Failed to open the file\", err)\n    return\n}\ndefer file.Close()<\/pre>\n<hr \/>\n<p>\u901a\u8fc7\u5185\u7f6e\u7684 panic() \u548c recover() \u51fd\u6570\uff0cGo\u8bed\u8a00\u63d0\u4f9b\u4e86\u4e00\u5957\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Go\u8bed\u8a00\u5c06\u9519\u8bef\u548c\u5f02\u5e38\u4e24\u8005\u533a\u5206\u5bf9\u5f85\uff1a<\/strong><\/span><\/p>\n<ul>\n<li>\u9519\u8bef\u662f\u6307\u53ef\u80fd\u51fa\u9519\u7684\u4e1c\u897f\uff0c\u7a0b\u5e8f\u9700\u8981\u4ee5\u4f18\u96c5\u7684\u65b9\u5f0f\u5c06\u5176\u5904\u7406(\u4f8b\u5982\uff0c\u6587\u4ef6\u4e0d\u80fd\u88ab\u6253\u5f00)\u3002<\/li>\n<li>\u800c\u5f02\u5e38\u662f\u6307&#8221;\u4e0d\u53ef\u80fd&#8221;\u53d1\u751f\u7684\u4e8b\u60c5(\u4f8b\u5982\uff0c\u4e00\u4e2a\u5e94\u8be5\u6c38\u8fdc\u4e3atrue\u7684\u6761\u4ef6\u5728\u5b9e\u9645\u73af\u5883\u4e2d\u5374\u662ffalse\u7684)\u3002<\/li>\n<\/ul>\n<p><span style=\"color: #ff0000;\">Go\u8bed\u8a00\u4e2d\u5904\u7406\u9519\u8bef\u7684\u60ef\u7528\u65b9\u6cd5\u662f\u5c06\u9519\u8bef\u4ee5\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u7684\u6700\u540e\u4e00\u4e2a\u8fd4\u56de\u503c\u7684\u5f62\u5f0f\u5c06\u5176\u8fd4\u56de\uff0c\u5e76\u603b\u662f\u5728\u8c03\u7528\u5b83\u7684\u5730\u65b9\u68c0\u67e5\u8fd4\u56de\u7684\u9519\u8bef\u503c<\/span>\u3002<\/p>\n<p>\u5bf9\u4e8e&#8221;\u4e0d\u53ef\u80fd\u53d1\u751f\u7684\u60c5\u51b5&#8221;\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u5185\u7f6e\u7684 panic() \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u4f20\u5165\u4efb\u4f55\u60f3\u8981\u7684\u503c\u3002\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u53ef\u80fd\u4f7f\u7528\u4e00\u4e2a\u65ad\u8a00\uff08\u53c2\u8003\u4e4b\u524d\u7684\u6587\u7ae0\uff1a<a href=\"http:\/\/ixyzero.com\/blog\/archives\/2452.html\" target=\"_blank\">Python\u4e2d\u7684assert<\/a>\uff09\uff0c\u4f46\u5728Go\u8bed\u8a00\u4e2d\u6211\u4eec\u4f7f\u7528panic()\u3002<\/p>\n<p><span style=\"color: #0000ff;\">\u5728\u65e9\u671f\u5f00\u53d1\u4ee5\u53ca\u4efb\u4f55\u53d1\u5e03\u9636\u6bb5\u4e4b\u524d\uff0c\u6700\u7b80\u5355\u540c\u65f6\u4e5f\u53ef\u80fd\u662f\u6700\u597d\u7528\u7684\u65b9\u6cd5\u662f\u8c03\u7528 panic() \u51fd\u6570\u6765\u4e2d\u65ad\u7a0b\u5e8f\u7684\u6267\u884c\u4ee5\u5f3a\u5236\u53d1\u751f\u9519\u8bef\uff0c\u4f7f\u5f97\u8be5\u9519\u8bef\u4e0d\u4f1a\u88ab\u5ffd\u7565\u56e0\u800c\u80fd\u591f\u88ab\u5c3d\u5feb\u4fee\u590d\u3002<\/span><\/p>\n<p>\u4e00\u65e6\u5f00\u59cb\u90e8\u7f72\u7a0b\u5e8f\u65f6\uff0c\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u53ef\u80fd\u53d1\u751f\u9519\u8bef\uff0c\u4f46\u6211\u4eec\u5e94\u8be5\u5c3d\u4e00\u5207\u53ef\u80fd\u907f\u514d\u4e2d\u65ad\u7a0b\u5e8f(panic + recover)\u3002<\/p>\n<p>\u5bf9\u4e8e\u4efb\u4f55\u60c5\u51b5\u4e0b\u53ef\u80fd\u8fd0\u884c\u4e5f\u53ef\u80fd\u4e0d\u8fd0\u884c\u7684\u51fd\u6570\u6216\u8005\u65b9\u6cd5\uff0c\u5982\u679c\u8c03\u7528\u4e86panic()\u51fd\u6570\u6216\u8005\u8c03\u7528\u4e86\u53d1\u751f\u5f02\u5e38\u7684\u51fd\u6570\u6216\u8005\u65b9\u6cd5\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528recover()\u4ee5\u4fdd\u8bc1\u5c06\u5f02\u5e38\u8f6c\u6362\u6210\u9519\u8bef\u3002<span style=\"color: #ff0000;\">\u7406\u60f3\u60c5\u51b5\u4e0b\uff0crecover()\u51fd\u6570\u5e94\u8be5\u5728\u5c3d\u53ef\u80fd\u63a5\u8fd1\u4e8e\u76f8\u5e94panic()\u7684\u5730\u65b9\u88ab\u8c03\u7528\uff0c\u5e76\u5728\u8bbe\u7f6e\u5176\u5916\u56f4\u51fd\u6570\u7684error\u8fd4\u56de\u503c\u4e4b\u524d\u5c3d\u53ef\u80fd\u5408\u7406\u7684\u5c06\u7a0b\u5e8f\u6062\u590d\u5230\u5065\u5eb7\u72b6\u6001<\/span>\u3002<\/p>\n<p>==<\/p>\n<p>Go\u8bed\u8a00\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e5f\u53ef\u4ee5\u662f\u4efb\u610f\u4e2a\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u90a3\u4e48\u8fd4\u56de\u503c\u5217\u8868\u7684\u53f3\u62ec\u53f7\u540e\u9762\u662f\u7d27\u63a5\u7740\u5de6\u5927\u62ec\u53f7\u7684\u3002<\/p>\n<p>\u5982\u679c\u51fd\u6570\u6709\u8fd4\u56de\u503c\uff0c\u5219\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u8bed\u53e5\u5fc5\u987b\u662f\u4e00\u4e2areturn\u8bed\u53e5\u6216\u8005panic()\u8c03\u7528\u3002\u5982\u679c\u51fd\u6570\u662f\u4ee5\u629b\u51fa\u5f02\u5e38\u7ed3\u675f\uff0cGo\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3a\u8fd9\u4e2a\u51fd\u6570\u4e0d\u9700\u8981\u6b63\u5e38\u8fd4\u56de\uff0c\u6240\u4ee5\u4e5f\u5c31\u4e0d\u9700\u8981\u8fd9\u4e2areturn\u8bed\u53e5\u3002<\/p>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= defer\u8bed\u53e5\u7528\u4e8e\u5ef6\u8fdf\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u65b9\u6cd5(\u6216\u8005\u5f53\u524d\u6240\u521b\u5efa\u7684\u533f\u540d\u51fd\u6570)\u7684\u6267\u884c\uff0c\u5b83\u4f1a\u5728\u5916\u56f4\u51fd\u6570\u6216\u8005\u65b9 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[567,563,564,568,569],"class_list":["post-2537","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-defer","tag-go","tag-golang","tag-panic","tag-recover"],"views":5715,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2537"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2537\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}