{"id":564,"date":"2020-03-03T17:15:00","date_gmt":"2020-03-03T09:15:00","guid":{"rendered":"https:\/\/www.myway5.com\/?p=564"},"modified":"2023-07-05T21:31:51","modified_gmt":"2023-07-05T13:31:51","slug":"golang-error","status":"publish","type":"post","link":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/","title":{"rendered":"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae"},"content":{"rendered":"<h2>\u4e00\u3001\u6982\u8ff0<\/h2>\n<p>Golang \u7684\u9519\u8bef\u5904\u7406\u4e00\u76f4\u662f\u4e00\u4e2a\u6bd4\u8f83\u8ba8\u8bba\u6bd4\u8f83\u591a\u7684\u8bdd\u3002\u6211\u521a\u63a5\u89e6 Golang \u7684\u65f6\u5019\u4e5f\u770b\u8fc7\u5173\u4e8e\u9519\u8bef\u5904\u7406\u7684\u4e00\u4e9b\u6587\u6863\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u653e\u5728\u5fc3\u4e0a\u3002\u5728\u6211\u4f7f\u7528 Golang \u4e00\u6bb5\u65f6\u95f4\u4e4b\u540e\uff0c\u6211\u89c9\u5f97\u6211\u53ef\u80fd\u65e0\u6cd5\u5ffd\u7565\u8fd9\u4e2a\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u4e3a\u4e86\u6574\u7406\u4e00\u4e9b\u5728 Golang \u4e2d\u5e38\u7528\u7684\u9519\u8bef\u5904\u7406\u6280\u5de7\u548c\u539f\u5219\u3002<\/p>\n<h2>\u4e8c\u3001\u9519\u8bef\u5904\u7406\u7684\u6280\u5de7\u548c\u539f\u5219<\/h2>\n<h3>2.1 \u4f7f\u7528\u5c01\u88c5\u6765\u907f\u514d\u91cd\u590d\u7684\u9519\u8bef\u5224\u65ad<\/h3>\n<p>\u5728 Golang \u7684\u9879\u76ee\u4e2d\uff0c\u6700\u591a\u7684\u4e00\u53e5\u4ee3\u7801\u80af\u5b9a\u662f <code>if err != nil<\/code>\u3002Golang \u5c06\u9519\u8bef\u4f5c\u4e3a\u8fd4\u56de\u503c\uff0c\u56e0\u6b64\u4f60\u4e0d\u5f97\u4e0d\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002\u4f46\u662f\u6709\u7684\u65f6\u5019\uff0c\u9519\u8bef\u5904\u7406\u7684\u5224\u65ad\u53ef\u80fd\u4f1a\u5360\u636e\u4f60\u7684\u4ee3\u7801\u7684\u4e00\u534a\u7bc7\u5e45\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u770b\u8d77\u6765\u4e71\u7cdf\u7cdf\u7684\u3002\u5728\u5b98\u65b9\u7684\u535a\u5ba2\u4e2d\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">_, err = fd.Write(p0[a:b])\nif err != nil {\n    return err\n}\n_, err = fd.Write(p1[c:d])\nif err != nil {\n    return err\n}\n_, err = fd.Write(p2[e:f])\nif err != nil {\n    return err\n}\n\/\/ and so on\n<\/code><\/pre>\n<p>\u662f\u7684\uff0c\u4f60\u6ca1\u6709\u770b\u9519\uff0c\u8fd9\u91cc\u5176\u5b9e\u5c31\u662f\u8c03\u7528\u4e86 3 \u884c <code>fd.Write<\/code>\uff0c\u4f46\u662f\u4f60\u4e0d\u5f97\u4e0d\u5199\u4e0a 9 \u9519\u8bef\u5224\u65ad\u3002\u56e0\u6b64\u5b98\u65b9\u7684\u535a\u5ba2\u4e2d\u4e5f\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6bd4\u8f83\u4f18\u96c5\u7684\u5904\u7406\u65b9\u6848\uff1a\u5c06 <code>io.Writer<\/code> \u518d\u5c01\u88c5\u4e00\u5c42\u3002<\/p>\n<pre><code class=\"language-go line-numbers\">type errWriter struct {\n    w   io.Writer\n    err error\n}\n\nfunc (ew *errWriter) write(buf []byte) {\n    if ew.err != nil {\n        return\n    }\n    _, ew.err = ew.w.Write(buf)\n}\n\new := &amp;errWriter{w: fd}\new.write(p0[a:b])\new.write(p1[c:d])\new.write(p2[e:f])\n\/\/ and so on\nif ew.err != nil {\n    return ew.err\n}\n<\/code><\/pre>\n<p>\u73b0\u5728\u770b\u4e0a\u53bb\u5c31\u597d\u591a\u4e86\uff0c<code>write(buf []byte)<\/code> \u65b9\u6cd5\u5728\u5185\u90e8\u5224\u65ad\u4e86\u9519\u8bef\u503c\uff0c\u6765\u907f\u514d\u5728\u5916\u9762\u591a\u6b21\u7684\u9519\u8bef\u5224\u65ad\u3002\u5f53\u7136\uff0c\u8fd9\u79cd\u5199\u6cd5\u53ef\u80fd\u4e5f\u6709\u5b83\u7684\u5f0a\u7aef\uff0c\u6bd4\u5982\u4f60\u6ca1\u6709\u529e\u6cd5\u77e5\u9053\u51fa\u9519\u5728\u54ea\u4e00\u884c\u8c03\u7528\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ea\u9700\u8981\u68c0\u67e5\u9519\u8bef\uff0c\u7136\u540e\u8fdb\u884c\u5904\u7406\u800c\u5df2\u3002\u56e0\u6b64\u8fd9\u79cd\u6280\u5de7\u8fd8\u662f\u5f88\u6709\u7528\u7684\u3002Golang \u7684\u6807\u51c6\u5e93\u4e5f\u6709\u5f88\u591a\u7c7b\u4f3c\u7684\u6280\u5de7\u3002\u6bd4\u5982<\/p>\n<pre><code class=\"language-go line-numbers\">b := bufio.NewWriter(fd)\nb.Write(p0[a:b])\nb.Write(p1[c:d])\nb.Write(p2[e:f])\n\/\/ and so on\nif b.Flush() != nil {\n    return b.Flush()\n}\n<\/code><\/pre>\n<p>\u5176\u4e2d <code>b.Write<\/code> \u662f\u6709\u9519\u8bef\u503c\u8fd4\u56de\u7684\uff0c\u8fd9\u53ea\u662f\u4e3a\u4e86\u7b26\u5408 <code>io.Writer<\/code> \u63a5\u53e3\u3002\u4f60\u53ef\u4ee5\u5728\u8c03\u7528 <code>b.Flush()<\/code> \u7684\u65f6\u5019\u518d\u8fdb\u884c\u9519\u8bef\u503c\u7684\u5224\u65ad\u3002<\/p>\n<h3>2.2 Golang 1.13 \u524d\u7684\u9519\u8bef\u5904\u7406<\/h3>\n<h4>\u68c0\u9a8c\u9519\u8bef<\/h4>\n<p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5bf9\u9519\u8bef\u8fdb\u884c\u7b80\u5355\u7684\u5224\u65ad\u5373\u53ef\u3002\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\u5bf9\u9519\u8bef\u505a\u5176\u4ed6\u7684\u5904\u7406\uff0c\u53ea\u9700\u8981\u4fdd\u8bc1\u4ee3\u7801\u903b\u8f91\u6b63\u786e\u6267\u884c\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-go line-numbers\">if err != nil {\n    \/\/ something went wrong\n}\n<\/code><\/pre>\n<p>\u4f46\u6709\u7684\u65f6\u5019\u6211\u4eec\u9700\u8981\u6839\u636e\u9519\u8bef\u7c7b\u578b\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff0c\u6bd4\u5982\u7f51\u7edc\u8fde\u63a5\u672a\u8fde\u63a5\/\u65ad\u5f00\u5bfc\u81f4\u7684\u9519\u8bef\uff0c\u6211\u4eec\u5e94\u8be5\u5728\u5224\u65ad\u662f\u672a\u8fde\u63a5\/\u65ad\u5f00\u65f6\uff0c\u8fdb\u884c\u91cd\u8fde\u64cd\u4f5c\u3002 \u5728\u6d89\u53ca\u5230\u9519\u8bef\u7c7b\u578b\u7684\u5224\u65ad\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u6709\u4e24\u79cd\u65b9\u6cd5<\/p>\n<ol>\n<li>\u5c06\u9519\u8bef\u548c\u5df2\u77e5\u7684\u503c\u8fdb\u884c\u6bd4\u5bf9<\/li>\n<\/ol>\n<pre><code class=\"language-go line-numbers\">var ErrNotFound = errors.New(\"not found\")\n\nif err == ErrNotFound {\n    \/\/ something wasn't found\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5224\u65ad\u9519\u8bef\u7684\u5177\u4f53\u7c7b\u578b<\/li>\n<\/ol>\n<pre><code class=\"language-go line-numbers\">type NotFoundError struct {\n    Name string\n}\n\nfunc (e *NotFoundError) Error() string { return e.Name + \": not found\" }\n\nif e, ok := err.(*NotFoundError); ok {\n    \/\/ e.Name wasn't found\n}\n<\/code><\/pre>\n<h4>\u6dfb\u52a0\u4fe1\u606f<\/h4>\n<p>\u5f53\u4e00\u4e2a\u9519\u8bef\u5728\u7ecf\u8fc7\u591a\u5c42\u7684\u8c03\u7528\u6808\u5411\u4e0a\u8fd4\u56de\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5728\u8fd9\u4e2a\u9519\u8bef\u4e0a\u6dfb\u52a0\u4e00\u4e9b\u989d\u5916\u7684\u4fe1\u606f\uff0c\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5224\u65ad\u9519\u8bef\u51fa\u73b0\u65f6\u7a0b\u5e8f\u8fd0\u884c\u5230\u4e86\u54ea\u91cc\uff0c\u53d1\u751f\u4e86\u4ec0\u4e48\u3002\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\uff0c\u4f7f\u7528\u4e4b\u524d\u7684\u9519\u8bef\u4fe1\u606f\u6784\u9020\u65b0\u7684\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">if err != nil {\n    return fmt.Errorf(\"decompress %v: %v\", name, err)\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528 <code>fmt.Errorf<\/code> \u53ea\u4fdd\u7559\u4e86\u4e0a\u4e00\u4e2a\u9519\u8bef\u7684\u6587\u672c\uff0c\u4e22\u5f03\u4e86\u5176\u4ed6\u6240\u6709\u7684\u4fe1\u606f\u3002\u5982\u679c\u6211\u4eec\u60f3\u4fdd\u7559\u4e0a\u4e00\u4e2a\u9519\u8bef\u7684\u6240\u6709\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">type QueryError struct {\n    Query string\n    Err   error\n}\n\nif e, ok := err.(*QueryError); ok &amp;&amp; e.Err == ErrPermission {\n    \/\/ query failed because of a permission problem\n}\n<\/code><\/pre>\n<h3>2.3 Golang 1.13 \u4e2d\u7684\u9519\u8bef\u5904\u7406<\/h3>\n<p>Golang 1.13 \u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u9519\u8bef\u5305\u542b\u4e86\u53e6\u4e00\u4e2a\u9519\u8bef\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 <code>Unwrap()<\/code> \u65b9\u6cd5\u6765\u8fd4\u56de\u5e95\u5c42\u7684\u9519\u8bef\u3002\u5982\u679c <code>e1.Unwrap()<\/code> \u8fd4\u56de\u4e86 e2\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bf4 e1 \u5305\u542b\u4e86 e2\u3002<\/p>\n<h4>\u4f7f\u7528 Is \u548c As \u6765\u68c0\u9a8c\u9519\u8bef<\/h4>\n<p>\u5728 2.2 \u4e2d\u63d0\u5230\u4e86\u9519\u8bef\u4fe1\u606f\u7684\u5e38\u89c1\u5904\u7406\u65b9\u5f0f\uff0c\u5728 Golang 1.13 \u4e2d\uff0c\u6807\u51c6\u5e93\u4e2d\u6dfb\u52a0\u4e86\u51e0\u4e2a\u65b9\u6cd5\u6765\u5e2e\u52a9\u6211\u4eec\u66f4\u5feb\u901f\u7684\u5b8c\u6210\u4ee5\u4e0a\u7684\u5de5\u4f5c\u3002\u5f53\u524d\u524d\u63d0\u662f\uff0c\u4f60\u7684\u81ea\u5b9a\u4e49 Error \u6b63\u786e\u7684\u5b9e\u73b0\u4e86 <code>Unwrap()<\/code> \u65b9\u6cd5<\/p>\n<p><code>errors.Is<\/code> \u7528\u6765\u5c06\u4e00\u4e2a\u9519\u8bef\u548c\u4e00\u4e2a\u503c\u8fdb\u884c\u5bf9\u6bd4\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">\/\/ Similar to:\n\/\/   if err == ErrNotFound { \u2026 }\nif errors.Is(err, ErrNotFound) {\n    \/\/ something wasn't found\n}\n<\/code><\/pre>\n<p><code>errors.As<\/code> \u7528\u6765\u5224\u65ad\u4e00\u4e2a\u9519\u8bef\u662f\u5426\u662f\u4e00\u4e2a\u7279\u5b9a\u7684\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">\/\/ Similar to:\n\/\/   if e, ok := err.(*QueryError); ok { \u2026 }\nvar e *QueryError\nif errors.As(err, &amp;e) {\n    \/\/ err is a *QueryError, and e is set to the error's value\n}\n<\/code><\/pre>\n<p>\u5f53\u64cd\u4f5c\u4e00\u4e2a\u5305\u88c5\u4e86\u7684\u9519\u8bef\u65f6\uff0c<code>Is<\/code> \u548c <code>As<\/code> \u4f1a\u8003\u8651\u9519\u8bef\u94fe\u4e0a\u6240\u6709\u7684\u9519\u8bef\u3002\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">type ErrorA struct {\n    Msg string\n}\n\nfunc (e *ErrorA) Error() string {\n    return e.Msg\n}\n\ntype ErrorB struct {\n    Msg string\n    Err *ErrorA\n}\n\nfunc (e *ErrorB) Error() string {\n    return e.Msg + e.Err.Msg\n}\n\nfunc (e *ErrorB) Unwrap() error {\n    return e.Err\n}\n\nfunc main() {\n    a := &amp;ErrorA{\"error a\"}\n\n    b := &amp;ErrorB{\"error b\", a}\n\n    if errors.Is(b, a) {\n        log.Println(\"error b is a\")\n    }\n\n    var tmpa *ErrorA\n    if errors.As(b, &amp;tmpa) {\n        log.Println(\"error b as ErrorA\")\n    }\n}\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"line-numbers\">error b is a\nerror b as ErrorA\n<\/code><\/pre>\n<h4>\u4f7f\u7528 %w \u6765\u5305\u88c5\u9519\u8bef<\/h4>\n<p>Go 1.13 \u4e2d\u589e\u52a0\u4e86 %w\uff0c\u5f53 %w \u51fa\u73b0\u65f6\uff0c\u7531 <code>fmt.Errorf<\/code> \u8fd4\u56de\u7684\u9519\u8bef\uff0c\u5c06\u4f1a\u6709 <code>Unwrap<\/code> \u65b9\u6cd5\uff0c\u8fd4\u56de\u7684\u662f %w \u5bf9\u5e94\u7684\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-go line-numbers\">type ErrorA struct {\n    Msg string\n}\n\nfunc (e *ErrorA) Error() string {\n    return e.Msg\n}\n\nfunc main() {\n    a := &amp;ErrorA{\"error a\"}\n\n    b := fmt.Errorf(\"new error: %w\", a)\n\n    if errors.Is(b, a) {\n        fmt.Println(\"error b is a\")\n    }\n\n    var tmpa *ErrorA\n    if errors.As(b, &amp;tmpa) {\n        fmt.Println(\"error b as ErrorA\")\n    }\n}\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"line-numbers\">error b is a\nerror b as ErrorA\n<\/code><\/pre>\n<h4>\u662f\u5426\u9700\u8981\u5bf9\u9519\u8bef\u8fdb\u884c\u5305\u88c5<\/h4>\n<p>\u5f53\u4f60\u5411\u4e00\u4e2a error \u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u65f6\uff0c\u8981\u4e48\u4f7f\u7528 <code>fmt.Errorf<\/code>\uff0c\u8981\u4e48\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u7c7b\u578b\uff0c\u8fd9\u662f\u4f60\u5c31\u8981\u51b3\u5b9a\u8fd9\u4e2a\u65b0\u7684\u9519\u8bef\u662f\u5426\u5e94\u8be5\u5305\u88c5\u539f\u59cb\u7684\u9519\u8bef\u4fe1\u606f\u3002\u8fd9\u662f\u4e00\u4e2a\u6ca1\u6709\u6807\u51c6\u7b54\u6848\u7684\u95ee\u9898\uff0c\u5b83\u53d6\u51b3\u4e8e\u65b0\u9519\u8bef\u521b\u5efa\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u5305\u88c5\u4e00\u4e2a\u9519\u8bef\u662f\u4e3a\u4e86\u5c06\u5b83\u66b4\u9732\u7ed9\u8c03\u7528\u8005\u3002\u8fd9\u6837\u8c03\u7528\u8005\u5c31\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u539f\u59cb\u9519\u8bef\u4f5c\u51fa\u4e0d\u540c\u7684\u5904\u7406\uff0c\u6bd4\u5982 <code>os.Open(file)<\/code> \u4f1a\u8fd4\u56de\u6587\u4ef6\u4e0d\u5b58\u5728\u8fd9\u79cd\u5177\u4f53\u7684\u9519\u8bef\uff0c \u8fd9\u6837\u8c03\u7528\u8005\u5c31\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6587\u4ef6\u6765\u8ba9\u4ee3\u7801\u53ef\u4ee5\u6b63\u786e\u5f80\u4e0b\u6267\u884c\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u4e0d\u60f3\u66b4\u9732\u5b9e\u73b0\u7ec6\u8282\u65f6\u5c31\u4e0d\u8981\u5305\u88c5\u9519\u8bef\u3002\u56e0\u4e3a\u66b4\u9732\u4e00\u4e2a\u5177\u5907\u7ec6\u8282\u7684\u9519\u8bef\uff0c\u5c31\u610f\u5473\u7684\u8c03\u7528\u8005\u548c\u6211\u4eec\u7684\u4ee3\u7801\u4ea7\u751f\u4e86\u8026\u5408\u3002\u8fd9\u4e5f\u8fdd\u53cd\u4e86\u62bd\u8c61\u7684\u539f\u5219\u3002<\/p>\n<h2>\u4e09\u3001\u53c2\u8003\u8d44\u6599<\/h2>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.golang.org\/errors-are-values\">Errors are values<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.golang.org\/go1.13-errors\">Working with Errors in Go 1.13<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6982\u8ff0 Golang \u7684\u9519\u8bef\u5904\u7406\u4e00\u76f4\u662f\u4e00\u4e2a\u6bd4\u8f83\u8ba8\u8bba\u6bd4\u8f83\u591a\u7684\u8bdd\u3002\u6211\u521a\u63a5\u89e6 Golang \u7684\u65f6\u5019\u4e5f\u770b\u8fc7\u5173\u4e8e\u9519\u8bef\u5904 &hellip; <a href=\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":538,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67],"tags":[],"class_list":["post-564","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae - \u4e00\u53ea\u5b89\u9759\u7684\u732b<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae - \u4e00\u53ea\u5b89\u9759\u7684\u732b\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u6982\u8ff0 Golang \u7684\u9519\u8bef\u5904\u7406\u4e00\u76f4\u662f\u4e00\u4e2a\u6bd4\u8f83\u8ba8\u8bba\u6bd4\u8f83\u591a\u7684\u8bdd\u3002\u6211\u521a\u63a5\u89e6 Golang \u7684\u65f6\u5019\u4e5f\u770b\u8fc7\u5173\u4e8e\u9519\u8bef\u5904 &hellip; \u7ee7\u7eed\u9605\u8bfbGolang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/\" \/>\n<meta property=\"og:site_name\" content=\"\u4e00\u53ea\u5b89\u9759\u7684\u732b\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-03T09:15:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-05T13:31:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png\" \/>\n\t<meta property=\"og:image:width\" content=\"600\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"jiangpengfei\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"jiangpengfei\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/\"},\"author\":{\"name\":\"jiangpengfei\",\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"headline\":\"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae\",\"datePublished\":\"2020-03-03T09:15:00+00:00\",\"dateModified\":\"2023-07-05T13:31:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/\"},\"wordCount\":32,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"image\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png\",\"articleSection\":[\"go\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/\",\"url\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/\",\"name\":\"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae - \u4e00\u53ea\u5b89\u9759\u7684\u732b\",\"isPartOf\":{\"@id\":\"https:\/\/www.myway5.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png\",\"datePublished\":\"2020-03-03T09:15:00+00:00\",\"dateModified\":\"2023-07-05T13:31:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#primaryimage\",\"url\":\"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png\",\"contentUrl\":\"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png\",\"width\":600,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.myway5.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.myway5.com\/#website\",\"url\":\"https:\/\/www.myway5.com\/\",\"name\":\"\u4e00\u53ea\u5b89\u9759\u7684\u732b\",\"description\":\"\u60f3\u5565\u5462\",\"publisher\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.myway5.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\",\"name\":\"jiangpengfei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g\",\"caption\":\"jiangpengfei\"},\"logo\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.myway5.com\/index.php\/author\/joyme\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae - \u4e00\u53ea\u5b89\u9759\u7684\u732b","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/","og_locale":"zh_CN","og_type":"article","og_title":"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae - \u4e00\u53ea\u5b89\u9759\u7684\u732b","og_description":"\u4e00\u3001\u6982\u8ff0 Golang \u7684\u9519\u8bef\u5904\u7406\u4e00\u76f4\u662f\u4e00\u4e2a\u6bd4\u8f83\u8ba8\u8bba\u6bd4\u8f83\u591a\u7684\u8bdd\u3002\u6211\u521a\u63a5\u89e6 Golang \u7684\u65f6\u5019\u4e5f\u770b\u8fc7\u5173\u4e8e\u9519\u8bef\u5904 &hellip; \u7ee7\u7eed\u9605\u8bfbGolang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae","og_url":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/","og_site_name":"\u4e00\u53ea\u5b89\u9759\u7684\u732b","article_published_time":"2020-03-03T09:15:00+00:00","article_modified_time":"2023-07-05T13:31:51+00:00","og_image":[{"width":600,"height":300,"url":"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png","type":"image\/png"}],"author":"jiangpengfei","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"jiangpengfei","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#article","isPartOf":{"@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/"},"author":{"name":"jiangpengfei","@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"headline":"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae","datePublished":"2020-03-03T09:15:00+00:00","dateModified":"2023-07-05T13:31:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/"},"wordCount":32,"commentCount":0,"publisher":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"image":{"@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#primaryimage"},"thumbnailUrl":"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png","articleSection":["go"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/","url":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/","name":"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae - \u4e00\u53ea\u5b89\u9759\u7684\u732b","isPartOf":{"@id":"https:\/\/www.myway5.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#primaryimage"},"image":{"@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#primaryimage"},"thumbnailUrl":"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png","datePublished":"2020-03-03T09:15:00+00:00","dateModified":"2023-07-05T13:31:51+00:00","breadcrumb":{"@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#primaryimage","url":"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png","contentUrl":"https:\/\/www.myway5.com\/wp-content\/uploads\/2020\/01\/golang.png","width":600,"height":300},{"@type":"BreadcrumbList","@id":"https:\/\/www.myway5.com\/index.php\/2020\/03\/03\/golang-error\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.myway5.com\/"},{"@type":"ListItem","position":2,"name":"Golang \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5efa\u8bae"}]},{"@type":"WebSite","@id":"https:\/\/www.myway5.com\/#website","url":"https:\/\/www.myway5.com\/","name":"\u4e00\u53ea\u5b89\u9759\u7684\u732b","description":"\u60f3\u5565\u5462","publisher":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.myway5.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685","name":"jiangpengfei","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.myway5.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g","caption":"jiangpengfei"},"logo":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/image\/"},"url":"https:\/\/www.myway5.com\/index.php\/author\/joyme\/"}]}},"views":5523,"_links":{"self":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/comments?post=564"}],"version-history":[{"count":1,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/564\/revisions"}],"predecessor-version":[{"id":1555,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/564\/revisions\/1555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/media\/538"}],"wp:attachment":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/media?parent=564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/categories?post=564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/tags?post=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}