{"id":6696,"date":"2022-01-29T17:14:05","date_gmt":"2022-01-29T09:14:05","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=6696"},"modified":"2023-09-12T10:19:54","modified_gmt":"2023-09-12T02:19:54","slug":"go-24","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2022\/01\/29\/go-24\/","title":{"rendered":"Go\u8bed\u8a00\u4e4b\u9519\u8bef\u5904\u7406\u548c\u5b95\u673a"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u8ffd\u6c42\u7b80\u6d01\u4f18\u96c5\uff0c\u6240\u4ee5\uff0cGo \u8bed\u8a00\u4e0d\u652f\u6301\u4f20\u7edf\u7684 try\u2026catch\u2026finally \u8fd9\u79cd\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u4e2d\u5f15\u5165\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u4e3a\uff1adefer, panic, recover<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u8fd4\u56de\u9519\u8bef\u524d\uff0c\u9700\u8981\u5b9a\u4e49\u4f1a\u4ea7\u751f\u54ea\u4e9b\u53ef\u80fd\u7684\u9519\u8bef\u3002\u5728Go \u8bed\u8a00\u4e2d\uff0c\u4f7f\u7528errors \u5305\u8fdb\u884c\u9519\u8bef\u7684\u5b9a\u4e49\uff0c \u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var err = errors.New(\"this is an error\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9519\u8bef\u5b57\u7b26\u4e32\u7531\u4e8e\u76f8\u5bf9\u56fa\u5b9a\uff0c \u4e00\u822c\u5728\u5305\u4f5c\u7528\u57df\u58f0\u660e\uff0c\u5e94\u5c3d\u91cf\u51cf\u5c11\u5728\u4f7f\u7528\u65f6\u76f4\u63a5\u4f7f\u7528errors.New \u8fd4\u56de<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001errors \u5305\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e2d\u5f15\u5165 error \u63a5\u53e3\u7c7b\u578b\u4f5c\u4e3a\u9519\u8bef\u5904\u7406\u7684\u6807\u51c6\u6a21\u5f0f\uff0c\u5982\u679c\u51fd\u6570\u8981\u8fd4\u56de\u9519\u8bef\uff0c\u5219\u8fd4\u56de\u503c\u7c7b\u578b\u5217\u8868\u4e2d\u80af\u5b9a\u5305\u542b error\u3002\u63a5\u53e3\u58f0\u660e\u4f4d\u7f6e\u4e3a\u7cfb\u7edf\u5305\u8def\u5f84(go1.18.1\u4e3a\u4f8b\u5b50)\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/local\/go\/src\/builtin\/builtin.go     \/\/\u672c\u4f8b\u5b50\u4e2dgo\u5b89\u88c5\u5728\/usr\/local\u76ee\u5f55\u4e0b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e2d\u8fd4\u56de\u7684 error \u7c7b\u578b\u7a76\u7adf\u662f\u4ec0\u4e48\u5462\uff1f\u67e5\u770bGo\u8bed\u8a00\u7684\u6e90\u7801\u5c31\u4f1a\u53d1\u73b0 error \u7c7b\u578b\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u63a5\u53e3\u7c7b\u578b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ The error built-in interface type is the conventional interface for\n\/\/ representing an error condition, with the nil value representing no error.\ntype error interface {\n    Error() string\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">error \u63a5\u53e3\u6709\u4e00\u4e2a\u7b7e\u540d\u4e3a Error() string \u7684\u65b9\u6cd5\uff0c\u6240\u6709\u5b9e\u73b0\u8be5\u63a5\u53e3\u7684\u7c7b\u578b\u90fd\u53ef\u4ee5\u5f53\u4f5c\u4e00\u4e2a\u9519\u8bef\u7c7b\u578b\u3002Error() \u65b9\u6cd5\u7ed9\u51fa\u4e86\u9519\u8bef\u7684\u63cf\u8ff0\uff0c\u5728\u4f7f\u7528fmt.Println \u6253\u5370\u9519\u8bef\u65f6\uff0c\u4f1a\u5728\u5185\u90e8\u8c03\u7528 Error() string \u65b9\u6cd5\u6765\u5f97\u5230\u8be5\u9519\u8bef\u7684\u63cf\u8ff0\u3002\u53ea\u8981\u5b9e\u73b0\u4e86error\u63a5\u53e3\u5c31\u53ef\u4ee5\u88aberror\u7c7b\u578b\u6240\u63a5\u6536\uff0c\u6253\u5370\u7684\u65f6\u5019\u4f1a\u76f4\u63a5\u6253\u5370\u5b9e\u73b0\u63a5\u53e3\u65f6\u65b9\u6cd5\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u51fd\u6570\u9700\u8981\u8fd4\u56de\u9519\u8bef\uff0c\u5c31\u5c06 error \u4f5c\u4e3a\u591a\u4e2a\u8fd4\u56de\u503c\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\uff08\u4f46\u8fd9\u5e76\u975e\u662f\u5f3a\u5236\u8981\u6c42\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u73b0\u4e86Error()\u65b9\u6cd5\uff0c\u5e76\u81ea\u5b9a\u4e49\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"616\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/2-7.png\" alt=\"\" class=\"wp-image-6710\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/2-7.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/2-7-300x261.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/2-7-600x523.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c13\u884c\uff1a\u5b9e\u73b0\u4e86error\u63a5\u53e3\u7684Error()\u65b9\u6cd5\uff0c\u65b9\u6cd5\u7684\u63a5\u6536\u5668\u4e3a\u7ed3\u6784\u4f53\uff0c\u8fd4\u56de\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b<\/li>\n\n\n\n<li>\u7b2c14\u884c\uff1a\u5b9a\u4e49\u4e86\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u8fd4\u56de\u7684\u9519\u8bef\u63d0\u793a\u4fe1\u606f<\/li>\n\n\n\n<li>\u7b2c19\u884c\uff1a\u521b\u5efa\u51fd\u6570\uff0c\u8fd4\u56de\u6574\u6570\u7c7b\u578b\u548c\u9519\u8bef\u7c7b\u578b\uff0c\u65b9\u6cd5\u5185\u90e8\u6267\u884c\u5b9e\u4f8b\u5316\u7ed3\u6784\u4f53<\/li>\n\n\n\n<li>\u7b2c26\u884c\uff1a\u4e3b\u51fd\u6570\u4e2d\uff0c\u8c03\u7528\u51fd\u6570\uff0c\u83b7\u53d6\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/li>\n\n\n\n<li>\u7b2c27\u884c\uff1a\u5224\u65ad\u8fd4\u56de\u7684\u7ed3\u6784\u4f53\u5185\u90e8\u7684\u503c(age)\u662f\u5426\u7b49\u4e8e1\uff0c\u4e0d\u7b49\u4e8e\u63d0\u793a\u62a5\u9519\u4fe1\u606f\uff0c\u5e76\u6253\u5370\u5b9e\u9645\u7684\u503c<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"107\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-6.png\" alt=\"\" class=\"wp-image-6714\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-6.png 704w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-6-300x46.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-6-600x91.png 600w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2<strong>\u3001\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u9519\u8bef\u5b9a\u4e49\uff0c\u624b\u52a8\u5b9a\u4e49\u9519\u8bef<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u624b\u52a8\u5b9a\u4e49\u9519\u8bef\u9700\u8981\u4f7f\u7528errors\u5305\u4e2d\u7684New\u51fd\u6570\uff0c\u51fd\u6570\u4f4d\u7f6e\u4e3a\u7cfb\u7edf\u5305\u8def\u5f84\u4e0b\u7684errors.go\u6587\u4ef6\u4e2d\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"389\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-6.png\" alt=\"\" class=\"wp-image-10774\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-6.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-6-300x164.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/11\/9-6-600x329.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u5b9a\u4e49\u4e00\u4e2a\u9664\u6cd5\u51fd\u6570\uff0c\u5f53\u9664\u6570\u4e3a0 \u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u9884\u5b9a\u4e49\u7684\u9664\u6570\u4e3a0 \u7684\u9519\u8bef\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"744\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/1-7-1024x744.png\" alt=\"\" class=\"wp-image-6717\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/1-7-1024x744.png 1024w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/1-7-300x218.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/1-7-768x558.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/1-7-600x436.png 600w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/1-7.png 1057w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c9\u884c\uff1a\u901a\u8fc7New\u51fd\u6570\u9884\u5b9a\u4e49\u4e00\u4e2a\u9664\u6570\u4e3a0\u7684\u9519\u8bef<\/li>\n\n\n\n<li>\u7b2c11\u884c\uff1a\u521b\u5efa\u8fd0\u7b97\u51fd\u6570\uff0c\u53c2\u6570\u4e3a\u88ab\u9664\u6570\u548c\u9664\u6570\uff0c\u8fd4\u56de\u7c7b\u578b\u4e3a\u6574\u578b\u548c\u9519\u8bef\u7c7b\u578b<\/li>\n\n\n\n<li>\u7b2c12\u884c\uff0c\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u679c\u88ab\u9664\u6570\u4e3a0\uff0c\u90a3\u4e48\u8fd4\u56de0\u548c\u9884\u5b9a\u4e49\u7684\u9519\u8bef<\/li>\n\n\n\n<li>\u7b2c16\u884c\uff0c\u8868\u793a\u6ca1\u6709\u5f02\u5e38\u60c5\u51b5\u4e0b\u6b63\u5e38\u6267\u884c\u8fd0\u7b97\uff0c\u8fd4\u56de\u7ed3\u679c\u548cnil<\/li>\n\n\n\n<li>\u7b2c19\u884c\uff1a\u4e3b\u51fd\u6570\u4e2d\u8c03\u7528\u8fd0\u7b97\u51fd\u6570\uff0c\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\uff0c\u6b64\u65f6\u9664\u6570\u4e3a0\uff0c\u51fd\u6570\u8fd4\u56de\u7684\u4e24\u4e2a\u503c\u5bf9\u5e94b\u548cc<\/li>\n\n\n\n<li>\u7b2c20\u884c\uff1a\u5982\u679c\u9664\u6570\u4e3a0\uff0c\u90a3\u4e48\u8fd4\u56de\u503c0\u5bf9\u5e94b\uff0cc\u5bf9\u5e94\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\uff0c\u56e0\u6b64\u5224\u65adb\u7b49\u4e8e0\u662f\uff0c\u6253\u5370c<\/li>\n\n\n\n<li>\u7b2c22\u884c\uff1a\u5982\u679c\u9664\u6570\u4e0d\u4e3a0\uff0c\u90a3\u4e48nil\u5bf9\u5e94c\uff0ccorrectResult\u5bf9\u5e94b\uff0c\u6b64\u65f6\u5224\u65adc\uff0c\u6253\u5370b<\/li>\n\n\n\n<li>\u7b2c25\u884c\uff1a\u6253\u5370\u81ea\u5b9a\u4e49\u9519\u8bef\u5b57\u6bb5\u53d8\u91cf\u7684\u7c7b\u578b<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1010\" height=\"149\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-7.png\" alt=\"\" class=\"wp-image-6720\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-7.png 1010w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-7-300x44.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-7-768x113.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/3-7-600x89.png 600w\" sizes=\"auto, (max-width: 1010px) 100vw, 1010px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u5982\u679cerror\u6ca1\u6709\u9519\u8bef\u8fd4\u56de\uff0c\u53ef\u76f4\u63a5\u8fd4\u56denil<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001\u8bbe\u7f6e\u9519\u8bef\u7684\u683c\u5f0f\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664errors\u5305\u5916\uff0c\u6807\u51c6\u5e93\u4e2d\u7684fmt \u5305\u8fd8\u63d0\u4f9b\u4e86\u65b9\u6cd5Errorf\uff0c\u53ef\u7528\u4e8e\u8bbe\u7f6e\u8fd4\u56de\u7684\u9519\u8bef\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\uff0c\u5c06\u591a\u4e2a\u503c\u5408\u5e76\u6210\u66f4\u6709\u610f\u4e49\u7684\u9519\u8bef\u5b57\u7b26\u4e32\uff0c \u4ece\u800c\u52a8\u6001\u5730\u521b\u5efa\u9519\u8bef\u5b57\u7b26\u4e32\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"220\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-8.png\" alt=\"\" class=\"wp-image-7225\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-8.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-8-300x93.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-8-600x187.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u540e\uff0c\u6253\u5370\u5982\u4e0b\u9519\u8bef\u5b57\u7b26\u4e32\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"76\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-8.png\" alt=\"\" class=\"wp-image-7227\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-8.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-8-300x32.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-8-600x65.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4\u3001\u4ece\u51fd\u6570\u8fd4\u56de\u9519\u8bef\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u8c03\u7528\u51fd\u6570\u65f6\u8fd4\u56de\u9519\u8bef\uff0c\u5728\u4e3b\u51fd\u6570\u4e2d\u5904\u7406\u9519\u8bef\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"356\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-8.png\" alt=\"\" class=\"wp-image-7230\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-8.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-8-300x151.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-8-600x301.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"87\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-7.png\" alt=\"\" class=\"wp-image-7231\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-7.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-7-300x37.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-7-600x74.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u7684\u4e00\u4e2a\u4f18\u70b9\uff1a\u9519\u8bef\u5904\u7406\u4e0d\u662f\u5728\u51fd\u6570\u4e2d\uff0c\u800c\u662f\u5728\u8c03\u7528\u51fd\u6570\u7684\u5730\u65b9\u8fdb\u884c\u7684\uff0c\u5728\u9519\u8bef\u5904\u7406\u65b9\u9762\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5\u3001\u5f02\u5e38\u7684\u6355\u83b7<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u4f7f\u7528defer + recover \u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff08\u8fd4\u56de\u9519\u8bef\u53d8\u91cf\uff09<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4f8b\u5b50\u6355\u83b7\u5f02\u5e38\uff0c\u5f02\u5e38\u4e3a\u9664\u6570\u4e0d\u80fd\u4e3a0\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"569\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1.png\" alt=\"\" class=\"wp-image-6727\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1.png 900w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-300x190.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-768x486.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-600x379.png 600w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c6\u884c\uff1a\u901a\u8fc7defer+recover\u8bed\u53e5\uff0c\u540e\u9762\u52a0\u533f\u540d\u51fd\u6570<\/li>\n\n\n\n<li>\u7b2c7\u884c\uff1arecover()\u4e3a\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u6355\u83b7\u5230\u5f02\u5e38<\/li>\n\n\n\n<li>\u7b2c8\u884c\uff1a\u5224\u65ad\u5982\u679c\u9519\u8bef\u4e0d\u4e3anil\uff0c\u90a3\u4e48\u6253\u5370err<\/li>\n\n\n\n<li>\u7b2c12-13\u884c\uff1a\u58f0\u660e\u53d8\u91cf\u5e76\u8d4b\u503c<\/li>\n\n\n\n<li>\u7b2c14\u884c\uff1a\u6267\u884c\u9664\u6cd5\u8fd0\u7b97\uff0c\u58f0\u660e\u53d8\u91cfresult\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9result<\/li>\n\n\n\n<li>\u7b2c19\u884c\uff1a\u8c03\u7528\u51fd\u6570\u6253\u5370\u7ed3\u679c<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"123\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2.png\" alt=\"\" class=\"wp-image-6729\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2.png 900w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-300x41.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-768x105.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-600x82.png 600w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u4e2d\u7684\u9519\u8bef\u53ef\u77e5\uff1a\u63d0\u793a\u9664\u6570\u4e0d\u80fd\u4e3a0\uff0carr1\u7684\u503c\u4e3a10\uff0carr2\u7684\u503c\u4e3a0\uff0crecover()\u6355\u83b7\u5230\u4e86\u6b64\u5f02\u5e38\uff0c\u5e76\u6253\u5370\u51fa\u6765<\/p>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong>\u5b95\u673a(panic)&#8211;\u7a0b\u5e8f\u7ec8\u6b62\u8fd0\u884c<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b95\u673a\u4e0d\u662f\u4e00\u4ef6\u5f88\u597d\u7684\u4e8b\u60c5\uff0c \u53ef\u80fd\u9020\u6210\u4f53\u9a8c\u505c\u6b62\u3001\u670d\u52a1\u4e2d\u65ad\uff0c\u4f46\u662f\uff0c\u5982\u679c\u5728\u635f\u5931\u53d1\u751f\u65f6\uff0c\u7a0b\u5e8f\u6ca1\u6709\u56e0\u4e3a\u5b95\u673a\u800c\u505c\u6b62\uff0c \u90a3\u4e48\u7528\u6237\u5c06\u4f1a\u4ed8\u51fa\u66f4\u5927\u7684\u4ee3\u4ef7\uff0c\u56e0\u6b64\uff0c \u770b\u673a\u6709\u65f6\u662f\u4e00\u79cd\u5408\u7406\u7684\u6b62\u635f\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u624b\u52a8\u89e6\u53d1\u5b95\u673a\uff0c\u8ba9\u7a0b\u5e8f\u5d29\u6124\uff0c \u8fd9\u6837\u5f00\u53d1\u8005\u53ef\u4ee5\u53ca\u65f6\u5730\u53d1\u73b0\u9519\u8bef\uff0c\u540c\u65f6\u51cf\u5c11\u53ef\u80fd\u7684\u635f\u5931\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u7a0b\u5e8f\u5728\u5b95\u673a\u65f6\uff0c\u4f1a\u5c06\u5806\u6808\u548cgoroutine \u4fe1\u606f\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff0c \u6240\u4ee5\u5b95\u673a\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u5730\u77e5\u6653\u53d1\u751f\u9519\u8bef\u7684\u4f4d\u7f6e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1\u3001\u624b\u52a8\u89e6\u53d1\u5b95\u673a\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u624b\u52a8\u89e6\u53d1\u5b95\u673a\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"907\" height=\"608\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3.png\" alt=\"\" class=\"wp-image-6735\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3.png 907w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-300x201.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-768x515.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-600x402.png 600w\" sizes=\"auto, (max-width: 907px) 100vw, 907px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c8\u884c\uff1a\u5b9a\u4e49\u51fd\u6570\uff0c\u8fd4\u56de\u7c7b\u578b\u4e3a\u9519\u8bef\u7c7b\u578b<\/li>\n\n\n\n<li>\u7b2c11\u884c\uff1a\u5224\u65ad\u53d8\u91cfarr2\u7684\u503c\u662f\u5426\u4e3a0\uff0c\u5982\u679c\u662f0 \uff0c\u629b\u51fa\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38<\/li>\n\n\n\n<li>\u7b2c14\u884c\uff1a\u5982\u679carr2\u4e0d\u4e3a0\u7684\u60c5\u51b5\u4e0b\uff0c\u6b63\u5e38\u6267\u884c\u8fd0\u7b97<\/li>\n\n\n\n<li>\u7b2c16\u884c\uff1a\u5982\u679c\u6b63\u5e38\u8fd0\u884c\uff0c\u90a3\u4e48\u51fd\u6570\u8fd4\u56denil<\/li>\n\n\n\n<li>\u7b2c20\u884c\uff1a\u5b9a\u4e49panic()\uff0c\u53c2\u6570\u4e3a\u51fd\u6570\u7c7b\u578b\uff0c\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u5c06\u89e6\u53d1\u5b95\u673a<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">panic()\u7684\u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"246\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4.png\" alt=\"\" class=\"wp-image-6739\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4.png 902w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-300x82.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-768x209.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-600x164.png 600w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u624b\u52a8\u5b95\u673a\u8fdb\u884c\u62a5\u9519\u7684\u65b9\u5f0f\u4e0d\u662f\u4e00\u79cd\u5077\u61d2\u7684\u65b9\u5f0f\uff0c\u53cd\u800c\u80fd\u8fc5\u901f\u62a5\u9519\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u7ee7\u7eed\u8fd0\u884c\uff0c\u9632\u6b62\u66f4\u5927\u7684\u9519\u8bef\u4ea7\u751f\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4efb\u4f55\u9519\u8bef\u90fd\u4f7f\u7528\u5b95\u673a\u5904\u7406\uff0c\u4e5f\u4e0d\u662f\u4e00\u79cd\u826f\u597d\u7684\u8bbe\u8ba1\u3002\u56e0\u6b64\u5e94\u6839\u636e\u9700\u8981\u6765\u51b3\u5b9a\u662f\u5426\u4f7f\u7528\u5b95\u673a\u8fdb\u884c\u62a5\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2\u3001\u5728\u5b95\u673a\u65f6\u89e6\u53d1\u5ef6\u8fdf\u6267\u884c\u8bed\u53e5\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53panic()\u89e6\u53d1\u7684\u5b95\u673a\u53d1\u751f\u65f6\uff0c panic()\u540e\u9762\u7684\u4ee3\u7801\u5c06\u4e0d\u4f1a\u88ab\u8fd0\u884c\uff0c\u4f46\u662f\u5728panic()\u51fd\u6570\u524d\u9762\u5df2\u7ecf\u8fd0\u884c\u8fc7\u7684defer \u8bed\u53e5\u4f9d\u7136\u4f1a\u5728\u5b95\u673a\u53d1\u751f\u65f6\u53d1\u751f\u4f5c\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\uff1a\u4fee\u6539\u4e0a\u9762\u7684\u8bed\u53e5\uff0c\u6dfb\u52a0defer\u8bed\u53e5\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"916\" height=\"662\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5.png\" alt=\"\" class=\"wp-image-6741\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5.png 916w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-300x217.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-768x555.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-600x434.png 600w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"292\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6.png\" alt=\"\" class=\"wp-image-6742\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6.png 903w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-300x97.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-768x248.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-600x194.png 600w\" sizes=\"auto, (max-width: 903px) 100vw, 903px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b95\u673a\u524d\uff0c defer \u8bed\u53e5\u4f1a\u4f18\u5148\u88ab\u6267\u884c<\/p>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong>\u5b95\u673a\u6062\u590d(recover)&#8211;\u9632\u6b62\u7a0b\u5e8f\u5954\u6e83<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0\u8bba\u662f\u4ee3\u7801\u8fd0\u884c\u9519\u8bef\u7531Runtime \u5c42\u629b\u51fa\u7684panic \u5d29\u6e0d\uff0c\u8fd8\u662f\u4e3b\u52a8\u89e6\u53d1\u7684panic \u5d29\u6e83\uff0c\u90fd\u53ef\u4ee5\u914d\u5408defer \u548crecover \u5b9e\u73b0\u9519\u8bef\u6355\u6349\u548c\u6062\u590d\uff0c\u8ba9\u4ee3\u7801\u5728\u53d1\u751f\u5d29\u6e83\u540e\u5141\u8bb8\u7ee7\u7eed\u8fd0\u884c\u3002panic\u7684\u53c2\u6570\u7c7b\u578b\u662finterface{}\uff0c\u56e0\u6b64\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u503c\uff0crecover()\u7684\u7c7b\u578b\u4e5f\u662finterface{}\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><strong>1<\/strong>\u3001\u8ba9\u7a0b\u5e8f\u5728\u5d29\u6e83\u65f6\u7ee7\u7eed\u6267\u884c\uff1a<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4ee3\u7801\u5b9e\u73b0\u4e86ProtectRun()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u6216\u95ed\u5305\u540e\u7684\u6267\u884c\u51fd\u6570\uff0c\u5f53\u4f20\u5165\u51fd\u6570\u4ee5\u4efb\u4f55\u5f62\u5f0f\u53d1\u751fpanic \u5d29\u6e83\u540e\uff0c\u53ef\u4ee5\u5c06\u5d29\u6e83\u53d1\u751f\u7684\u9519\u8bef\u6253\u5370\u51fa\u6765\uff0c\u540c\u65f6\u5141\u8bb8\u540e\u9762\u7684\u4ee3\u7801\u7ee7\u7eed\u8fd0\u884c\uff0c\u4e0d\u4f1a\u9020\u6210\u6574\u4e2a\u8fdb\u7a0b\u7684\u5d29\u6e83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u5954\u6e83\u65f6\u4f1a\u7ee7\u7eed\u8fd0\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"909\" height=\"660\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7.png\" alt=\"\" class=\"wp-image-6749\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7.png 909w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-300x218.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-768x558.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-600x436.png 600w\" sizes=\"auto, (max-width: 909px) 100vw, 909px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"512\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/10.png\" alt=\"\" class=\"wp-image-6754\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/10.png 908w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/10-300x169.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/10-768x433.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/10-600x338.png 600w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c8\u884c\uff1a\u58f0\u660e\u63cf\u8ff0\u9519\u8bef\u7684\u7ed3\u6784\u4f53\uff0c\u6210\u5458\u4fdd\u5b58\u9519\u8bef\u7684\u6267\u884c\u51fd\u6570<\/li>\n\n\n\n<li>\u7b2c13\u884c\uff1a\u4f7f\u7528defer \u5c06\u95ed\u5305\u5ef6\u8fdf\u6267\u884c\uff0c\u5f53panic \u89e6\u53d1\u5d29\u6e83\u65f6\uff0c ProtectRun()\u51fd\u6570\u5c06\u7ed3\u675f\u8fd0\u884c\uff0c\u6b64\u65f6defer \u540e\u7684\u95ed\u5305\u5c06\u4f1a\u53d1\u751f\u8c03\u7528\u3002<\/li>\n\n\n\n<li>\u7b2c14\u884c\uff1arecover()\u83b7\u53d6\u5230panic \u4f20\u5165\u7684\u53c2\u6570<\/li>\n\n\n\n<li>\u7b2c15\u884c\uff1a\u4f7f\u7528switch \u5bf9err\u53d8\u91cf\u8fdb\u884c\u7c7b\u578b\u65ad\u8a00<\/li>\n\n\n\n<li>\u7b2c16\u884c\uff1a\u5982\u679c\u9519\u8bef\u662f\u6709Runtime \u5c42\u629b\u51fa\u7684\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u5982\u7a7a\u6307\u9488\u8bbf\u95ee\u3001\u9664\u6570\u4e3a0\u7b49\u60c5\u51b5\uff0c\u6253\u5370\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/li>\n\n\n\n<li>\u7b2c18\u884c\uff1a\u5176\u4ed6\u9519\u8bef\uff0c \u6253\u5370\u4f20\u9012\u8fc7\u6765\u7684\u9519\u8bef\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u7b2c28\u884c\uff1a\u4f7f\u7528panic \u624b\u52a8\u89e6\u53d1\u4e00\u4e2a\u9519\u8bef\uff0c \u4e95\u5c06\u4e00\u4e2a\u7ed3\u6784\u4f53\u9644\u5e26\u4fe1\u606f\u4f20\u9012\u8fc7\u53bb\uff0c\u6b64\u65f6\uff0crecover \u5c31\u4f1a\u83b7\u53d6\u5230\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4fe1\u606f\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/li>\n\n\n\n<li>\u7b2c36\u884c\uff1a\u6a21\u62df\u4ee3\u7801\u4e2d\u7a7a\u6307\u9488\u8d4b\u503c\u9020\u6210\u7684\u9519\u8bef\uff0c\u6b64\u65f6\u4f1a\u7531Runtime \u5c42\u629b\u51fa\u9519\u8bef\uff0c\u88abProtectRun()\u51fd\u6570\u7684recover()\u51fd\u6570\u6355\u83b7\u5230\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">2\u3001\u5728panic\u671f\u95f4\uff0c\u5ef6\u8fdf\u51fd\u6570\u53ef\u6b63\u5e38\u6267\u884c<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728panic\u671f\u95f4\uff0c\u4efb\u4f55\u5ef6\u8fdf\u7684\u51fd\u6570\u8c03\u7528\u90fd\u5c06\u5b8c\u6210\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u4e2d\u653e\u7f6e\u4e00\u4e2arecover\u8c03\u7528\uff0c\u5e76\u5728\u5f15\u53d1panic\u7684\u4ee3\u7801\u4e4b\u524d\u4f7f\u7528defer\u8c03\u7528\u8be5\u51fd\u6570<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"697\" height=\"387\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/9-1.png\" alt=\"\" class=\"wp-image-14800\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/9-1.png 697w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/9-1-300x167.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/9-1-600x333.png 600w\" sizes=\"auto, (max-width: 697px) 100vw, 697px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"81\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/8-1.png\" alt=\"\" class=\"wp-image-14798\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/8-1.png 695w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/8-1-300x35.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/8-1-600x70.png 600w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u770b\u51fa\uff0c\u7b2c12\u884c\u6ca1\u6709\u6253\u5370\u51fa\u6765\uff0c\u53ea\u6709\u7b2c16\u884c\u6253\u5370\u4e86\uff0c\u56e0\u4e3a\u4ea7\u751fpanic\u7684\u51fd\u6570\u5c06\u7acb\u5373\u8fd4\u56de\uff0c\u800c\u8be5\u51fd\u6570\u5757\u4e2dpanic\u4e4b\u540e\u7684\u4efb\u4f55\u4ee3\u7801\u90fd\u4e0d\u4f1a\u6267\u884c\u3002\u4f46\u662f\uff0c\u5728\u4ea7\u751fpanic\u7684\u51fd\u6570\u8fd4\u56de\u4e4b\u540e\uff0c\u6b63\u5e38\u7684\u6267\u884c\u5c06\u6062\u590d\uff0c\u56e0\u6b64main\u51fd\u6570\u4e2dtest1()\u540e\u9762\u7684\u5c06\u7ee7\u7eed\u6267\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">3\u3001panic\u503c\u4ecerecover\u4e2d\u8fd4\u56de<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6ca1\u6709\u51fa\u73b0panic\uff0c\u76f4\u63a5\u6253\u5370recover()\u5c06\u4f1a\u5f97\u5230nil\uff0c\u4f46\u662f\u5f53\u51fa\u73b0panic\u65f6\uff0crecover\u8fd4\u56de\u4f20\u9012\u7ed9panic\u7684\u4efb\u4f55\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u6253\u5370recover()\u7684\u503c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"274\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/1-7.png\" alt=\"\" class=\"wp-image-14803\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/1-7.png 698w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/1-7-300x118.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/1-7-600x236.png 600w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"701\" height=\"65\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/2-6.png\" alt=\"\" class=\"wp-image-14804\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/2-6.png 701w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/2-6-300x28.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/2-6-600x56.png 600w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001panic\u4e2d\u4f20\u9012err\u540e\u8c03\u7528Error()\u62a5\u9519<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136panic\u548crecover\u7684\u53c2\u6570\u90fd\u662f\u7a7a\u63a5\u53e3\uff0c\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u7684\u503c\uff0c\u4f46\u662f\u5982\u679c\u4f20\u9012\u4e86error\u540e\uff0c\u6b64\u65f6\u7684\u53c2\u6570\u662finterface{}\uff0c\u662f\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528Error()\u7684\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5176\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u4e3aerror\u540e\u624d\u53ef\u8c03\u7528\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"317\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/3-5.png\" alt=\"\" class=\"wp-image-14808\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/3-5.png 696w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/3-5-300x137.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/3-5-600x273.png 600w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"697\" height=\"79\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/4-6.png\" alt=\"\" class=\"wp-image-14809\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/4-6.png 697w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/4-6-300x34.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/4-6-600x68.png 600w\" sizes=\"auto, (max-width: 697px) 100vw, 697px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">panic \u548crecover \u7684\u5173\u7cfb:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6709panic \u6ca1recover \uff0c \u7a0b\u5e8f\u5ca9\u673a<\/li>\n\n\n\n<li>\u6709panic \u4e5f\u6709recover \u6355\u83b7\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u5b95\u673a\u673a\u3002\u6267\u884c\u5b8c\u5bf9\u5e94\u7684defer \u540e\uff0c\u4ece\u5b95\u673a\u673a\u70b9\u9000\u51fa\uff0c\u5f53\u524d\u51fd\u6570\u540e\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u867d\u7136panic\/recover \u80fd\u6a21\u62df\u5176\u4ed6\u8bed\u8a00\u7684\u5f02\u5e38\u673a\u5236\uff0c\u4f46\u5e76\u4e0d\u5efa\u8bae\u7f16\u5199\u666e\u901a\u51fd\u6570\u4e5f\u7ecf\u5e38\u6027\u4f7f\u7528\u8fd9\u79cd\u7279\u6027\u3002\u5728panic \u89e6\u53d1\u7684defer \u51fd\u6570\u5185\uff0c\u53ef\u4ee5\u7ee7\u7eed\u8c03\u7528panic \uff0c\u8fdb\u4e00\u6b65\u5c06\u9519\u8bef\u5916\u629b\u76f4\u5230\u7a0b\u5e8f\u6574\u4f53\u5d29\u6e83\u3002\u5982\u679c\u60f3\u5728\u6355\u83b7\u9519\u8bef\u65f6\u8bbe\u7f6e\u5f53\u524d\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u5bf9\u8fd4\u56de\u503c\u4f7f\u7528\u547d\u540d\u8fd4\u56de\u503c\u65b9\u5f0f\u76f4\u63a5\u8fdb\u884c\u8bbe\u7f6e<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u4ec5\u5f53\u7a0b\u5e8f\u65e0\u6cd5\u4ece\u9519\u8bef\u4e2d\u6062\u590d\u65f6\uff0c\u624d\u4f7f\u7528panic \u3002panic \u662f\u6ca1\u6709\u529e\u6cd5\u7684\u529e\u6cd5\uff0c\u4ec5\u5f53\u8ba9\u7a0b\u5e8f\u5d29\u6e83\u662f\u6700\u8d1f\u8d23\u4efb\u7684\u9009\u62e9\u65f6\u624d\u5e94\u4f7f\u7528panic<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go \u8bed\u8a00\u8ffd\u6c42\u7b80\u6d01\u4f18\u96c5\uff0c\u6240\u4ee5\uff0cGo \u8bed\u8a00\u4e0d\u652f\u6301\u4f20\u7edf\u7684 try\u2026catch\u2026finally \u8fd9\u79cd\u5904\u7406\u3002 Go \u4e2d [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":6697,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-6696","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/6696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6696"}],"version-history":[{"count":53,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/6696\/revisions"}],"predecessor-version":[{"id":14810,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/6696\/revisions\/14810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/6697"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}