{"id":979,"date":"2025-01-16T00:31:01","date_gmt":"2025-01-15T16:31:01","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=979"},"modified":"2025-01-16T00:31:01","modified_gmt":"2025-01-15T16:31:01","slug":"o%e5%b9%b6%e5%8f%91%e8%a7%a3%e7%a0%81%e4%b8%8egoroutine%e8%b0%83%e5%ba%a6%ef%bc%9a%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%b9%b6%e8%a1%8c%e5%8c%96%e8%a7%a3%e7%a0%81%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/16\/o%e5%b9%b6%e5%8f%91%e8%a7%a3%e7%a0%81%e4%b8%8egoroutine%e8%b0%83%e5%ba%a6%ef%bc%9a%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%b9%b6%e8%a1%8c%e5%8c%96%e8%a7%a3%e7%a0%81%e4%bb%bb%e5%8a%a1\/","title":{"rendered":"o\u5e76\u53d1\u89e3\u7801\u4e0eGoroutine\u8c03\u5ea6\uff1a\u5982\u4f55\u9ad8\u6548\u5e76\u884c\u5316\u89e3\u7801\u4efb\u52a1"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Go \u4e2d\uff0c<strong>\u5e76\u53d1\u89e3\u7801<\/strong>\u901a\u5e38\u662f\u6307\u5728\u591a\u4e2a goroutine \u4e2d\u5e76\u884c\u6267\u884c\u89e3\u7801\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5904\u7406\u6548\u7387\u3002Go \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5e76\u53d1\u673a\u5236\uff0c\u901a\u8fc7 goroutines \u548c channels \u6765\u5b9e\u73b0\u9ad8\u6548\u7684\u5e76\u884c\u8ba1\u7b97\u3002\u5728\u5904\u7406\u50cf JSON \u89e3\u7801\u3001XML \u89e3\u6790\u7b49\u4efb\u52a1\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5e76\u53d1\u89e3\u7801\u6765\u52a0\u901f\u5904\u7406\u8fc7\u7a0b\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Goroutines \u7b80\u4ecb<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u7684\u5e76\u53d1\u6a21\u578b\u57fa\u4e8e <strong>goroutine<\/strong>\uff0c\u5b83\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u5177\u6709\u8f83\u5c0f\u7684\u5185\u5b58\u5360\u7528\u548c\u66f4\u4f4e\u7684\u542f\u52a8\u5f00\u9500\u3002Goroutines \u662f\u7531 Go \u8fd0\u884c\u65f6\u8c03\u5ea6\u7684\uff0c\u53ef\u4ee5\u6709\u6548\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u521b\u5efa\u4e00\u4e2a goroutine\uff0c\u53ea\u9700\u8981\u5728\u51fd\u6570\u8c03\u7528\u524d\u52a0\u4e0a <code>go<\/code> \u5173\u952e\u5b57\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go func() {\n    \/\/ \u6267\u884c\u5e76\u53d1\u4efb\u52a1\n}()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8fd0\u884c\u65f6\u4f1a\u81ea\u52a8\u8c03\u5ea6\u8fd9\u4e9b goroutines\uff0c\u5229\u7528\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u6c60\u548c\u81ea\u6709\u7684\u8c03\u5ea6\u5668\u6765\u5206\u914d\u8ba1\u7b97\u8d44\u6e90\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Goroutine \u8c03\u5ea6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u901a\u8fc7 <strong>\u8c03\u5ea6\u5668<\/strong>\uff08Go Scheduler\uff09\u7ba1\u7406 goroutines\uff0c\u5b83\u4f1a\u5c06 goroutines \u5206\u914d\u5230\u64cd\u4f5c\u7cfb\u7edf\u7684\u7ebf\u7a0b\u4e0a\u3002Go \u4f7f\u7528\u7684\u8c03\u5ea6\u7b56\u7565\u662f\u57fa\u4e8e <strong>M:N \u8c03\u5ea6<\/strong>\uff0c\u5373\u591a\u4e2a goroutines \u7531\u5c11\u91cf\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u6765\u8c03\u5ea6\u6267\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>Goroutine \u8c03\u5ea6\u6d41\u7a0b<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>P\uff08Processor\uff09<\/strong>\uff1a\u8868\u793a Go \u8fd0\u884c\u65f6\u7684\u5904\u7406\u5668\uff0c\u8c03\u5ea6\u5668\u5728\u5176\u4e0a\u6267\u884c goroutines\u3002<\/li>\n\n\n\n<li><strong>M\uff08Machine\uff09<\/strong>\uff1a\u4ee3\u8868\u64cd\u4f5c\u7cfb\u7edf\u7684\u7ebf\u7a0b\uff0cGo \u8c03\u5ea6\u5668\u5c06 M \u5206\u914d\u7ed9 P \u6267\u884c goroutines\u3002<\/li>\n\n\n\n<li><strong>G\uff08Goroutine\uff09<\/strong>\uff1a\u5373\u6211\u4eec\u901a\u8fc7 <code>go<\/code> \u5173\u952e\u5b57\u542f\u52a8\u7684\u8f7b\u91cf\u7ea7\u6267\u884c\u5355\u5143\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u5ea6\u5668\u4f1a\u5c06 goroutines \u6392\u961f\u5728 P \u4e0a\uff0cP \u4f1a\u5c06 goroutines \u5206\u914d\u7ed9 M \u6765\u6267\u884c\u3002\u591a\u4e2a P \u53ef\u4ee5\u540c\u65f6\u5b58\u5728\uff0c\u4ece\u800c\u5229\u7528\u591a\u6838 CPU \u6765\u63d0\u9ad8\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u8c03\u5ea6\u5668\u7684\u5de5\u4f5c\u65b9\u5f0f<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u534f\u4f5c\u5f0f\u8c03\u5ea6<\/strong>\uff1a\u6bcf\u4e2a goroutine \u5728\u6267\u884c\u65f6\u5fc5\u987b\u4e3b\u52a8\u8ba9\u51fa\u63a7\u5236\u6743\u6216\u53d1\u751f\u963b\u585e\uff0c\u624d\u80fd\u8ba9\u5176\u4ed6 goroutine \u5f97\u5230\u6267\u884c\u7684\u673a\u4f1a\u3002<\/li>\n\n\n\n<li><strong>\u62a2\u5360\u5f0f\u8c03\u5ea6<\/strong>\uff1aGo 1.14 \u5f15\u5165\u4e86\u62a2\u5360\u5f0f\u8c03\u5ea6\uff0c\u8c03\u5ea6\u5668\u53ef\u4ee5\u6839\u636e\u8d85\u65f6\u6216\u5176\u4ed6\u4e8b\u4ef6\u6765\u5f3a\u5236\u6682\u505c\u4e00\u4e2a\u957f\u65f6\u95f4\u8fd0\u884c\u7684 goroutine\uff0c\u5e76\u8ba9\u5176\u4ed6 goroutine \u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Goroutine \u89e3\u7801\u5e76\u53d1\u5316<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u8981\u89e3\u7801\u591a\u4e2a JSON \u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u542f\u52a8\u591a\u4e2a goroutine \u6765\u5e76\u884c\u89e3\u7801\uff0c\u63d0\u5347\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5176\u4e2d\u6211\u4eec\u4f7f\u7528 goroutines \u6765\u5e76\u53d1\u89e3\u7801\u591a\u4e2a JSON \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"log\"\n    \"sync\"\n)\n\n\/\/ \u793a\u4f8b\u7ed3\u6784\u4f53\ntype Person struct {\n    Name string `json:\"name\"`\n    Age  int    `json:\"age\"`\n}\n\nfunc decodeJSON(data &#91;]byte, wg *sync.WaitGroup) {\n    defer wg.Done()\n\n    var person Person\n    err := json.Unmarshal(data, &amp;person)\n    if err != nil {\n        log.Println(\"Error decoding JSON:\", err)\n        return\n    }\n\n    fmt.Printf(\"Decoded person: %+v\\n\", person)\n}\n\nfunc main() {\n    jsonData1 := &#91;]byte(`{\"name\": \"Alice\", \"age\": 25}`)\n    jsonData2 := &#91;]byte(`{\"name\": \"Bob\", \"age\": 30}`)\n    jsonData3 := &#91;]byte(`{\"name\": \"Charlie\", \"age\": 35}`)\n\n    var wg sync.WaitGroup\n\n    \/\/ \u542f\u52a8\u591a\u4e2a goroutines \u8fdb\u884c\u5e76\u53d1\u89e3\u7801\n    wg.Add(3)\n    go decodeJSON(jsonData1, &amp;wg)\n    go decodeJSON(jsonData2, &amp;wg)\n    go decodeJSON(jsonData3, &amp;wg)\n\n    \/\/ \u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\n    wg.Wait()\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>decodeJSON<\/code> \u51fd\u6570\uff0c\u5b83\u5c06\u4f20\u5165\u7684 JSON \u6570\u636e\u89e3\u7801\u4e3a <code>Person<\/code> \u7ed3\u6784\u4f53\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>sync.WaitGroup<\/code> \u6765\u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>go<\/code> \u5173\u952e\u5b57\u542f\u52a8\u591a\u4e2a goroutine \u6765\u5e76\u53d1\u89e3\u7801\u591a\u4e2a JSON \u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5982\u4f55\u4f18\u5316 Goroutine \u8c03\u5ea6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u6211\u4eec\u4f7f\u7528\u591a\u4e2a goroutine \u6765\u5e76\u53d1\u5904\u7406\u4efb\u52a1\u65f6\uff0c\u9700\u8981\u8003\u8651\u5230 <strong>goroutine \u8c03\u5ea6\u548c\u8d44\u6e90\u7ba1\u7406<\/strong>\uff0c\u907f\u514d\u56e0\u8fc7\u591a\u7684 goroutine \u5bfc\u81f4\u8c03\u5ea6\u5668\u9891\u7e41\u5207\u6362\uff0c\u589e\u52a0\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <strong>\u9650\u5236 goroutine \u6570\u91cf<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u5904\u7406\u5927\u91cf\u4efb\u52a1\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 <strong>\u5de5\u4f5c\u6c60\u6a21\u5f0f<\/strong>\uff08worker pool\uff09\u6765\u9650\u5236\u5e76\u53d1\u7684 goroutine \u6570\u91cf\uff0c\u907f\u514d\u521b\u5efa\u8fc7\u591a\u7684 goroutine \u5bfc\u81f4\u7cfb\u7edf\u8d44\u6e90\u8017\u5c3d\u3002\u4f7f\u7528 goroutines \u65f6\uff0c\u5e94\u8be5\u6839\u636e\u4efb\u52a1\u91cf\u548c\u7cfb\u7edf\u8d1f\u8f7d\u6765\u52a8\u6001\u8c03\u6574\u5e76\u53d1\u6570\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const maxWorkers = 5\nsem := make(chan struct{}, maxWorkers) \/\/ \u9650\u5236\u5e76\u53d1\u7684 goroutine \u6570\u91cf\n\n\/\/ \u542f\u52a8\u591a\u4e2a goroutines\nfor i := 0; i &lt; len(jsonData); i++ {\n    sem &lt;- struct{}{} \/\/ \u83b7\u5f97\u4e00\u4e2a\u4fe1\u53f7\n    go func(i int) {\n        defer func() { &lt;-sem }() \/\/ \u89e3\u9501\u4fe1\u53f7\n\n        \/\/ \u89e3\u7801\u903b\u8f91\n    }(i)\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <strong>\u907f\u514d\u8fc7\u591a\u540c\u6b65<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c3d\u91cf\u907f\u514d\u5728\u5927\u91cf goroutines \u4e2d\u9891\u7e41\u4f7f\u7528\u9501\uff08\u5982 <code>sync.Mutex<\/code>\uff09\uff0c\u56e0\u4e3a\u9501\u4f1a\u5f15\u8d77\u6027\u80fd\u74f6\u9888\u3002\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u3001\u4f7f\u7528\u65e0\u9501\u6570\u636e\u7ed3\u6784\u6216\u8005\u901a\u8fc7\u901a\u9053\u6765\u8fdb\u884c\u6570\u636e\u540c\u6b65\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.3 <strong>\u4f7f\u7528 <code>sync.Pool<\/code> \u63d0\u9ad8\u5bf9\u8c61\u590d\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u6d89\u53ca\u5230\u5927\u91cf\u77ed\u671f\u4f7f\u7528\u7684\u5bf9\u8c61\u65f6\uff08\u6bd4\u5982\u89e3\u7801\u8fc7\u7a0b\u4e2d\u521b\u5efa\u7684\u4e34\u65f6\u5bf9\u8c61\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>sync.Pool<\/code> \u6765\u51cf\u5c11\u5185\u5b58\u5206\u914d\u548c\u5783\u573e\u56de\u6536\u7684\u8d1f\u62c5\uff0c\u63d0\u5347\u6027\u80fd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var pool = sync.Pool{\n    New: func() interface{} {\n        return new(Person) \/\/ \u9884\u5206\u914d Person \u5bf9\u8c61\n    },\n}\n\n\/\/ \u83b7\u53d6\u5bf9\u8c61\nperson := pool.Get().(*Person)\n\/\/ \u4f7f\u7528\u5bf9\u8c61\npool.Put(person) \/\/ \u4f7f\u7528\u540e\u653e\u56de\u6c60\u4e2d<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go \u7684\u5e76\u53d1\u6a21\u578b\u4f7f\u5f97\u5e76\u884c\u89e3\u7801\u53d8\u5f97\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7 goroutine \u5e76\u884c\u5904\u7406\u591a\u4e2a\u89e3\u7801\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u8c03\u5ea6\u5668\u8d1f\u8d23\u5c06 goroutine \u5206\u914d\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\uff0c\u901a\u8fc7 M:N \u8c03\u5ea6\u6a21\u578b\u9ad8\u6548\u5730\u5229\u7528 CPU\u3002<\/li>\n\n\n\n<li>\u4e3a\u4e86\u907f\u514d\u8fc7\u591a\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u548c\u8d44\u6e90\u6d6a\u8d39\uff0c\u5efa\u8bae\u901a\u8fc7\u9650\u5236\u5e76\u53d1 goroutine \u6570\u91cf\u3001\u51cf\u5c11\u9501\u7684\u4f7f\u7528\u3001\u4ee5\u53ca\u5408\u7406\u4f7f\u7528\u5185\u5b58\u6c60\u6765\u4f18\u5316\u6027\u80fd\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u4e2d\uff0c\u5e76\u53d1\u89e3\u7801\u901a\u5e38\u662f\u6307\u5728\u591a\u4e2a goroutine \u4e2d\u5e76\u884c\u6267\u884c\u89e3\u7801\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5904\u7406\u6548\u7387\u3002Go \u63d0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-979","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/979","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=979"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/979\/revisions"}],"predecessor-version":[{"id":980,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/979\/revisions\/980"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}