{"id":3213,"date":"2025-03-25T23:58:59","date_gmt":"2025-03-25T15:58:59","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3213"},"modified":"2025-03-25T23:58:59","modified_gmt":"2025-03-25T15:58:59","slug":"go-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/25\/go-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86\/","title":{"rendered":"Go \u9519\u8bef\u5904\u7406"},"content":{"rendered":"\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u9519\u8bef\uff08Error\uff09\u5904\u7406\u662f\u901a\u8fc7\u8fd4\u56de\u9519\u8bef\u5bf9\u8c61\u6765\u8fdb\u884c\u7684\uff0cGo \u8bed\u8a00\u5e76\u4e0d\u4f7f\u7528\u5f02\u5e38\u673a\u5236\u3002\u9519\u8bef\u5904\u7406\u7684\u57fa\u672c\u7406\u5ff5\u662f\u663e\u5f0f\u5730\u8fd4\u56de\u9519\u8bef\uff0c\u5e76\u4e14\u8c03\u7528\u8005\u9700\u8981\u68c0\u67e5\u8fd9\u4e9b\u9519\u8bef\u3002\u8fd9\u79cd\u65b9\u5f0f\u9f13\u52b1\u5f00\u53d1\u8005\u5728\u51fd\u6570\u8c03\u7528\u540e\u7acb\u5373\u5904\u7406\u9519\u8bef\uff0c\u589e\u52a0\u4e86\u7a0b\u5e8f\u7684\u53ef\u9884\u6d4b\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u9519\u8bef\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u9519\u8bef\u901a\u5e38\u662f\u901a\u8fc7\u5b9e\u73b0 <code>error<\/code> \u63a5\u53e3\u7684\u7c7b\u578b\u6765\u8868\u793a\u7684\u3002Go \u8bed\u8a00\u5185\u7f6e\u4e86 <code>error<\/code> \u7c7b\u578b\uff0c\u5b83\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5305\u542b\u4e00\u4e2a <code>Error()<\/code> \u65b9\u6cd5\uff0c\u8fd4\u56de\u4e00\u4e2a\u63cf\u8ff0\u9519\u8bef\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 <code>error<\/code> \u63a5\u53e3<\/strong><\/h3>\n\n\n\n<p><code>error<\/code> \u63a5\u53e3\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type error interface {\n    Error() string\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u6765\u5b9e\u73b0 <code>error<\/code> \u63a5\u53e3\uff0c\u521b\u5efa\u66f4\u5177\u8bed\u4e49\u5316\u7684\u9519\u8bef\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\ntype MyError struct {\n    Code    int\n    Message string\n}\n\n\/\/ \u5b9e\u73b0 Error() \u65b9\u6cd5\nfunc (e *MyError) Error() string {\n    return fmt.Sprintf(\"Code: %d, Message: %s\", e.Code, e.Message)\n}\n\nfunc doSomething() error {\n    return &amp;MyError{Code: 404, Message: \"Not Found\"}\n}\n\nfunc main() {\n    err := doSomething()\n    if err != nil {\n        fmt.Println(\"Error:\", err) \/\/ \u8f93\u51fa: Error: Code: 404, Message: Not Found\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u9519\u8bef\u5904\u7406\u6a21\u5f0f<\/strong><\/h2>\n\n\n\n<p>Go \u4e2d\u7684\u9519\u8bef\u5904\u7406\u901a\u5e38\u901a\u8fc7\u8fd4\u56de\u503c\u7684\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\u3002\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u5176\u4e2d\u5305\u62ec\u4e00\u4e2a\u7c7b\u578b\u4e3a <code>error<\/code> \u7684\u503c\uff0c\u7528\u6765\u8868\u793a\u51fd\u6570\u662f\u5426\u6267\u884c\u6210\u529f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u68c0\u67e5\u9519\u8bef<\/strong><\/h3>\n\n\n\n<p>Go \u4e2d\u7684\u60ef\u7528\u505a\u6cd5\u662f\u5728\u8c03\u7528\u51fd\u6570\u540e\u7acb\u5373\u68c0\u67e5\u9519\u8bef\u503c\u3002\u5982\u679c\u9519\u8bef\u503c\u4e3a <code>nil<\/code>\uff0c\u8bf4\u660e\u64cd\u4f5c\u6210\u529f\uff1b\u5982\u679c\u9519\u8bef\u503c\u4e0d\u4e3a <code>nil<\/code>\uff0c\u5219\u8bf4\u660e\u53d1\u751f\u4e86\u9519\u8bef\uff0c\u8c03\u7528\u8005\u5e94\u8be5\u5904\u7406\u5b83\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"errors\"\n)\n\nfunc divide(a, b int) (int, error) {\n    if b == 0 {\n        return 0, errors.New(\"division by zero\") \/\/ \u8fd4\u56de\u9519\u8bef\n    }\n    return a \/ b, nil \/\/ \u8fd4\u56de\u7ed3\u679c\u548c nil \u9519\u8bef\n}\n\nfunc main() {\n    result, err := divide(10, 0)\n    if err != nil {\n        fmt.Println(\"Error:\", err) \/\/ \u8f93\u51fa: Error: division by zero\n    } else {\n        fmt.Println(\"Result:\", result)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u4f20\u9012\u9519\u8bef<\/strong><\/h3>\n\n\n\n<p>Go \u9519\u8bef\u5904\u7406\u4e2d\u7684\u53e6\u4e00\u4e2a\u5e38\u89c1\u6a21\u5f0f\u662f\u5c06\u9519\u8bef\u4f20\u9012\u7ed9\u8c03\u7528\u8005\u3002\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u4e00\u4e2a\u51fd\u6570\u65e0\u6cd5\u5904\u7406\u9519\u8bef\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de\u9519\u8bef\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u4e0a\u4e00\u7ea7\u8c03\u7528\u8005\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"errors\"\n)\n\nfunc step1() error {\n    return errors.New(\"Step 1 failed\")\n}\n\nfunc step2() error {\n    return step1() \/\/ \u5c06\u9519\u8bef\u4f20\u9012\u5230\u4e0a\u7ea7\n}\n\nfunc main() {\n    if err := step2(); err != nil {\n        fmt.Println(\"Error:\", err) \/\/ \u8f93\u51fa: Error: Step 1 failed\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u9519\u8bef\u5305\u88c5<\/strong><\/h2>\n\n\n\n<p>Go 1.13 \u5f15\u5165\u4e86\u9519\u8bef\u5305\u88c5\uff08Error Wrapping\uff09\u3002\u8fd9\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u539f\u59cb\u9519\u8bef\u57fa\u7840\u4e0a\u9644\u52a0\u66f4\u591a\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4ece\u800c\u66f4\u597d\u5730\u8ffd\u8e2a\u9519\u8bef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 <code>fmt.Errorf<\/code> \u548c\u9519\u8bef\u5305\u88c5<\/strong><\/h3>\n\n\n\n<p><code>fmt.Errorf<\/code> \u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5e26\u6709\u683c\u5f0f\u5316\u4fe1\u606f\u7684\u9519\u8bef\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 <code>%w<\/code> \u6765\u5305\u88c5\u73b0\u6709\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"errors\"\n)\n\nfunc doSomething() error {\n    originalErr := errors.New(\"original error\")\n    return fmt.Errorf(\"wrapped error: %w\", originalErr) \/\/ \u9519\u8bef\u5305\u88c5\n}\n\nfunc main() {\n    err := doSomething()\n    if err != nil {\n        fmt.Println(\"Error:\", err) \/\/ \u8f93\u51fa: Error: wrapped error: original error\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 <code>errors.Is<\/code> \u548c <code>errors.As<\/code><\/strong><\/h3>\n\n\n\n<p><code>errors.Is<\/code> \u548c <code>errors.As<\/code> \u662f Go 1.13 \u5f15\u5165\u7684\u4e24\u4e2a\u65b0\u65b9\u6cd5\uff0c\u7528\u4e8e\u89e3\u5305\u6216\u68c0\u67e5\u9519\u8bef\u3002<code>errors.Is<\/code> \u68c0\u67e5\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u7279\u5b9a\u7684\u9519\u8bef\uff0c<code>errors.As<\/code> \u7528\u6765\u5c06\u9519\u8bef\u7c7b\u578b\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.2.1 <code>errors.Is<\/code><\/strong><\/h4>\n\n\n\n<p><code>errors.Is<\/code> \u7528\u4e8e\u5224\u65ad\u67d0\u4e2a\u9519\u8bef\u662f\u5426\u4e0e\u76ee\u6807\u9519\u8bef\u76f8\u540c\uff0c\u6216\u8005\u662f\u76ee\u6807\u9519\u8bef\u7684\u5305\u88c5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"errors\"\n)\n\nvar ErrNotFound = errors.New(\"not found\")\n\nfunc find() error {\n    return fmt.Errorf(\"some error: %w\", ErrNotFound) \/\/ \u5305\u88c5 ErrNotFound\n}\n\nfunc main() {\n    err := find()\n    if errors.Is(err, ErrNotFound) {\n        fmt.Println(\"Error: Not Found\") \/\/ \u8f93\u51fa: Error: Not Found\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.2.2 <code>errors.As<\/code><\/strong><\/h4>\n\n\n\n<p><code>errors.As<\/code> \u7528\u4e8e\u5c06\u9519\u8bef\u65ad\u8a00\u4e3a\u67d0\u4e2a\u5177\u4f53\u7c7b\u578b\uff0c\u901a\u5e38\u7528\u4e8e\u83b7\u53d6\u5305\u88c5\u9519\u8bef\u4e2d\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"errors\"\n)\n\ntype MyError struct {\n    Code    int\n    Message string\n}\n\nfunc (e *MyError) Error() string {\n    return fmt.Sprintf(\"Code: %d, Message: %s\", e.Code, e.Message)\n}\n\nfunc doSomething() error {\n    return &amp;MyError{Code: 404, Message: \"Not Found\"}\n}\n\nfunc main() {\n    err := doSomething()\n    var myErr *MyError\n    if errors.As(err, &amp;myErr) {\n        fmt.Println(\"MyError:\", myErr) \/\/ \u8f93\u51fa: MyError: Code: 404, Message: Not Found\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u9519\u8bef\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u663e\u5f0f\u68c0\u67e5\u9519\u8bef<\/strong>\uff1a\u603b\u662f\u68c0\u67e5\u51fd\u6570\u8fd4\u56de\u7684\u9519\u8bef\uff0c\u907f\u514d\u5ffd\u7565\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u7acb\u5373\u5904\u7406\u9519\u8bef<\/strong>\uff1a\u5728\u51fd\u6570\u8c03\u7528\u540e\u7acb\u5373\u5904\u7406\u9519\u8bef\uff0c\u907f\u514d\u9519\u8bef\u4f20\u64ad\u5230\u540e\u7eed\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u9519\u8bef\u4fe1\u606f<\/strong>\uff1a\u5c3d\u91cf\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4f8b\u5982\uff1a\u9519\u8bef\u7801\u3001\u9519\u8bef\u539f\u56e0\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u9519\u8bef\u5305\u88c5<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u5305\u88c5\u539f\u59cb\u9519\u8bef\u5e76\u6dfb\u52a0\u66f4\u591a\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4ee5\u4fbf\u66f4\u5bb9\u6613\u5730\u8ffd\u8e2a\u548c\u5b9a\u4f4d\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Go \u9519\u8bef\u5904\u7406\u673a\u5236<\/strong>\uff1aGo \u4f7f\u7528\u8fd4\u56de\u9519\u8bef\u503c\u7684\u65b9\u5f0f\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u6ca1\u6709\u5f02\u5e38\u673a\u5236\u3002\u51fd\u6570\u8fd4\u56de\u7684\u9519\u8bef\u503c\u53ef\u4ee5\u662f <code>nil<\/code> \u6216\u8005\u5177\u4f53\u7684\u9519\u8bef\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u63a5\u53e3<\/strong>\uff1aGo \u8bed\u8a00\u7684\u9519\u8bef\u662f\u901a\u8fc7\u5b9e\u73b0 <code>error<\/code> \u63a5\u53e3\u7684\u7c7b\u578b\u6765\u8868\u793a\u7684\uff0c\u63a5\u53e3\u53ea\u5305\u542b\u4e00\u4e2a <code>Error()<\/code> \u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u5305\u88c5<\/strong>\uff1aGo 1.13 \u53ca\u4ee5\u540e\u652f\u6301\u9519\u8bef\u5305\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>fmt.Errorf<\/code> \u6765\u521b\u5efa\u5e26\u6709\u4e0a\u4e0b\u6587\u7684\u9519\u8bef\uff0c\u5e76\u4f7f\u7528 <code>errors.Is<\/code> \u548c <code>errors.As<\/code> \u6765\u89e3\u5305\u6216\u68c0\u67e5\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u663e\u5f0f\u68c0\u67e5\u9519\u8bef\uff0c\u7acb\u5373\u5904\u7406\u9519\u8bef\uff0c\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4f7f\u7528\u9519\u8bef\u5305\u88c5\u6765\u589e\u5f3a\u53ef\u8c03\u8bd5\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 \u53c2\u8003\u8d44\u6599<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/golang.org\/doc\/effective_go.html#errors\">Go \u5b98\u65b9\u6587\u6863 &#8211; \u9519\u8bef\u5904\u7406<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/golang\/go\/wiki\/ErrorHandling\">Go Wiki &#8211; \u9519\u8bef\u5904\u7406<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u9519\u8bef\uff08Error\uff09\u5904\u7406\u662f\u901a\u8fc7\u8fd4\u56de\u9519\u8bef\u5bf9\u8c61\u6765\u8fdb\u884c\u7684\uff0cGo \u8bed\u8a00\u5e76\u4e0d\u4f7f\u7528\u5f02\u5e38\u673a\u5236\u3002\u9519\u8bef\u5904\u7406\u7684\u57fa\u672c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3214,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3213"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3213\/revisions"}],"predecessor-version":[{"id":3215,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3213\/revisions\/3215"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3214"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}