{"id":205900,"date":"2025-06-12T09:51:54","date_gmt":"2025-06-12T01:51:54","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205900\/"},"modified":"2025-06-12T09:51:54","modified_gmt":"2025-06-12T01:51:54","slug":"%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86-golang-%e5%b9%b6%e5%8f%91%e5%87%bd%e6%95%b0%e4%b8%ad%e7%9a%84%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205900\/","title":{"rendered":"\u5982\u4f55\u5904\u7406 Golang \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff1f"},"content":{"rendered":"<p>\u5bf9\u4e8e\u4e00\u4e2aGOLANG\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cGOLANG\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5982\u4f55\u5904\u7406 GOLANG \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01,\u5728 Go \u5e76\u53d1\u51fd\u6570\u4e2d\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 sync.ErrorGroup\uff1a\u5b83\u6536\u96c6\u7b2c\u4e00\u4e2a\u53d1\u751f\u7684\u9519\u8bef\u5e76\u8fd4\u56de\u3002\u4f7f\u7528 Channels\uff1a\u5728 goroutine \u4e4b\u95f4\u901a\u4fe1\u9519\u8bef\uff0c\u5141\u8bb8\u53d1\u9001\u548c\u63a5\u6536\u9519\u8bef\u6d88\u606f\u3002\u4f7f\u7528 recover\uff1a\u6355\u6349\u5e76\u53d1\u51fd\u6570\u4e2d\u7684 panic \u5e76\u8fd4\u56de\u5f15\u53d1 panic \u7684\u503c\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730033333671e36b533386.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u5904\u7406 Go \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef<\/strong>,\u5728\u5e76\u53d1\u7a0b\u5e8f\u4e2d\uff0c\u5904\u7406\u9519\u8bef\u81f3\u5173\u91cd\u8981\u3002Go \u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5236\u6765\u5904\u7406\u5e76\u53d1\u51fd\u6570\u4e2d\u53d1\u751f\u7684\u9519\u8bef\u3002,<br \/>\n<strong>\u4f7f\u7528 ErrorGroup<\/strong>,<br \/>\n<code>sync.ErrorGroup<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u591a\u4e2a\u5e76\u53d1\u64cd\u4f5c\u5e76\u6536\u96c6\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u4ea7\u751f\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u7684\u7ed3\u6784\u3002\u51fd\u6570<br \/>\n<code>Add(f func()) error<\/code> \u5411\u7ec4\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u64cd\u4f5c\u3002\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u9519\u8bef\u4f1a\u88ab\u5b58\u50a8\u5728\u7ec4\u4e2d\u3002\u8c03\u7528<br \/>\n<code>Wait()<\/code> \u65f6\uff0c\u5b83\u5c06\u8fd4\u56de\u9047\u5230\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\uff08\u5982\u679c\u6709\uff09\u3002,<br \/>\n<strong>\u4f7f\u7528 Channels<\/strong>,Channels \u53ef\u7528\u4e8e\u5728\u5e76\u53d1\u51fd\u6570\u4e4b\u95f4\u901a\u4fe1\uff0c\u5305\u62ec\u9519\u8bef\u3002\u4f7f\u7528 channel\uff0c\u60a8\u53ef\u4ee5\u5c06\u9519\u8bef\u6d88\u606f\u53d1\u9001\u7ed9\u4e00\u4e2a\u6307\u5b9a channel\uff0c\u7136\u540e\u4ece\u53e6\u4e00\u4e2a goroutine \u4e2d\u63a5\u6536\u5b83\u4eec\u3002,<br \/>\n<strong>\u4f7f\u7528 recover<\/strong>,\u4f7f\u7528<br \/>\n<code>recover<\/code> \u8bed\u53e5\u53ef\u4ee5 catch \u53d1\u751f\u5728\u5e76\u53d1\u51fd\u6570\u4e2d\u7684 panic\u3002<br \/>\n<code>recover()<\/code> \u8fd4\u56de\u5f15\u53d1 panic \u7684\u503c\u3002,\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u5904\u7406 GOLANG \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002,\u5bf9\u4e8e\u4e00\u4e2aGOLANG\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cGOLANG\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5982\u4f55\u5904\u7406 GOLANG \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u5904\u7406 GOLANG \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u5904\u7406 GOLANG \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff1f<\/h1>\n<p><span>2024-10-27 20:49:08<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2aGOLANG\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cGOLANG\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5982\u4f55\u5904\u7406 GOLANG \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p>\u5728 Go \u5e76\u53d1\u51fd\u6570\u4e2d\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 sync.ErrorGroup\uff1a\u5b83\u6536\u96c6\u7b2c\u4e00\u4e2a\u53d1\u751f\u7684\u9519\u8bef\u5e76\u8fd4\u56de\u3002\u4f7f\u7528 Channels\uff1a\u5728 goroutine \u4e4b\u95f4\u901a\u4fe1\u9519\u8bef\uff0c\u5141\u8bb8\u53d1\u9001\u548c\u63a5\u6536\u9519\u8bef\u6d88\u606f\u3002\u4f7f\u7528 recover\uff1a\u6355\u6349\u5e76\u53d1\u51fd\u6570\u4e2d\u7684 panic \u5e76\u8fd4\u56de\u5f15\u53d1 panic \u7684\u503c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730033333671e36b533386.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5904\u7406 Go \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef<\/strong><\/p>\n<p>\u5728\u5e76\u53d1\u7a0b\u5e8f\u4e2d\uff0c\u5904\u7406\u9519\u8bef\u81f3\u5173\u91cd\u8981\u3002Go \u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5236\u6765\u5904\u7406\u5e76\u53d1\u51fd\u6570\u4e2d\u53d1\u751f\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528 ErrorGroup<\/strong><\/p>\n<p><code>sync.ErrorGroup<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u591a\u4e2a\u5e76\u53d1\u64cd\u4f5c\u5e76\u6536\u96c6\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u4ea7\u751f\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u7684\u7ed3\u6784\u3002\u51fd\u6570 <code>Add(f func()) error<\/code> \u5411\u7ec4\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u64cd\u4f5c\u3002\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u9519\u8bef\u4f1a\u88ab\u5b58\u50a8\u5728\u7ec4\u4e2d\u3002\u8c03\u7528 <code>Wait()<\/code> \u65f6\uff0c\u5b83\u5c06\u8fd4\u56de\u9047\u5230\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\uff08\u5982\u679c\u6709\uff09\u3002<\/p>\n<pre>import (\n    \"context\"\n    \"fmt\"\n    \"sync\"\n    \"sync\/atomic\"\n)\n\nfunc main() {\n    var eg sync.ErrGroup\n    var count int64\n    for i := 0; i &lt; 10; i++ {\n        eg.Add(func() error {\n            atomic.AddInt64(&amp;count, 1)\n            fmt.Printf(\"Count: %d\\n\", count)\n            if count%2 == 0 {\n                return fmt.Errorf(\"even count: %d\", count)\n            }\n            return nil\n        })\n    }\n\n    if err := eg.Wait(); err != nil {\n        fmt.Println(err)\n    }\n}<\/pre>\n<p><strong>\u4f7f\u7528 Channels<\/strong><\/p>\n<p>Channels \u53ef\u7528\u4e8e\u5728\u5e76\u53d1\u51fd\u6570\u4e4b\u95f4\u901a\u4fe1\uff0c\u5305\u62ec\u9519\u8bef\u3002\u4f7f\u7528 channel\uff0c\u60a8\u53ef\u4ee5\u5c06\u9519\u8bef\u6d88\u606f\u53d1\u9001\u7ed9\u4e00\u4e2a\u6307\u5b9a channel\uff0c\u7136\u540e\u4ece\u53e6\u4e00\u4e2a goroutine \u4e2d\u63a5\u6536\u5b83\u4eec\u3002<\/p>\n<pre>import (\n    \"context\"\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    ctx := context.Background()\n    errCh := make(chan error)\n    wg := sync.WaitGroup{}\n\n    for i := 0; i &lt; 10; i++ {\n        wg.Add(1)\n        go func(i int) {\n            defer wg.Done()\n            if i%2 == 0 {\n                errCh &lt;- fmt.Errorf(\"even number: %d\", i)\n                return\n            }\n            errCh &lt;- nil\n        }(i)\n    }\n\n    go func() {\n        wg.Wait()\n        close(errCh)\n    }()\n\n    for err := range errCh {\n        if err != nil {\n            fmt.Println(err)\n        }\n    }\n}<\/pre>\n<p><strong>\u4f7f\u7528 recover<\/strong><\/p>\n<p>\u4f7f\u7528 <code>recover<\/code> \u8bed\u53e5\u53ef\u4ee5 catch \u53d1\u751f\u5728\u5e76\u53d1\u51fd\u6570\u4e2d\u7684 panic\u3002<code>recover()<\/code> \u8fd4\u56de\u5f15\u53d1 panic \u7684\u503c\u3002<\/p>\n<pre>import (\n    \"context\"\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    ctx := context.Background()\n    errCh := make(chan error)\n    wg := sync.WaitGroup{}\n\n    for i := 0; i &lt; 10; i++ {\n        wg.Add(1)\n        go func(i int) {\n            defer wg.Done()\n            if i%2 == 0 {\n                panic(fmt.Errorf(\"even number: %d\", i))\n            }\n            errCh &lt;- nil\n        }(i)\n    }\n\n    go func() {\n        wg.Wait()\n        close(errCh)\n    }()\n\n    for err := range errCh {\n        if err != nil {\n            fmt.Println(err)\n        }\n    }\n\n    \/\/ \u5728\u4e3b goroutine \u4e2d recover \u4efb\u4f55\u672a\u5904\u7406\u7684 panic\n    defer func() {\n        if r := recover(); r != nil {\n            fmt.Println(\"Recovered:\", r)\n        }\n    }()\n}<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u5904\u7406 GOLANG \u5e76\u53d1\u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Go \u4e2d\u7684\u53ef\u53d8\u53c2\u6570\u5217\u8868\u662f\u5426\u53ef\u4ee5\u5728\u5e76\u884c\u4efb\u52a1\u4e2d\u5b89\u5168\u4f7f\u7528\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   jssip\u89c6\u9891\u901a\u8bdd\u5bf9\u65b9\u89c6\u9891\u5ef6\u8fdf30\u79d2\u5982\u4f55\u4f18\u5316\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8e\u4e00\u4e2aGOLANG\u5f00\u53d1\u8005\u6765\u8bf4\uff0c&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205900","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205900"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205900\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}