{"id":10006722,"date":"2022-10-24T16:00:39","date_gmt":"2022-10-24T08:00:39","guid":{"rendered":"https:\/\/ityet.com\/?p=10006722"},"modified":"2022-10-24T16:00:39","modified_gmt":"2022-10-24T08:00:39","slug":"go-try-%e6%96%b0%e6%8f%90%e6%a1%88%e9%9d%a0%e8%b0%b1%e5%90%97%ef%bc%9f%e6%83%b3%e7%ae%80%e5%8c%96%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%ba%86","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006722","title":{"rendered":"Go try \u65b0\u63d0\u6848\u9760\u8c31\u5417\uff1f\u60f3\u7b80\u5316\u9519\u8bef\u5904\u7406\u4e86"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002<\/p>\n<p>\u5728\u8fd1\u65e5\u65b0\u7684 try \u63d0\u6848\u300a<a href=\"https:\/\/link.segmentfault.com\/?enc=qQN1LXbcJKjI8hGbD1OQNg%3D%3D.VCa4YRDlmqFKvEOfzfgOSTusOwkNJdG2hRvViYcpTAaKZeKgPQs8Mi1DphoqCtjC\" rel=\"nofollow\" title=\"proposal: Go 2: error handling: try statement with handler\">proposal: Go 2: error handling: try statement with handler<\/a>\u300b\u5728\u793e\u533a\u5f15\u53d1\u4e86\u70ed\u8bae\u3002<\/p>\n<p>\u4eca\u5929\u714e\u9c7c\u548c\u5927\u5bb6\u4e00\u8d77\u6253\u5f00\u6765\u770b\u770b\uff0c\u8fd9\u80fd\u628a Go \u9519\u8bef\u5904\u7406\u673a\u5236\u7ed9\u6380\u5f00\u91cd\u6574\u4e0d\u3002<\/p>\n<h2>\u80cc\u666f<\/h2>\n<p>\u6765\u81ea PingCAP \u7684\u63d0\u6848\u4f5c\u8005 @Greg Weber \u4f1a\u5e72\u8fd9\u4e8b\u57fa\u4e8e\u4e24\u4e2a\u56e0\u7d20\uff0c\u4e00\u4e2a\u662f\u5728\u300a<a href=\"https:\/\/link.segmentfault.com\/?enc=e8rMD4x9Mp3HCiyLDor53w%3D%3D.2E1ERmZMcjUkNJHppsd0U7vrWfTfUEppW6vIYkdl5WWg4Qe%2FgWbkNN0undPi2df%2B\" rel=\"nofollow\" title=\"Go Developer Survey 2022 Q2 Results\">Go Developer Survey 2022 Q2 Results<\/a>\u300b\u4e2d\u660e\u786e\u63d0\u5230\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042676461\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u968f\u7740 Go1.18 \u6cdb\u578b\u7684\u53d1\u5e03\uff0c\u539f\u5148\u77db\u76fe\u6700\u6df1\u7684\u6cdb\u578b\u5df2\u7ecf\u5f97\u5230\u4e00\u4e2a\u521d\u6b65\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5728\u793e\u533a\u8c03\u7814\u4e0a\uff0c\u5f00\u53d1\u8005\u5728\u4f7f\u7528 Go \u65f6\u9762\u4e34\u7684\u6700\u5927\u6311\u6218\u5df2\u7ecf\u8f6c\u79fb\u5230\u4e86\u9519\u8bef\u5904\u7406\u4e0a\uff0c\u9700\u8981\u6295\u5165\u7cbe\u529b\u53bb \u201c\u89e3\u51b3\u201d \u5b83\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u56e0\u7d20\u5c31\u662f\u4f17\u6240\u7686\u77e5\u7684\uff0cGo \u9519\u8bef\u5904\u7406\u4ee3\u7801\u6bd4\u8f83\u7e41\u7410\uff0c\u5e38\u88ab\u5de5\u7a0b\u5e08\u4eec\u620f\u79f0\u4e00\u4e2a Go \u5de5\u7a0b\u91cc\u6709 30% \u90fd if err = nil\u3002<\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">_, err := f()\nif err != nil {\n    ...\n}\n_, err = r()\nif err != nil {\n    ...\n}\n_, err = w()\nif err != nil {\n    ...\n}<\/code><\/pre>\n<p>\u5e0c\u671b\u8ba9\u5176\u66f4\u4f18\u96c5\u3002\u4e5f\u6709\u8bb8\u591a\u5c0f\u4f19\u4f34\u8ba4\u540c\u8fd9\u4e2a\u8bbe\u8ba1\uff0c\u786e\u5b9e\u662f\u7b80\u5355\u3001\u76f4\u89c2\u7684\u5904\u7406\uff0c\u5728\u793e\u533a\u5f62\u6210\u4e86\u89d2\u529b\u3002<\/p>\n<h2>try-handler \u63d0\u6848<\/h2>\n<p>\u672c\u6b21\u63d0\u6848\u4e2d\u6240\u63d0\u5230\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u662f\u589e\u52a0\u4e00\u4e2a\u65b0\u8bed\u53e5 try \uff0c\u4ee5\u6b64\u8fbe\u5230\u7b80\u6d01\u7684\u9519\u8bef\u5904\u7406\u7684\u4f5c\u7528\uff0c\u8ba9 if err != nil \u7684\u5904\u7406\u4e1d\u6ed1\u8d77\u6765\u3002<\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">try err, handler<\/code><\/pre>\n<p>\u7f16\u8bd1\u5668\u7ffb\u8bd1\u540e\u751f\u6210\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">if err != nil {\n    return handler(err)\n}<\/code><\/pre>\n<p>\u5728\u51fd\u6570\u4e2d\u53ef\u4ee5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">func(args...) (rtype1, rtypes..., rtypeN, error) {\n    try err, handler\n    ...\n}<\/code><\/pre>\n<p>\u7ffb\u8bd1\u540e\u751f\u6210\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">func(args...) (rtype1, rtypes..., rtypeN, error) {\n    if err != nil {\n            return Zero(rtype1), Zeros(rtypes...)..., Zero(rtypeN), handler(err)\n    }\n    ...\n}<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u53ea\u9488\u5bf9 if err != nil \u5904\u7406\u3002\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">try err<\/code><\/pre>\n<p>\u7ffb\u8bd1\u540e\u751f\u6210\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">if err != nil {\n    return err\n}<\/code><\/pre>\n<p>\u4e0d\u4f1a\u8c03\u7528\u4e0d\u5b58\u5728\u7684 handler \u8fdb\u884c\u5904\u7406\uff0c\u5c06\u4f1a\u76f4\u63a5\u8fd4\u56de\u3002\u4e09\u884c\uff08if err != nil \u7684\u903b\u8f91\uff09\u76f4\u63a5\u53d8 3 \u4e2a\u5355\u8bcd\uff08try\uff09\u3002<\/p>\n<p>\u4e0d\u60f3\u5199\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\uff1a<\/p>\n<pre><code class=\"go\">x, err := f()\ntry err, fmt.Errorf(&quot;f fail: %w&quot;, err)<\/code><\/pre>\n<p>\u9488\u5bf9 defer+try \u7684\u573a\u666f\u53ef\u4ee5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">func CopyFile(src, dst string) error {\n    defer try func(err error) error {\n        return fmt.Errorf(&quot;copy %s %s: %w&quot;, src, dst, err)\n    }\n    ...\n}<\/code><\/pre>\n<p>\u5165\u53c2\u662f\u6bd4\u8f83\u7075\u6d3b\u7684\uff0c\u4f5c\u8005\u5e0c\u671b\u5b83\u662f\u6cdb\u578b\uff0c\u8fd9\u6837\u80fd\u591f\u9002\u914d\u5404\u79cd\u573a\u666f\u7684\u8981\u6c42\u3002<\/p>\n<h2>\u793a\u4f8b\u548c\u5b9e\u8df5<\/h2>\n<p>\u9488\u5bf9\u672c\u63d0\u6848\uff0c\u539f\u4f5c\u8005\u7ed9\u51fa\u4e86\u5404\u7c7b\u4f7f\u7528\u573a\u666f\u7684\u793a\u4f8b\u3002\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">import (\n    &quot;fmt&quot;\n)\n\n\/\/ This helper should be defined in the fmt package\nfunc Handlew(format string, args ...any) func(error) error {\n    return func(err error) error {\n        args = append(args, err)\n        return fmt.Errorf(format+&quot;: %w&quot;, args...)\n    }\n}\n\n\/\/ This helper should be defined in the fmt package\nfunc Handlef(format string, args ...any) func(error) error {\n    return func(err error) error {\n        args = append(args, err)\n        return fmt.Errorf(format+&quot;: %v&quot;, args...)\n    }\n}\n\nfunc valAndError() (int, error) {\n    return 1, fmt.Errorf(&quot;make error&quot;)\n}\n\nfunc newGo() (int, error) {\n    x, err := valAndError()\n    try err\n\n    \/\/ Common formatting functions will already be provided\n    i := 2\n    x, err = valAndError()\n    try err, Handlew(&quot;custom Error %d&quot;, i)\n\n    \/\/ Using a custom error type\n    \/\/ For convenience the error type can expose a method to set the error\n    x, err = valAndError()\n    try err, TheErrorAsHandler(i)\n}\n\ntype TheError struct{\n    num int\n    err error\n}\n\nfunc (t TheError) Error() String {\n    return fmt.Sprintf(&quot;theError %d %v&quot;, t.num, t.err)\n}\n\nfunc TheErrorAsHandler(num int) func(err) TheError {\n    return func(err error) TheError {\n        return theError{ num: i, err: err }\n    }\n}<\/code><\/pre>\n<p>\u53e6\u5916\u5728\u65e5\u5e38\u7684 Go \u5de5\u7a0b\u4e2d\uff0c\u63d0\u6848\u4f5c\u8005\u8ba4\u4e3a CopyFile \u51fd\u6570\u662f\u65b0\u63d0\u6848\u8bed\u53e5\u7684\u4e00\u79cd\u5f88\u597d\u7684\u5b9e\u8df5\u3002\u4e3a\u6b64\u57fa\u4e8e try-handler \u8fdb\u884c\u4e86\u4e00\u7248\u6539\u9020\u548c\u8bf4\u660e\u3002<\/p>\n<p>\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">\/\/ This helper can be used with defer\nfunc handle(err *error, handler func(err error) error) {\n    if err == nil {\n        return nil\n    }\n    *err = handler(err)\n}\n\nfunc CopyFile(src, dst string) (err error) {\n    defer handle(&amp;err, func(err error) error {\n        return fmt.Errorf(&quot;copy %s %s: %w&quot;, src, dst, err)\n    })\n\n    r, err := os.Open(src)\n    try err\n    defer r.Close()\n\n    w, err := os.Create(dst)\n    try err, func(err error) error {\n            os.Remove(dst) \/\/ only if Create fails\n            return fmt.Errorf(&quot;dir %s: %w&quot;, dst, err)\n    }\n    defer w.Close()\n\n    err = io.Copy(w, r)\n    try err\n    err = w.Close()\n    try err\n    return nil\n}<\/code><\/pre>\n<p>\u5f15\u5165 try-hanlder \u540e\uff0c\u80fd\u591f\u505a\u5230\uff1a<\/p>\n<ul>\n<li>\u63d2\u5165\u9519\u8bef\u7684\u8fd4\u56de\u8bed\u53e5\uff0c\u8fdb\u884c\u673a\u5236\u9884\u8bbe\u3002<\/li>\n<li>\u5728\u8fd4\u56de\u9519\u8bef\u4e4b\u524d\u5c06\u9519\u8bef\u5904\u7406\u51fd\u6570\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u5904\u7406\u3002<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5728\u8fd9\u4e2a\u65b0\u63d0\u6848\u4e2d\uff0c\u4e00\u65e6\u5b9e\u65bd\uff0c\u5c31\u53ef\u4ee5\u51cf\u5c11\u5982\u4e0b\u4ee3\u7801\u7684\u7f16\u5199\uff1a<\/p>\n<pre><code>if err != nil {\n  return ...\n}<\/code><\/pre>\n<p>\u5728\u4ee3\u7801\u7f16\u5199\u4e0a\u4f1a\u8282\u7701\u4e00\u4e9b\u884c\u6570\uff0c\u4e14\u53ef\u4ee5\u4e3a\u9519\u8bef\u5904\u7406\u673a\u5236\u5f15\u5165\u4e00\u4e9b\u65b0\u7684 \u201d\u64cd\u4f5c\u201c\uff0c\u8fd9\u662f\u8be5\u63d0\u6848\u7684\u4f18\u52bf\u3002<\/p>\n<p>\u4f46\u662f\u4ece Go \u5f00\u53d1\u8005\u7684\u89d2\u5ea6\u800c\u8a00\uff0c\u4f1a\u5f15\u5165\u4e00\u4e9b\u65b0\u7684\u526f\u4f5c\u7528\uff0c\u4f8b\u5982\uff1a\u521d\u5b66\u8005\u7684\u5b66\u4e60\u6210\u672c\u3001Go \u5de5\u5177\u94fe\u7684\u6539\u9020\u3001\u7a0b\u5e8f\u7406\u89e3\u7684\u590d\u6742\u5ea6\u589e\u52a0\u3002<\/p>\n<p>\u53e6\u5916\u65b0\u7684\u8bed\u53e5\uff0c\u4f3c\u4e4e\u6bd4\u8f83\u96be\u4e0e Go1.13 \u5f15\u5165\u7684 error.Is \u548c As \u6709\u8f83\u597d\u7684\u76f8\u5173\u8054\u6027\u3002\u5982\u679c\u662f\u505a\u4e00\u4e2a\u7b2c\u4e09\u65b9\u7528\u6237\u5e93\u5f15\u5165\u5012\u53ef\u4ee5\uff0c\u4f46\u82e5\u662f\u4f5c\u4e3a\u6807\u51c6\u8fdb\u5165 Go \u6e90\u4ee3\u7801\u4e2d\uff0c\u4f3c\u4e4e\u53c8\u6709\u4e9b\u683c\u683c\u4e0d\u5165\uff08\u63d0\u6848\u4f5c\u8005\u5e0c\u671b\u8fdb\u5165\uff09\u3002<\/p>\n<p>\u770b\u4e86\u90a3\u4e48\u591a\u63d0\u6848\uff0cGo \u9519\u8bef\u5904\u7406\u673a\u5236\u7684 \u201d\u5347\u7ea7\u201c\uff0c\u4f3c\u4e4e\u9677\u5165\u4e86\u624b\u5fc3\u624b\u80cc\u90fd\u662f\u8089\u7684\u9636\u6bb5&#8230;<\/p>\n<blockquote><p>\u6587\u7ae0\u6301\u7eed\u66f4\u65b0\uff0c\u53ef\u4ee5\u5fae\u4fe1\u641c\u3010\u8111\u5b50\u8fdb\u714e\u9c7c\u4e86\u3011\u9605\u8bfb\uff0c\u672c\u6587 <strong>GitHub<\/strong> <a href=\"https:\/\/link.segmentfault.com\/?enc=bXYK28PbnOyVuX6x0fgEdQ%3D%3D.FK4%2Bgr1bdIn2LvC1gwU7lar2pGtdLmmEaF5A1XwE1tQ%3D\" rel=\"nofollow\">github.com\/eddycjy\/blog<\/a> \u5df2\u6536\u5f55\uff0c\u5b66\u4e60 Go \u8bed\u8a00\u53ef\u4ee5\u770b <a href=\"https:\/\/link.segmentfault.com\/?enc=etlYqpojxH0%2BeXI8uO%2FR%2BQ%3D%3D.fLVxI0CBUiTEBNqjSHbsUVAjjlb3tOpEh1pIrcrjm2JPDQz9XcZxuUmRhzT4XvDz\" rel=\"nofollow\">Go \u5b66\u4e60\u5730\u56fe\u548c\u8def\u7ebf<\/a>\uff0c\u6b22\u8fce Star \u50ac\u66f4\u3002<\/p><\/blockquote>\n<h2>\u63a8\u8350\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=8%2BI%2FxTBpTj8oUtWWdKK%2Fgw%3D%3D.lcy%2F3X8zO0aNcfvjkA9C1vFli5jiQh7fR6tStewrNi31fo%2B0M9P9Yf5dNLGUyzfnjknC0bQSvowjQL3rwsE%2BnA%3D%3D\" rel=\"nofollow\">Go \u53ea\u4f1a if err != nil\uff1f\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u5206\u4eab\u8fd9\u4e9b\u4f18\u96c5\u7684\u5904\u7406\u59ff\u52bf\u7ed9\u4f60\uff01<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=IXJShKqgQRSRgBxejEiaCg%3D%3D.zZjGHm0sqSJQnhR3oieYEIP4ITFw2AgYJCmN02ISwLJTneK6iU9b5mE%2Bfm4zxuFxobFnw0REGhKcZON%2Bu5M5hQ%3D%3D\" rel=\"nofollow\">Go \u9519\u8bef\u5904\u7406\u65b0\u601d\u8def\uff1f\u7528\u5de6\u4fa7\u51fd\u6570\u548c\u8868\u8fbe\u5f0f<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=j8X8iLnaccKeMm3FJUEPQw%3D%3D.lrTceGmTlUVXIMvyFZvVNVx9b5xMSWT2haQIMY1TankQx2azQzcjL9YU0BFvzWVGiDMjeYOgHsvv7NDnel%2FPsw%3D%3D\" rel=\"nofollow\">\u5148\u7779\u4e3a\u5feb\uff0cGo2 Error \u7684\u6323\u624e\u4e4b\u8def<\/a><\/li>\n<\/ul>\n<h3>Go \u56fe\u4e66\u7cfb\u5217<\/h3>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=aYqPMsTIh1%2BjFjRtWSRHxg%3D%3D.mXPM0Q6cpqoc8DdvXydFtJVAhrAcv%2Fb%2BPGkm0e2kKON6kNiNdU9OH5SNjiK75Ccw\" rel=\"nofollow\">Go \u8bed\u8a00\u5165\u95e8\u7cfb\u5217\uff1a\u521d\u63a2 Go \u9879\u76ee\u5b9e\u6218<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=2ofvvofOgb%2FRuDeKaIuIiQ%3D%3D.0hTm%2FI5cfir%2FIxWeWH4pNXL%2F0JVcVuYmFPD7GUOTNu0%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u7f16\u7a0b\u4e4b\u65c5\uff1a\u6df1\u5165\u7528 Go \u505a\u9879\u76ee<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=nEVuqwoj%2BCuXUdjGn2B24w%3D%3D.xWRRjk2ZxCY24W0pMefhRO%2FQb3NhJMmuapay%2BTpCbtc%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8bbe\u8ba1\u54f2\u5b66\uff1a\u4e86\u89e3 Go \u7684\u4e3a\u4ec0\u4e48\u548c\u8bbe\u8ba1\u601d\u8003<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=Y3yw2AWObAQh8zA8l06MnA%3D%3D.KTzoiCCUJRnTp0Wguywuj%2BcQSD%2B9R4FK8X49%2FtmO5ok%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8fdb\u9636\u4e4b\u65c5\uff1a\u8fdb\u4e00\u6b65\u6df1\u5165 Go \u6e90\u7801<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042676459\">https:\/\/segmentfault.com\/a\/1190000042676459<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002 \u5728\u8fd1\u65e5\u65b0\u7684 try \u63d0\u6848\u300aproposal: Go 2: error handling:  [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006722","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006722"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006722\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}