{"id":3216,"date":"2025-03-26T00:00:02","date_gmt":"2025-03-25T16:00:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3216"},"modified":"2025-03-26T00:00:02","modified_gmt":"2025-03-25T16:00:02","slug":"go-%e5%b9%b6%e5%8f%91","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/26\/go-%e5%b9%b6%e5%8f%91\/","title":{"rendered":"Go \u5e76\u53d1"},"content":{"rendered":"\n<p>Go \u8bed\u8a00\u7684\u5e76\u53d1\u5904\u7406\u662f\u5176\u4e00\u5927\u4eae\u70b9\u3002Go \u63d0\u4f9b\u4e86\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\u79f0\u4e3a goroutine\uff0c\u4f7f\u5f97\u5e76\u53d1\u7f16\u7a0b\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\u3002Go \u7684\u5e76\u53d1\u6a21\u578b\u57fa\u4e8e CSP\uff08\u901a\u4fe1\u987a\u5e8f\u8fdb\u7a0b\uff09\u6a21\u578b\uff0c\u901a\u8fc7 goroutine \u548c channel \u6765\u8fdb\u884c\u534f\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Goroutine<\/strong><\/h2>\n\n\n\n<p>Goroutine \u662f Go \u4e2d\u7684\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u3002\u5b83\u6bd4\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u66f4\u52a0\u9ad8\u6548\uff0c\u6bcf\u4e2a goroutine \u7684\u5185\u5b58\u5f00\u9500\u975e\u5e38\u5c0f\uff0c\u901a\u5e38\u53ea\u6709\u51e0 KB\uff0c\u5e76\u4e14 Go \u7684\u8c03\u5ea6\u5668\u4f1a\u81ea\u52a8\u7ba1\u7406\u5b83\u4eec\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u542f\u52a8 Goroutine<\/strong><\/h3>\n\n\n\n<p>\u8981\u542f\u52a8\u4e00\u4e2a goroutine\uff0c\u53ea\u9700\u4f7f\u7528 <code>go<\/code> \u5173\u952e\u5b57\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc sayHello() {\n    fmt.Println(\"Hello from Goroutine!\")\n}\n\nfunc main() {\n    go sayHello() \/\/ \u542f\u52a8\u4e00\u4e2a goroutine\n    fmt.Println(\"Hello from Main!\")\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>sayHello()<\/code> \u51fd\u6570\u4f1a\u4f5c\u4e3a\u4e00\u4e2a goroutine \u88ab\u5f02\u6b65\u6267\u884c\uff0c\u800c <code>main()<\/code> \u51fd\u6570\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\u7a0b\u5e8f\u7684\u8f93\u51fa\u987a\u5e8f\u4e0d\u786e\u5b9a\uff0c\u53ef\u80fd\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello from Main!\nHello from Goroutine!<\/code><\/pre>\n\n\n\n<p>\u4e5f\u53ef\u80fd\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello from Goroutine!\nHello from Main!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u5e76\u53d1\u6267\u884c\u591a\u4e2a Goroutine<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u542f\u52a8\u591a\u4e2a goroutine \u6765\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc task(id int) {\n    fmt.Printf(\"Task %d started\\n\", id)\n}\n\nfunc main() {\n    for i := 1; i &lt;= 5; i++ {\n        go task(i) \/\/ \u542f\u52a8 5 \u4e2a goroutine\n    }\n    fmt.Println(\"All tasks started\")\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u542f\u52a8\u4e86 5 \u4e2a goroutine \u6267\u884c <code>task<\/code> \u51fd\u6570\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Channel<\/strong><\/h2>\n\n\n\n<p>Channel \u662f Go \u4e2d\u7528\u4e8e\u5728 goroutine \u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u7684\u7ba1\u9053\u3002\u5b83\u662f\u4e00\u79cd\u5f3a\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f20\u9012\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u63a7\u5236 goroutine \u4e4b\u95f4\u7684\u540c\u6b65\u3002\u901a\u8fc7 channel\uff0c\u591a\u4e2a goroutine \u53ef\u4ee5\u901a\u8fc7\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u8fdb\u884c\u534f\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u521b\u5efa\u548c\u4f7f\u7528 Channel<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>make<\/code> \u51fd\u6570\u521b\u5efa\u4e00\u4e2a channel\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ch := make(chan string) \/\/ \u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684 channel\n\n    go func() {\n        ch &lt;- \"Hello from Goroutine!\" \/\/ \u5411 channel \u53d1\u9001\u6570\u636e\n    }()\n\n    msg := &lt;-ch \/\/ \u4ece channel \u63a5\u6536\u6570\u636e\n    fmt.Println(msg)\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>main<\/code> \u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a channel\uff0c\u5e76\u4e14\u542f\u52a8\u4e86\u4e00\u4e2a goroutine \u5411\u8be5 channel \u53d1\u9001\u6570\u636e\u3002\u4e3b\u51fd\u6570\u4ece channel \u63a5\u6536\u6570\u636e\u5e76\u6253\u5370\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u5173\u95ed Channel<\/strong><\/h3>\n\n\n\n<p>\u5f53\u4f60\u4e0d\u518d\u9700\u8981\u5411 channel \u53d1\u9001\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u5173\u95ed channel\u3002\u5173\u95ed\u7684 channel \u65e0\u6cd5\u518d\u53d1\u9001\u6570\u636e\uff0c\u4f46\u662f\u53ef\u4ee5\u7ee7\u7eed\u63a5\u6536\u6570\u636e\uff0c\u76f4\u5230 channel \u4e2d\u7684\u6240\u6709\u6570\u636e\u90fd\u88ab\u63a5\u6536\u5b8c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ch := make(chan string)\n\n    go func() {\n        ch &lt;- \"Hello\"\n        close(ch) \/\/ \u5173\u95ed channel\n    }()\n\n    msg, ok := &lt;-ch\n    if !ok {\n        fmt.Println(\"Channel closed!\")\n    } else {\n        fmt.Println(msg)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u5728\u591a\u4e2a goroutine \u4e4b\u95f4\u4f7f\u7528 Channel<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u8ba9\u591a\u4e2a goroutine \u4e4b\u95f4\u901a\u8fc7 channel \u8fdb\u884c\u6570\u636e\u4ea4\u6362\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>select<\/code> \u8bed\u53e5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc worker(id int, ch chan string) {\n    ch &lt;- fmt.Sprintf(\"Worker %d done\", id)\n}\n\nfunc main() {\n    ch := make(chan string)\n    for i := 1; i &lt;= 3; i++ {\n        go worker(i, ch)\n    }\n\n    for i := 1; i &lt;= 3; i++ {\n        fmt.Println(&lt;-ch) \/\/ \u4ece channel \u63a5\u6536\u6d88\u606f\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u542f\u52a8\u4e86 3 \u4e2a goroutine\uff0c\u901a\u8fc7 channel \u5c06\u5de5\u4f5c\u5b8c\u6210\u7684\u6d88\u606f\u4f20\u9012\u56de\u4e3b\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Select \u8bed\u53e5<\/strong><\/h2>\n\n\n\n<p><code>select<\/code> \u8bed\u53e5\u53ef\u4ee5\u540c\u65f6\u76d1\u542c\u591a\u4e2a channel\uff0c\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a channel \u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u5b83\u7c7b\u4f3c\u4e8e <code>switch<\/code> \u8bed\u53e5\uff0c\u4f46\u662f\u7528\u4e8e channel \u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 <code>select<\/code> \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ch1 := make(chan string)\n    ch2 := make(chan string)\n\n    go func() {\n        ch1 &lt;- \"From Channel 1\"\n    }()\n\n    go func() {\n        ch2 &lt;- \"From Channel 2\"\n    }()\n\n    select {\n    case msg1 := &lt;-ch1:\n        fmt.Println(msg1)\n    case msg2 := &lt;-ch2:\n        fmt.Println(msg2)\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>select<\/code> \u4f1a\u76d1\u542c <code>ch1<\/code> \u548c <code>ch2<\/code>\uff0c\u5e76\u6253\u5370\u4ece\u5176\u4e2d\u4e00\u4e2a channel \u63a5\u6536\u5230\u7684\u6d88\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 <code>select<\/code> \u7684\u9ed8\u8ba4\u9009\u62e9<\/strong><\/h3>\n\n\n\n<p><code>select<\/code> \u8bed\u53e5\u8fd8\u6709\u4e00\u4e2a <code>default<\/code> \u5206\u652f\uff0c\u5f53\u6240\u6709 channel \u90fd\u6ca1\u6709\u51c6\u5907\u597d\u65f6\uff0c<code>default<\/code> \u5206\u652f\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    ch := make(chan string)\n\n    select {\n    case msg := &lt;-ch:\n        fmt.Println(msg)\n    default:\n        fmt.Println(\"No message received!\")\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c <code>ch<\/code> \u6ca1\u6709\u6570\u636e\uff0c<code>default<\/code> \u4f1a\u88ab\u6267\u884c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Goroutine \u7684\u540c\u6b65<\/strong><\/h2>\n\n\n\n<p>Go \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u540c\u6b65\u591a\u4e2a goroutine \u7684\u6267\u884c\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u6709\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>WaitGroup<\/strong>\uff1a\u7528\u4e8e\u7b49\u5f85\u4e00\u7ec4 goroutine \u5b8c\u6210\u3002<\/li>\n\n\n\n<li><strong>Mutex\uff08\u4e92\u65a5\u9501\uff09<\/strong>\uff1a\u7528\u4e8e\u5728\u591a\u4e2a goroutine \u4e4b\u95f4\u5171\u4eab\u8d44\u6e90\u65f6\u4fdd\u8bc1\u4e92\u65a5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 WaitGroup \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p><code>sync.WaitGroup<\/code> \u7528\u4e8e\u7b49\u5f85\u4e00\u7ec4 goroutine \u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc task(id int, wg *sync.WaitGroup) {\n    defer wg.Done() \/\/ \u5f53\u4efb\u52a1\u5b8c\u6210\u65f6\u8c03\u7528 Done\n    fmt.Printf(\"Task %d started\\n\", id)\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n    for i := 1; i &lt;= 3; i++ {\n        wg.Add(1) \/\/ \u589e\u52a0\u7b49\u5f85\u7684 goroutine \u6570\u91cf\n        go task(i, &amp;wg)\n    }\n\n    wg.Wait() \/\/ \u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\n    fmt.Println(\"All tasks completed\")\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sync.WaitGroup<\/code> \u7528\u6765\u786e\u4fdd\u4e3b\u7a0b\u5e8f\u7b49\u5f85\u6240\u6709 goroutine \u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 Mutex \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p><code>sync.Mutex<\/code> \u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u786e\u4fdd\u5728\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a goroutine \u80fd\u8bbf\u95ee\u8be5\u8d44\u6e90\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nvar mu sync.Mutex\nvar count int\n\nfunc increment() {\n    mu.Lock()         \/\/ \u9501\u5b9a\n    count++\n    mu.Unlock()       \/\/ \u89e3\u9501\n}\n\nfunc main() {\n    for i := 0; i &lt; 5; i++ {\n        go increment()\n    }\n\n    \/\/ \u7b49\u5f85 goroutine \u5b8c\u6210\n    fmt.Println(\"Final count:\", count)\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>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Goroutine<\/strong>\uff1aGo \u63d0\u4f9b\u4e86\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\uff0c\u80fd\u591f\u5728\u540e\u53f0\u5e76\u53d1\u6267\u884c\u4efb\u52a1\uff0c\u6781\u5927\u63d0\u9ad8\u4e86\u5e76\u53d1\u6027\u80fd\u3002<\/li>\n\n\n\n<li><strong>Channel<\/strong>\uff1aGo \u4e2d goroutine \u4e4b\u95f4\u7684\u901a\u4fe1\u662f\u901a\u8fc7 channel \u5b8c\u6210\u7684\uff0cchannel \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u652f\u6301\u540c\u6b65\u548c\u6570\u636e\u4f20\u9012\u3002<\/li>\n\n\n\n<li><strong>Select<\/strong>\uff1a<code>select<\/code> \u8bed\u53e5\u53ef\u4ee5\u76d1\u542c\u591a\u4e2a channel\uff0c\u5f53\u67d0\u4e2a channel \u6709\u6570\u636e\u65f6\uff0c\u6267\u884c\u5bf9\u5e94\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>\u540c\u6b65\u65b9\u5f0f<\/strong>\uff1a\u4f7f\u7528 <code>sync.WaitGroup<\/code> \u7b49\u5de5\u5177\u6765\u7b49\u5f85\u591a\u4e2a goroutine \u5b8c\u6210\uff1b\u4f7f\u7528 <code>sync.Mutex<\/code> \u6765\u4fdd\u8bc1\u5e76\u53d1\u8bbf\u95ee\u65f6\u7684\u8d44\u6e90\u5b89\u5168\u3002<\/li>\n<\/ul>\n\n\n\n<p>Go \u7684\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\u7b80\u5355\u3001\u9ad8\u6548\uff0c\u662f\u8fdb\u884c\u5e76\u53d1\u5904\u7406\u7684\u7406\u60f3\u9009\u62e9\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528 goroutine \u548c channel\uff0c\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u5e76\u53d1\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go \u8bed\u8a00\u7684\u5e76\u53d1\u5904\u7406\u662f\u5176\u4e00\u5927\u4eae\u70b9\u3002Go \u63d0\u4f9b\u4e86\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b\u79f0\u4e3a goroutine\uff0c\u4f7f\u5f97\u5e76\u53d1\u7f16\u7a0b\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u548c [&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-3216","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3216","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=3216"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3216\/revisions"}],"predecessor-version":[{"id":3218,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3216\/revisions\/3218"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}