{"id":1437,"date":"2025-02-08T23:49:54","date_gmt":"2025-02-08T15:49:54","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1437"},"modified":"2025-02-08T23:49:55","modified_gmt":"2025-02-08T15:49:55","slug":"go%e8%af%ad%e8%a8%80%e9%ab%98%e7%ba%a7%e7%89%b9%e6%80%a7%ef%bc%9a%e6%8c%87%e9%92%88%e3%80%81%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%8e%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/08\/go%e8%af%ad%e8%a8%80%e9%ab%98%e7%ba%a7%e7%89%b9%e6%80%a7%ef%bc%9a%e6%8c%87%e9%92%88%e3%80%81%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%8e%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b\/","title":{"rendered":"Go\u8bed\u8a00\u9ad8\u7ea7\u7279\u6027\uff1a\u6307\u9488\u3001\u9762\u5411\u5bf9\u8c61\u4e0e\u5e76\u53d1\u7f16\u7a0b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e0d\u4ec5\u5177\u6709\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u8fd8\u5185\u7f6e\u4e86\u5f3a\u5927\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u8fd9\u4e9b\u7279\u6027\u4f7f\u5f97Go\u5728\u5904\u7406\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\u65f6\u5c24\u5176\u6709\u6548\u3002\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Go\u8bed\u8a00\u4e2d\u7684\u4e00\u4e9b\u9ad8\u7ea7\u7279\u6027\uff0c\u6db5\u76d6\u6307\u9488\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u5e76\u53d1\u7f16\u7a0b\u7b49\u5185\u5bb9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 <strong>\u6307\u9488<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6307\u9488\u5728Go\u8bed\u8a00\u4e2d\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u5141\u8bb8\u4f60\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u5730\u5740\uff0c\u5e76\u901a\u8fc7\u5f15\u7528\u6765\u4f20\u9012\u6570\u636e\u3002Go\u8bed\u8a00\u4e2d\u7684\u6307\u9488\u907f\u514d\u4e86C\/C++\u4e2d\u7684\u8bb8\u591a\u5e38\u89c1\u9519\u8bef\uff0c\u5982\u91ce\u6307\u9488\u7b49\uff0c\u56e0\u6b64\u5b83\u7684\u4f7f\u7528\u65e2\u5b89\u5168\u53c8\u9ad8\u6548\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1.1 <strong>\u6307\u9488\u7684\u5b9a\u4e49\u548c\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6307\u9488\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u7528\u4e8e\u5b58\u50a8\u53e6\u4e00\u4e2a\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u4f7f\u7528\u6307\u9488\u53ef\u4ee5\u5728\u51fd\u6570\u4e4b\u95f4\u4f20\u9012\u5927\u91cf\u6570\u636e\u65f6\u907f\u514d\u6570\u636e\u590d\u5236\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var x int = 58\n    var ptr *int = &amp;x  \/\/ \u83b7\u53d6x\u7684\u5185\u5b58\u5730\u5740\n\n    fmt.Println(\"x:\", x)        \/\/ \u8f93\u51fax\u7684\u503c\n    fmt.Println(\"ptr:\", ptr)    \/\/ \u8f93\u51faptr\u7684\u5730\u5740\n    fmt.Println(\"*ptr:\", *ptr)  \/\/ \u89e3\u5f15\u7528\uff0c\u8f93\u51faptr\u6307\u5411\u7684\u503c\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x: 58\nptr: 0xc0000160a0\n*ptr: 58<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.1.2 <strong>\u6307\u9488\u4e0e\u51fd\u6570\u53c2\u6570\u4f20\u9012<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u51fd\u6570\u53c2\u6570\u9ed8\u8ba4\u662f\u6309\u503c\u4f20\u9012\u3002\u4f7f\u7528\u6307\u9488\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc increment(x *int) {\n    *x = *x + 1  \/\/ \u4fee\u6539\u6307\u9488\u6307\u5411\u7684\u503c\n}\n\nfunc main() {\n    a := 10\n    increment(&amp;a)  \/\/ \u4f20\u9012a\u7684\u5730\u5740\n    fmt.Println(\"a after increment:\", a)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a after increment: 11<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.1.3 <strong>\u6307\u9488\u548c\u7ed3\u6784\u4f53\u7684\u7ed3\u5408<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6307\u9488\u548c\u7ed3\u6784\u4f53\u7ed3\u5408\u4f7f\u7528\uff0c\u5e38\u5e38\u7528\u4e8e\u907f\u514d\u590d\u5236\u5927\u578b\u7ed3\u6784\u4f53\u5e76\u80fd\u591f\u76f4\u63a5\u4fee\u6539\u7ed3\u6784\u4f53\u5185\u5bb9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\ntype Person struct {\n    Name string\n    Age  int\n}\n\nfunc (p *Person) birthday() {\n    p.Age++  \/\/ \u901a\u8fc7\u6307\u9488\u4fee\u6539\u7ed3\u6784\u4f53\u5b57\u6bb5\n}\n\nfunc main() {\n    person := Person{Name: \"John\", Age: 30}\n    person.birthday()  \/\/ \u8c03\u7528\u65b9\u6cd5\u4fee\u6539\u7ed3\u6784\u4f53\u5185\u5bb9\n    fmt.Println(person.Age)  \/\/ \u8f93\u51fa: 31\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 <strong>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u901a\u8fc7\u7ed3\u6784\u4f53\uff08<code>struct<\/code>\uff09\u548c\u63a5\u53e3\uff08<code>interface<\/code>\uff09\u5b9e\u73b0\u4e86\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u6838\u5fc3\u601d\u60f3\uff1a\u5c01\u88c5\u3001\u7ec4\u5408\u548c\u591a\u6001\u3002Go\u5e76\u4e0d\u63d0\u4f9b\u4f20\u7edf\u7684\u7c7b\u548c\u7ee7\u627f\u673a\u5236\uff0c\u800c\u662f\u901a\u8fc7\u7ec4\u5408\u548c\u63a5\u53e3\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.2.1 <strong>\u7ed3\u6784\u4f53\u4e0e\u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u4e2d\u7684\u7ed3\u6784\u4f53\u662f\u5c06\u591a\u4e2a\u5b57\u6bb5\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u3002\u4f60\u53ef\u4ee5\u4e3a\u7ed3\u6784\u4f53\u7c7b\u578b\u5b9a\u4e49\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\ntype Circle struct {\n    Radius float64\n}\n\n\/\/ \u4e3aCircle\u7c7b\u578b\u5b9a\u4e49\u65b9\u6cd5\nfunc (c Circle) Area() float64 {\n    return 3.14 * c.Radius * c.Radius\n}\n\nfunc main() {\n    c := Circle{Radius: 5}\n    fmt.Println(\"Area of circle:\", c.Area())  \/\/ \u8c03\u7528\u65b9\u6cd5\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Area of circle: 78.5<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2.2 <strong>\u63a5\u53e3\uff08interface\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u7684\u63a5\u53e3\u5141\u8bb8\u4f60\u5b9a\u4e49\u4e00\u7ec4\u65b9\u6cd5\u7b7e\u540d\uff0c\u800c\u4efb\u4f55\u7c7b\u578b\u53ea\u8981\u5b9e\u73b0\u4e86\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\uff0c\u5c31\u81ea\u52a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u3002Go\u7684\u63a5\u53e3\u662f\u9690\u5f0f\u5b9e\u73b0\u7684\uff0c\u65e0\u9700\u660e\u786e\u58f0\u660e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\ntype Speaker interface {\n    Speak() string\n}\n\ntype Person struct {\n    Name string\n}\n\nfunc (p Person) Speak() string {\n    return \"Hello, \" + p.Name\n}\n\nfunc greet(s Speaker) {\n    fmt.Println(s.Speak())\n}\n\nfunc main() {\n    p := Person{Name: \"Alice\"}\n    greet(p)  \/\/ \u8f93\u51fa: Hello, Alice\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u4e2d\u7684\u63a5\u53e3\u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\u7c7b\u578b\u5b9e\u73b0\u4e86\u54ea\u4e2a\u63a5\u53e3\uff0c\u53ea\u8981\u7c7b\u578b\u5b9e\u73b0\u4e86\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\uff0cGo\u4f1a\u81ea\u52a8\u8ba4\u4e3a\u8be5\u7c7b\u578b\u5b9e\u73b0\u4e86\u63a5\u53e3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.2.3 <strong>\u7c7b\u578b\u65ad\u8a00\u4e0e\u7c7b\u578b\u9009\u62e9<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u652f\u6301\u7c7b\u578b\u65ad\u8a00\u548c\u7c7b\u578b\u9009\u62e9\uff0c\u5b83\u4eec\u5141\u8bb8\u6211\u4eec\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u68c0\u67e5\u63a5\u53e3\u7684\u5177\u4f53\u7c7b\u578b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7c7b\u578b\u65ad\u8a00\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var i interface{} = 42\nvalue, ok := i.(int)  \/\/ \u5224\u65adi\u662f\u5426\u662fint\u7c7b\u578b\nif ok {\n    fmt.Println(\"value:\", value)\n} else {\n    fmt.Println(\"Not an int!\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7c7b\u578b\u9009\u62e9\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func printType(i interface{}) {\n    switch v := i.(type) {\n    case int:\n        fmt.Println(\"Integer:\", v)\n    case string:\n        fmt.Println(\"String:\", v)\n    default:\n        fmt.Println(\"Unknown type\")\n    }\n}\n\nfunc main() {\n    printType(42)\n    printType(\"Hello\")\n    printType(3.14)\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 <strong>\u5d4c\u5165\u5f0f\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e0d\u652f\u6301\u4f20\u7edf\u7684\u7c7b\u7ee7\u627f\u673a\u5236\uff0c\u800c\u662f\u901a\u8fc7\u5d4c\u5165\u5f0f\u7c7b\u578b\uff08\u7ec4\u5408\uff09\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7ee7\u627f\u7684\u884c\u4e3a\u3002\u5d4c\u5165\u5f0f\u7c7b\u578b\u4f7f\u5f97\u7ed3\u6784\u4f53\u53ef\u4ee5\u7ee7\u627f\u53e6\u4e00\u4e2a\u7ed3\u6784\u4f53\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\ntype Animal struct {\n    Name string\n}\n\nfunc (a *Animal) Speak() {\n    fmt.Println(a.Name, \"makes a sound\")\n}\n\ntype Dog struct {\n    Animal  \/\/ \u5d4c\u5165\u5f0f\u7c7b\u578b\n    Breed   string\n}\n\nfunc main() {\n    d := Dog{Animal: Animal{Name: \"Buddy\"}, Breed: \"Labrador\"}\n    d.Speak()  \/\/ Dog\u901a\u8fc7\u5d4c\u5165\u5f0fAnimal\u7ed3\u6784\u4f53\u7ee7\u627f\u4e86Speak\u65b9\u6cd5\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Buddy makes a sound<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 <strong>\u5e76\u53d1\u7f16\u7a0b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u7684\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\u662f\u5176\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u7279\u70b9\u3002\u901a\u8fc7goroutines\u548cchannels\uff0cGo\u4f7f\u5f97\u5e76\u53d1\u7f16\u7a0b\u53d8\u5f97\u7b80\u5355\u800c\u9ad8\u6548\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.4.1 <strong>goroutines\uff08\u534f\u7a0b\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">goroutines\u662fGo\u8bed\u8a00\u7684\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u3002\u6bcf\u4e2agoroutine\u90fd\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u6808\uff0c\u5b83\u4eec\u7531Go\u8fd0\u884c\u65f6\u8c03\u5ea6\uff0c\u652f\u6301\u5e76\u53d1\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc printMessage(message string) {\n    fmt.Println(message)\n}\n\nfunc main() {\n    go printMessage(\"Hello from goroutine!\")  \/\/ \u542f\u52a8\u4e00\u4e2agoroutine\n    time.Sleep(time.Second)  \/\/ \u7b49\u5f85goroutine\u6267\u884c\u5b8c\u6210\n    fmt.Println(\"Main function finished\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello from goroutine!\nMain function finished<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.4.2 <strong>channels\uff08\u901a\u9053\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Channels\u662fGo\u4e2d\u7528\u4e8e\u5728goroutines\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u673a\u5236\uff0c\u5b83\u5141\u8bb8goroutines\u901a\u8fc7\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u6765\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc greet(c chan string) {\n    c &lt;- \"Hello, Go!\"  \/\/ \u5411channel\u53d1\u9001\u6570\u636e\n}\n\nfunc main() {\n    c := make(chan string)  \/\/ \u521b\u5efa\u4e00\u4e2a\u65e0\u7f13\u51b2\u7684channel\n    go greet(c)\n    message := &lt;-c  \/\/ \u4ecechannel\u63a5\u6536\u6570\u636e\n    fmt.Println(message)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Go!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.4.3 <strong>select\u8bed\u53e5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>select<\/code>\u8bed\u53e5\u5141\u8bb8\u4f60\u5728\u591a\u4e2achannel\u64cd\u4f5c\u4e2d\u9009\u62e9\u4e00\u4e2a\u53ef\u7528\u7684channel\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u5b83\u5728\u5904\u7406\u591a\u4e2achannel\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\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() { ch1 &lt;- \"Message from ch1\" }()\n    go func() { ch2 &lt;- \"Message from ch2\" }()\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 class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Message from ch1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.4.4 <strong>sync\u5305\uff08sync.Mutex\u3001sync.WaitGroup\u7b49\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u63d0\u4f9b\u4e86<code>sync<\/code>\u5305\u7528\u4e8e\u5904\u7406\u5e76\u53d1\u4e2d\u7684\u540c\u6b65\u95ee\u9898\u3002<code>sync.Mutex<\/code>\u7528\u4e8e\u786e\u4fdd\u53ea\u6709\u4e00\u4e2agoroutine\u80fd\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u800c<code>sync.WaitGroup<\/code>\u5219\u7528\u4e8e\u7b49\u5f85\u591a\u4e2agoroutine\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nvar counter int\nvar mu sync.Mutex  \/\/ \u7528\u4e8e\u540c\u6b65\n\nfunc increment() {\n    mu.Lock()\n    counter++\n    mu.Unlock()\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n\n    for i := 0; i &lt; 5; i++ {\n        wg.Add(1)\n        go func() {\n            defer wg.Done()\n            increment()\n        }()\n    }\n\n    wg.Wait()  \/\/ \u7b49\u5f85\u6240\u6709goroutine\u5b8c\u6210\n    fmt.Println(\"Final counter value:\", counter)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Final counter value: 5<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4.5 <strong>\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u5e76\u53d1\u6a21\u578b\u901a\u8fc7goroutines\u548cchannels\u4f7f\u5f97\u5f88\u591a\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f\u53d8\u5f97\u7b80\u5355\u3002\u5e38\u89c1\u7684\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f\u5305\u62ec\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u3001\u5de5\u4f5c\u6c60\u6a21\u5f0f\u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.5.1 <strong>\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u5e38\u7528\u4e8e\u591a\u4efb\u52a1\u5904\u7406\u573a\u666f\uff0c\u5176\u4e2d\u751f\u4ea7\u8005\u4ea7\u751f\u6570\u636e\uff0c\u6d88\u8d39\u8005\u4ece\u7f13\u51b2\u533a\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5904\u7406\u3002Go\u7684channel\u662f\u5b9e\u73b0\u8fd9\u4e00\u6a21\u5f0f\u7684\u7406\u60f3\u5de5\u5177\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc producer(ch chan int) {\n    for i := 0; i &lt; 5; i++ {\n        ch &lt;- i\n        fmt.Println(\"Produced:\", i)\n        time.Sleep(time.Second)\n    }\n    close(ch)\n}\n\nfunc consumer(ch chan int) {\n    for item := range ch {\n        fmt.Println(\"Consumed:\", item)\n    }\n}\n\nfunc main() {\n    ch := make(chan int)\n    go producer(ch)\n    go consumer(ch)\n    time.Sleep(6 * time.Second)  \/\/ \u7b49\u5f85\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u5b8c\u6210\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Produced: 0\nConsumed: 0\nProduced: 1\nConsumed: 1\nProduced: 2\nConsumed: 2\n...<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u7ae0\u8282\u6df1\u5165\u8bb2\u89e3\u4e86Go\u8bed\u8a00\u7684\u4e00\u4e9b\u9ad8\u7ea7\u7279\u6027\uff0c\u5305\u62ec\u6307\u9488\u3001\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3001\u5e76\u53d1\u7f16\u7a0b\u7b49\u3002\u901a\u8fc7\u6307\u9488\u548c\u7ed3\u6784\u4f53\uff0c\u6211\u4eec\u80fd\u591f\u9ad8\u6548\u5730\u7ba1\u7406\u5185\u5b58\u548c\u6570\u636e\uff0c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u901a\u8fc7\u63a5\u53e3\u548c\u5d4c\u5165\u5f0f\u7c7b\u578b\u5b9e\u73b0\u4e86\u7075\u6d3b\u7684\u4ee3\u7801\u7ec4\u7ec7\u65b9\u5f0f\u3002\u540c\u65f6\uff0cGo\u7684\u5e76\u53d1\u7f16\u7a0b\u6a21\u578b\uff08\u5305\u62ecgoroutines\u3001channels\u3001select\u8bed\u53e5\u548csync\u5305\uff09\u4f7f\u5f97\u7f16\u5199\u9ad8\u6548\u3001\u53ef\u6269\u5c55\u7684\u5e76\u53d1\u7a0b\u5e8f\u53d8\u5f97\u5f02\u5e38\u7b80\u5355\u3002\u638c\u63e1\u8fd9\u4e9b\u9ad8\u7ea7\u7279\u6027\uff0c\u5c06\u4f7f\u4f60\u5728Go\u8bed\u8a00\u7684\u5f00\u53d1\u4e2d\u5982\u864e\u6dfb\u7ffc\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u8bed\u8a00\u4e0d\u4ec5\u5177\u6709\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u8fd8\u5185\u7f6e\u4e86\u5f3a\u5927\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u8fd9\u4e9b\u7279\u6027\u4f7f\u5f97Go\u5728\u5904\u7406\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\u65f6\u5c24\u5176\u6709\u6548\u3002\u8fd9\u91cc\uff0c\u6211\u4eec [&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-1437","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1437","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=1437"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"predecessor-version":[{"id":1456,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1437\/revisions\/1456"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}