{"id":60485,"date":"2026-01-15T18:06:03","date_gmt":"2026-01-15T10:06:03","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/60485.html"},"modified":"2026-01-15T18:06:03","modified_gmt":"2026-01-15T10:06:03","slug":"%e6%9c%80%e8%af%a6%e7%bb%86%e7%9a%84%e5%90%8e%e7%ab%af%e9%9d%a2%e8%af%95%e7%b3%bb%e5%88%97%e5%86%85%e5%ae%b9-golang%e5%85%ab%e8%82%a1%ef%bc%88%e6%8c%81%e7%bb%ad%e6%9b%b4%e6%96%b0%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/60485.html","title":{"rendered":"\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09"},"content":{"rendered":"<h2 id=\"%E5%BC%95%E8%A8%80\">\u5f15\u8a00<\/h2>\n<p>\u8fd9\u91cc\u662f\u9633\u660eCoding&#xff0c;\u672c\u671f\u5e26\u6765\u7684\u662f\u7ed9\u9762\u8bd5golang\u540e\u7aef\u5f00\u53d1\u5c97\u7684\u670b\u53cb\u4eec&#xff0c;\u6700\u8be6\u7ec6\u7684golang\u9762\u8bd5\u516b\u80a1\u3002golang\u7684\u516b\u80a1\u4e3b\u8981\u662f\u56f4\u7ed5\u534f\u7a0b&#xff0c;\u5185\u5b58\u6a21\u578b&#xff0c;Channel\u7b49\u5185\u5bb9\u5c55\u5f00\u3002\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u5f00\u59cb\u4eca\u5929\u7684\u6c89\u6d78\u5f0f\u516b\u80a1\u4e4b\u65c5<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115100602-6968bc0a0786f.png\" \/><\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E5%BC%95%E8%A8%80-toc\" style=\"margin-left:0px\">\u5f15\u8a00<\/p>\n<p id=\"golang%E7%9A%84%E5%8D%8F%E7%A8%8B%E8%AF%B4%E4%B8%80%E4%B8%8B-toc\" style=\"margin-left:0px\">golang\u7684\u534f\u7a0b\u8bf4\u4e00\u4e0b<\/p>\n<p id=\"gmp%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B-toc\" style=\"margin-left:0px\">gmp\u5185\u5b58\u6a21\u578b<\/p>\n<p id=\"%E4%B8%89%E8%89%B2%E6%A0%87%E8%AE%B0%E6%B3%95%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6-toc\" style=\"margin-left:0px\">\u4e09\u8272\u6807\u8bb0\u6cd5\u5783\u573e\u56de\u6536<\/p>\n<p id=\"make%E5%92%8Cnew%E7%9A%84%E5%8C%BA%E5%88%AB-toc\" style=\"margin-left:0px\">make\u548cnew\u7684\u533a\u522b<\/p>\n<p id=\"golang%E7%9A%84map%E7%BB%93%E6%9E%84-toc\" style=\"margin-left:0px\">golang\u7684map\u7ed3\u6784<\/p>\n<p id=\"%E6%9C%89%E7%BC%93%E5%86%B2Channel%E5%92%8C%E6%97%A0%E7%BC%93%E5%86%B2Channel%E5%8C%BA%E5%88%AB-toc\" style=\"margin-left:0px\">\u6709\u7f13\u51b2Channel\u548c\u65e0\u7f13\u51b2Channel\u533a\u522b<\/p>\n<p id=\"golang%E5%B9%B6%E5%8F%91%E5%AE%89%E5%85%A8-toc\" style=\"margin-left:0px\">golang\u5e76\u53d1\u5b89\u5168<\/p>\n<p id=\"%E6%8E%A5%E5%8F%A3%E4%B8%8E%E5%8F%8D%E5%B0%84-toc\" style=\"margin-left:0px\">\u63a5\u53e3\u4e0e\u53cd\u5c04<\/p>\n<hr id=\"hr-toc\" \/>\n<h2>golang\u7684\u6545\u4e8b<\/h2>\n<p>\u5728\u5f00\u59cb\u6c89\u6d78\u5f0f\u516b\u80a1\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u6765\u5f00\u59cb\u4e00\u6bb5golang\u7684\u6545\u4e8b<\/p>\n<p><span style=\"color:#fe2c24\">Golang \u7684\u6545\u4e8b&#xff1a;\u4e00\u95e8\u201c\u88ab\u5de5\u7a0b\u5e08\u903c\u51fa\u6765\u201d\u7684\u8bed\u8a00<\/span><\/p>\n<p>Go \u8bed\u8a00\u7684\u8bde\u751f&#xff0c;\u5e76\u4e0d\u662f\u56e0\u4e3a\u201c\u4e16\u754c\u4e0a\u8fd8\u4e0d\u591f\u591a\u7684\u7f16\u7a0b\u8bed\u8a00\u201d&#xff0c;\u800c\u662f\u56e0\u4e3a\u5de5\u7a0b\u5e08\u771f\u7684\u88ab\u73b0\u6709\u5de5\u5177\u6298\u78e8\u591f\u4e86\u3002\u65f6\u95f4\u56de\u5230 2007 \u5e74&#xff0c;\u5730\u70b9\u662f Google \u603b\u90e8\u3002<\/p>\n<p>\u90a3\u4e00\u5e74&#xff0c;Google \u7684\u4ee3\u7801\u5e93\u5df2\u7ecf\u5927\u5230\u4ee4\u4eba\u5d29\u6e83&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6570\u767e\u4e07\u884c C&#043;&#043;<\/p>\n<\/li>\n<li>\n<p>\u7f16\u8bd1\u4e00\u6b21\u52a8\u8f84\u51e0\u5341\u5206\u949f<\/p>\n<\/li>\n<li>\n<p>\u591a\u6838 CPU \u5df2\u7ecf\u666e\u53ca&#xff0c;\u4f46\u5e76\u53d1\u7f16\u7a0b\u5374\u53c8\u590d\u6742\u53c8\u5371\u9669<\/p>\n<\/li>\n<li>\n<p>\u6784\u5efa\u7cfb\u7edf\u50cf\u4e00\u53f0\u968f\u65f6\u4f1a\u7206\u70b8\u7684\u673a\u5668<\/p>\n<\/li>\n<\/ul>\n<p>\u6709\u4e00\u5929&#xff0c;Google \u7684\u4e09\u4f4d\u5de5\u7a0b\u5e08\u5750\u5728\u529e\u516c\u5ba4\u91cc&#xff0c;\u5bf9\u7740\u7f13\u6162\u7684\u7f16\u8bd1\u5668\u53d1\u7262\u9a9a&#xff1a;<\/p>\n<ul>\n<li>\n<p>Rob Pike&#xff08;Unix \u8001\u5175&#xff0c;\u300aUnix \u7f16\u7a0b\u73af\u5883\u300b\u4f5c\u8005&#xff09;<\/p>\n<\/li>\n<li>\n<p>Ken Thompson&#xff08;C \u8bed\u8a00\u4e4b\u7236\u3001Unix \u4e4b\u7236&#xff09;<\/p>\n<\/li>\n<li>\n<p>Robert Griesemer&#xff08;\u7f16\u8bd1\u5668\u4e13\u5bb6&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>\u4ed6\u4eec\u5fc3\u91cc\u6709\u4e00\u4e2a\u5171\u540c\u7684\u7591\u95ee&#xff1a;<\/p>\n<p>\u201c\u4e3a\u4ec0\u4e48\u5199\u7a0b\u5e8f&#xff0c;\u53cd\u800c\u53d8\u5f97\u8d8a\u6765\u8d8a\u6162\u3001\u8d8a\u6765\u8d8a\u590d\u6742\u4e86&#xff1f;\u201d<\/p>\n<p><span style=\"color:#fe2c24\">\u4ed6\u4eec\u60f3\u8981\u4e00\u95e8\u4ec0\u4e48\u6837\u7684\u8bed\u8a00&#xff1f;<\/span><\/p>\n<p>\u4e0d\u662f\u201c\u6700\u5f3a\u5927\u201d&#xff0c;\u800c\u662f\u6700\u987a\u624b\u3002<\/p>\n<p>\u4ed6\u4eec\u5217\u4e86\u4e00\u5f20\u201c\u53cd\u9700\u6c42\u6e05\u5355\u201d&#xff1a;<\/p>\n<p>\u274c \u4e0d\u8981\u590d\u6742\u7684\u6a21\u677f \u274c \u4e0d\u8981\u96be\u4ee5\u7406\u89e3\u7684\u7ee7\u627f\u5c42\u7ea7 \u274c \u4e0d\u8981\u6f2b\u957f\u7684\u7f16\u8bd1\u65f6\u95f4 \u274c \u4e0d\u8981\u8ba9\u5e76\u53d1\u6210\u4e3a\u5669\u68a6<\/p>\n<p>\u76f8\u53cd&#xff0c;\u4ed6\u4eec\u60f3\u8981\u7684\u662f&#xff1a;<\/p>\n<p>\u2705 \u50cf C \u4e00\u6837\u7b80\u5355 \u2705 \u50cf Python \u4e00\u6837\u5199\u5f97\u5feb \u2705 \u539f\u751f\u652f\u6301\u5e76\u53d1 \u2705 \u7f16\u8bd1\u5feb\u5230\u201c\u51e0\u4e4e\u611f\u89c9\u4e0d\u5230\u201d<\/p>\n<p>\u4e8e\u662f&#xff0c;\u4ed6\u4eec\u51b3\u5b9a&#xff0c;\u5e72\u8106\u81ea\u5df1\u9020\u4e00\u95e8\u8bed\u8a00\u3002<\/p>\n<\/p>\n<p><span style=\"color:#fe2c24\">Go \u7684\u540d\u5b57&#xff0c;\u5176\u5b9e\u5f88\u201c\u4efb\u6027\u201d<\/span><\/p>\n<p>\u6700\u521d&#xff0c;\u8fd9\u95e8\u8bed\u8a00\u7684\u540d\u5b57\u5c31\u53eb \u201cGo\u201d\u3002\u539f\u56e0\u5f88\u7b80\u5355&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u77ed<\/p>\n<\/li>\n<li>\n<p>\u597d\u6253<\/p>\n<\/li>\n<li>\n<p>\u597d\u8bb0<\/p>\n<\/li>\n<\/ul>\n<p>\u6709\u4eba\u8bf4\u5b83\u592a\u968f\u610f\u4e86&#xff0c;\u4f46\u8fd9\u6070\u6070\u7b26\u5408 Go \u7684\u6c14\u8d28&#xff1a; \u4e0d\u70ab\u6280&#xff0c;\u4e0d\u590d\u6742&#xff0c;\u53ea\u5411\u524d\u8d70\u3002<\/p>\n<p>&#xff08;\u540e\u6765\u4e3a\u4e86\u907f\u514d\u548c\u52a8\u8bcd\u6df7\u6dc6&#xff0c;\u5b98\u65b9\u6587\u6863\u91cc\u66f4\u591a\u4f7f\u7528 \u201cGo language\u201d \u6216 \u201cGolang\u201d \u8fd9\u4e2a\u53eb\u6cd5\u3002&#xff09;<\/p>\n<\/p>\n<hr \/>\n<h2 id=\"golang%E7%9A%84%E5%8D%8F%E7%A8%8B%E8%AF%B4%E4%B8%80%E4%B8%8B\">golang\u7684\u534f\u7a0b\u8bf4\u4e00\u4e0b<\/h2>\n<p>golang\u7684\u534f\u7a0bGoroutine\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u7ebf\u7a0b&#xff0c;\u7531go\u8fd0\u884c\u65f6\u7ba1\u7406\u3002\u521d\u59cb\u6808\u4ec52kb&#xff0c;\u4e0e\u7ebf\u7a0bM&#xff1a;N\u6620\u5c04&#xff0c;\u9ad8\u6548\u5229\u7528\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<p>func main() {<br \/>\n    go func() {<br \/>\n        fmt.Println(&#034;Goroutine running&#034;)<br \/>\n    }()<\/p>\n<p>    time.Sleep(time.Second)<br \/>\n}<\/p>\n<\/p>\n<hr \/>\n<h2 id=\"gmp%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B\">gmp\u5185\u5b58\u6a21\u578b<\/h2>\n<p>golang\u7684gmp\u5185\u5b58\u6a21\u578b\u4e3b\u8981\u7531\u4e09\u90e8\u5206\u7ec4\u6210&#xff1a;<\/p>\n<p>G&#xff1a;Goroutine&#xff0c;\u534f\u7a0b\u5bf9\u8c61<\/p>\n<p>M&#xff1a;Machine&#xff0c;\u7cfb\u7edf\u7ebf\u7a0b&#xff0c;\u8d1f\u8d23\u6267\u884cG<\/p>\n<p>P&#xff1a;Processor&#xff0c;\u903b\u8f91\u5904\u7406\u5668&#xff0c;\u8d1f\u8d23\u7ba1\u7406G\u961f\u5217<\/p>\n<\/p>\n<p>\u4f18\u52bf&#xff1a;\u51cf\u5c11\u7ebf\u7a0b\u9891\u7e41\u7684\u521b\u5efa\u548c\u9500\u6bc1&#xff0c;\u5229\u7528\u591a\u6838\u5e76\u884c\u7684\u4f18\u52bf&#xff0c;\u907f\u514d\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3002<\/p>\n<\/p>\n<h2>golang\u5783\u573e\u56de\u6536\u7684\u8fc7\u7a0b<\/h2>\n<p>\u5728 Go \u4e2d&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5bf9\u8c61\u5728 \u5806&#xff08;heap&#xff09; \u4e0a\u5206\u914d<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u518d\u88ab\u5f15\u7528\u7684\u5bf9\u8c61 \u2192 \u5783\u573e<\/p>\n<\/li>\n<li>\n<p>GC \u7684\u4efb\u52a1&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u627e\u51fa\u201c\u8fd8\u6d3b\u7740\u7684\u5bf9\u8c61\u201d<\/p>\n<\/li>\n<li>\n<p>\u56de\u6536\u201c\u4e0d\u53ef\u8fbe\u5bf9\u8c61\u201d<\/p>\n<\/li>\n<li>\n<p>\u5c3d\u91cf\u51cf\u5c11 Stop-The-World&#xff08;STW&#xff09;\u65f6\u95f4<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>1. STW&#xff08;\u5f88\u77ed&#xff09;<br \/>\n2. \u5e76\u53d1\u6807\u8bb0&#xff08;Mark&#xff09;<br \/>\n3. STW&#xff08;\u5f88\u77ed&#xff09;<br \/>\n4. \u5e76\u53d1\u6e05\u626b&#xff08;Sweep&#xff09;<\/p>\n<\/p>\n<hr \/>\n<h2 id=\"%E4%B8%89%E8%89%B2%E6%A0%87%E8%AE%B0%E6%B3%95%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6\" style=\"background-color:transparent\">\u4e09\u8272\u6807\u8bb0\u6cd5\u5783\u573e\u56de\u6536<\/h2>\n<p>\u521d\u59cb\u6807\u8bb0\u65f6\u4f1a\u8fdb\u884c\u4e00\u6b21stw\u6682\u505c<\/p>\n<\/p>\n<p>\u6807\u8bb0\u8fc7\u7a0b&#xff1a;<\/p>\n<ul>\n<li>\n<p>GC Root&#xff08;\u5168\u5c40\u53d8\u91cf\u3001\u6808\u53d8\u91cf\u7b49&#xff09; \u2192 \u7070\u8272<\/p>\n<\/li>\n<li>\n<p>\u7070\u8272\u5bf9\u8c61&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u626b\u63cf\u5f15\u7528<\/p>\n<\/li>\n<li>\n<p>\u5f15\u7528\u5bf9\u8c61\u6807\u4e3a\u7070\u8272<\/p>\n<\/li>\n<li>\n<p>\u81ea\u5df1\u53d8\u9ed1<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u91cd\u590d\u76f4\u5230 \u6ca1\u6709\u7070\u8272\u5bf9\u8c61<\/p>\n<\/li>\n<\/ul>\n<p>\u5f3a\u4e09\u8272&#xff1a;\u4efb\u4f55\u9ed1\u8272\u5bf9\u8c61\u4e0d\u80fd\u76f4\u63a5\u5f15\u7528\u767d\u8272\u5bf9\u8c61<\/p>\n<p>\u5f31\u4e09\u8272&#xff1a;\u9ed1\u8272\u5bf9\u8c61\u53ef\u4ee5\u5f15\u7528\u767d\u8272\u5bf9\u8c61&#xff0c;\u4f46\u5fc5\u987b\u6709\u7070\u8272\u5bf9\u8c61\u4f5c\u4e3a\u4e2d\u95f4\u5a92\u4ecb<\/p>\n<hr \/>\n<h2>\u5199\u5c4f\u969c\u662f\u4ec0\u4e48<\/h2>\n<p>\u5728\u6307\u9488\u5199\u5165\u65f6&#xff0c;\u4fdd\u8bc1\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\u4e0d\u4f1a\u662f\u201c\u767d\u8272\u201d\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u63d2\u5165\u5199\u5c4f\u969c&#xff08;Dijkstra&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5220\u9664\u5199\u5c4f\u969c&#xff08;Yuasa&#xff09;<\/p>\n<\/li>\n<\/ul>\n<\/p>\n<hr \/>\n<h2 id=\"make%E5%92%8Cnew%E7%9A%84%E5%8C%BA%E5%88%AB\">make\u548cnew\u7684\u533a\u522b<\/h2>\n<p>make\u4e00\u822c\u7528\u4e8e\u521d\u59cb\u5316slice&#xff0c;map&#xff0c;Channel\u3002\u8fd4\u56de\u7684\u662f\u5f15\u7528\u7c7b\u578b\u672c\u8eab\u3002\u662f\u4e00\u4e2a\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u5bf9\u8c61<\/p>\n<p>new\u7528\u4e8e\u5206\u914d\u5185\u5b58&#xff0c;\u8fd4\u56de\u6307\u9488&#xff0c;\u8fd4\u56de\u6307\u5411\u96f6\u503c\u7684\u6307\u9488&#xff0c;\u5e76\u4e0d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<\/p>\n<p>\/\/ new&#xff1a;\u8fd4\u56de\u6307\u9488<br \/>\np :&#061; new(int)<br \/>\nfmt.Println(*p) \/\/ 0<\/p>\n<p>\/\/ make&#xff1a;\u8fd4\u56de\u53ef\u7528\u5bf9\u8c61<br \/>\ns :&#061; make([]int, 0, 4)<br \/>\ns &#061; append(s, 1, 2, 3)<\/p>\n<hr \/>\n<h2 id=\"golang%E7%9A%84map%E7%BB%93%E6%9E%84\">golang\u7684map\u7ed3\u6784<\/h2>\n<\/p>\n<\/p>\n<hr \/>\n<h2 id=\"%E6%9C%89%E7%BC%93%E5%86%B2Channel%E5%92%8C%E6%97%A0%E7%BC%93%E5%86%B2Channel%E5%8C%BA%E5%88%AB\">\u6709\u7f13\u51b2Channel\u548c\u65e0\u7f13\u51b2Channel\u533a\u522b<\/h2>\n<p>\u65e0\u7f13\u51b2Channel\u5177\u6709\u540c\u6b65\u963b\u585e&#xff0c;\u751f\u4ea7\u548c\u6d88\u8d39\u5fc5\u987b\u540c\u65f6\u8fdb\u884c\u3002\u9002\u7528\u4e8e\u4e00\u4e9b\u4efb\u52a1\u987a\u5e8f\u6267\u884c\u7684\u60c5\u51b5<\/p>\n<p>ch :&#061; make(chan int)<\/p>\n<p>go func() {<br \/>\n    ch &lt;- 1 \/\/ \u963b\u585e&#xff0c;\u76f4\u5230\u6709\u4eba\u63a5\u6536<br \/>\n}()<\/p>\n<p>fmt.Println(&lt;-ch)<\/p>\n<p>\u6709\u7f13\u51b2Channel\u4e3a\u5f02\u6b65&#xff0c;\u7f13\u51b2\u533a\u672a\u6ee1\u6216\u4e0d\u4e3a\u7a7a\u65f6\u4e0d\u4f1a\u963b\u585e\u3002\u5929\u7136\u961f\u5217\u7ed3\u6784&#xff0c;\u9002\u5408\u751f\u4ea7\u2014\u2014\u6d88\u8d39\u6a21\u578b<\/p>\n<p>ch :&#061; make(chan int, 2)<\/p>\n<p>ch &lt;- 1<br \/>\nch &lt;- 2<br \/>\n\/\/ ch &lt;- 3 \/\/ \u963b\u585e&#xff08;\u6ee1\u4e86&#xff09;<\/p>\n<p>fmt.Println(&lt;-ch)<\/p>\n<hr \/>\n<h2 id=\"golang%E5%B9%B6%E5%8F%91%E5%AE%89%E5%85%A8\">golang\u5e76\u53d1\u5b89\u5168<\/h2>\n<p>1. \u8bed\u8a00\u7ea7\u522b&#xff1a;Channel\u901a\u8fc7\u901a\u4fe1\u5171\u4eab\u6570\u636e&#xff0c;\u800c\u4e0d\u662f\u901a\u8fc7\u5171\u4eab\u6570\u636e\u901a&#xff0c;\u907f\u514d\u4e86\u5e76\u53d1\u4e0b\u7684\u4e00\u4e2a\u6570\u636e\u7ade\u4e89\u7684\u95ee\u9898\u3002<\/p>\n<p>2. \u540c\u6b65\u539f\u8bed&#xff1a;<\/p>\n<p><span style=\"color:#fe2c24\">Mutex&#xff08;\u4e92\u65a5\u9501&#xff09;\u00a0<\/span><\/p>\n<p>var mu sync.Mutex<br \/>\ncount :&#061; 0<\/p>\n<p>mu.Lock()<br \/>\ncount&#043;&#043;<br \/>\nmu.Unlock()<\/p>\n<p><span style=\"color:#fe2c24\">\u00a0RWMutex&#xff08;\u591a\u8bfb\u5e76\u53d1&#xff0c;\u5199\u4e92\u65a5&#xff09;<\/span><\/p>\n<p>var rw sync.RWMutex<br \/>\nrw.RLock()<br \/>\n\/\/ read<br \/>\nrw.RUnlock()<\/p>\n<p><span style=\"color:#fe2c24\">WaitGroup&#xff08;\u7b49\u5f85\u4e00\u7ec4Goroutine\u5b8c\u6210&#xff09;&#xff0c;\u4e0d\u505a\u6570\u636e\u4fdd\u62a4&#xff0c;\u53ea\u505a\u751f\u547d\u5468\u671f\u540c\u6b65<\/span><\/p>\n<p>var wg sync.WaitGroup<\/p>\n<p>wg.Add(1)<br \/>\ngo func() {<br \/>\n    defer wg.Done()<br \/>\n    fmt.Println(&#034;done&#034;)<br \/>\n}()<\/p>\n<p>wg.Wait()<\/p>\n<p>Cond&#xff08;\u6761\u4ef6\u53d8\u91cf&#xff09;\u9002\u5408\u590d\u6742\u7b49\u5f85\u6761\u4ef6&#xff0c;\u6bd4Channel\u66f4\u5e95\u5c42<\/p>\n<p><span style=\"color:#fe2c24\">Once&#xff08;\u53ea\u6267\u884c\u4e00\u6b21&#xff09;\u5e76\u53d1\u5b89\u5168\u7684\u5355\u4f8b\u6216\u8005\u521d\u59cb\u5316<\/span><\/p>\n<p>var once sync.Once<\/p>\n<p>func initConfig() {<br \/>\n    fmt.Println(&#034;init once&#034;)<br \/>\n}<\/p>\n<p>once.Do(initConfig)<\/p>\n<p>3. \u539f\u5b50\u64cd\u4f5c&#xff1a;atomic\u5305\u3002CPU\u539f\u5b50\u6307\u4ee4&#xff0c;\u65e0\u9501&#xff0c;\u9ad8\u6027\u80fd<\/p>\n<\/p>\n<hr \/>\n<h2 id=\"%E6%8E%A5%E5%8F%A3%E4%B8%8E%E5%8F%8D%E5%B0%84\">\u63a5\u53e3\u4e0e\u53cd\u5c04<\/h2>\n<p>golang\u7684\u63a5\u53e3\u53ef\u4ee5\u901a\u8fc7\u9690\u5f0f\u5b9e\u73b0&#xff0c;\u4e0d\u9700\u8981implement\u5173\u952e\u5b57&#xff0c;\u53ea\u9700\u8981\u65b9\u6cd5\u96c6\u5339\u914d&#xff0c;\u5c31\u81ea\u52a8\u5b9e\u73b0\u63a5\u53e3<\/p>\n<p>type Reader interface {<br \/>\n    Read(p []byte) (int, error)<br \/>\n}<\/p>\n<p>type File struct{}<\/p>\n<p>func (f *File) Read(p []byte) (int, error) {<br \/>\n    return 0, nil<br \/>\n}<\/p>\n<p>var _ Reader &#061; (*File)(nil)<\/p>\n<hr \/>\n<h2>golang\u7684context\u7684\u4f5c\u7528<\/h2>\n<p>golang\u7684context\u7528\u4e8e\u5e76\u53d1\u548c\u8c03\u7528\u94fe\u4e2d\u4f20\u9012\u53d6\u6d88\u4fe1\u53f7&#xff0c;\u8d85\u5e02\/\u622a\u6b62\u65f6\u95f4\u4ee5\u53ca\u8bf7\u6c42\u7ea7\u5143\u7d20\u3002\u7528\u6765\u63a7\u5236Goroutine\u7684\u751f\u547d\u5468\u671f&#xff0c;\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f<\/p>\n<p>\u5b83\u53ef\u4ee5\u5728\u591a\u5c42\u51fd\u6570\u8c03\u7528\u548c\u591a\u4e2a goroutine \u4e4b\u95f4\u4f20\u64ad \u53d6\u6d88&#xff08;cancel&#xff09;\u3001\u8d85\u65f6&#xff08;timeout\/deadline&#xff09; \u548c \u5143\u6570\u636e&#xff08;value&#xff09;&#xff0c;<\/p>\n<\/p>\n<p><span style=\"color:#fe2c24\">\u56db\u4e2a\u6838\u5fc3\u65b9\u6cd5<\/span> Deadline \u5b9a\u4e49\u65f6\u95f4\u8fb9\u754c&#xff0c;Done \u8d1f\u8d23\u53d6\u6d88\u901a\u77e5&#xff0c;Err \u8868\u793a\u53d6\u6d88\u539f\u56e0&#xff0c;Value \u7528\u4e8e\u5728\u8c03\u7528\u94fe\u4e2d\u4f20\u9012\u8bf7\u6c42\u7ea7\u5143\u6570\u636e\u3002<\/p>\n<\/p>\n<hr \/>\n<p>\u4ee5\u4e0a\u5c31\u662f\u540e\u7aef\u9762\u8bd5\u95ee\u9898\u2014\u2014golang\u516b\u80a1\u6587\u7684\u5168\u90e8\u5185\u5bb9\u3002\u6709\u4ec0\u4e48\u95ee\u9898\u548c\u610f\u89c1\u4ee5\u53ca\u5176\u4ed6\u60f3\u6cd5\u7684\u53ef\u4ee5\u5728\u8bc4\u8bba\u533a\u8fdb\u884c\u7559\u8a00\u3002\u5982\u679c\u8fd9\u7bc7\u535a\u5ba2\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u53ef\u4ee5\u70b9\u8d5e\u6536\u85cf\u52a0\u5173\u6ce8\u3002\u4f60\u4eec\u7684\u652f\u6301\u662f\u6211\u66f4\u65b0\u7684\u6700\u5927\u52a8\u529b\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u8fd9\u91cc\u662f\u9633\u660eCoding&#xff0c;\u672c\u671f\u5e26\u6765\u7684\u662f\u7ed9\u9762\u8bd5golang\u540e\u7aef\u5f00\u53d1\u5c97\u7684\u670b\u53cb\u4eec&#xff0c;\u6700\u8be6\u7ec6\u7684golang\u9762\u8bd5\u516b\u80a1\u3002golang\u7684\u516b\u80a1\u4e3b\u8981\u662f\u56f4\u7ed5\u534f\u7a0b&#xff0c;\u5185\u5b58\u6a21\u578b&#xff0c;Channel\u7b49\u5185\u5bb9\u5c55\u5f00\u3002\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u5f00\u59cb\u4eca\u5929\u7684\u6c89\u6d78\u5f0f\u516b\u80a1\u4e4b\u65c5 \u76ee\u5f55<br \/>\n\u5f15\u8a00<br \/>\ngolang\u7684\u534f\u7a0b\u8bf4\u4e00\u4e0b<br \/>\ngmp\u5185\u5b58\u6a21\u578b<br \/>\n\u4e09\u8272\u6807\u8bb0\u6cd5\u5783\u573e\u56de\u6536<br \/>\nmake\u548cnew\u7684\u533a\u522b<br \/>\ngolang\u7684map\u7ed3\u6784<br \/>\n\u6709\u7f13\u51b2Channel\u548c\u65e0\u7f13\u51b2Channel\u533a\u522b<br \/>\ngol<\/p>\n","protected":false},"author":2,"featured_media":60484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[925,176],"topic":[],"class_list":["post-60485","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-925","tag-176"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/60485.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5f15\u8a00 \u8fd9\u91cc\u662f\u9633\u660eCoding&#xff0c;\u672c\u671f\u5e26\u6765\u7684\u662f\u7ed9\u9762\u8bd5golang\u540e\u7aef\u5f00\u53d1\u5c97\u7684\u670b\u53cb\u4eec&#xff0c;\u6700\u8be6\u7ec6\u7684golang\u9762\u8bd5\u516b\u80a1\u3002golang\u7684\u516b\u80a1\u4e3b\u8981\u662f\u56f4\u7ed5\u534f\u7a0b&#xff0c;\u5185\u5b58\u6a21\u578b&#xff0c;Channel\u7b49\u5185\u5bb9\u5c55\u5f00\u3002\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u5f00\u59cb\u4eca\u5929\u7684\u6c89\u6d78\u5f0f\u516b\u80a1\u4e4b\u65c5 \u76ee\u5f55 \u5f15\u8a00 golang\u7684\u534f\u7a0b\u8bf4\u4e00\u4e0b gmp\u5185\u5b58\u6a21\u578b \u4e09\u8272\u6807\u8bb0\u6cd5\u5783\u573e\u56de\u6536 make\u548cnew\u7684\u533a\u522b golang\u7684map\u7ed3\u6784 \u6709\u7f13\u51b2Channel\u548c\u65e0\u7f13\u51b2Channel\u533a\u522b gol\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/60485.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-15T10:06:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115100602-6968bc0a0786f.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/60485.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/60485.html\",\"name\":\"\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-15T10:06:03+00:00\",\"dateModified\":\"2026-01-15T10:06:03+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/60485.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/60485.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/60485.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/60485.html","og_locale":"zh_CN","og_type":"article","og_title":"\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5f15\u8a00 \u8fd9\u91cc\u662f\u9633\u660eCoding&#xff0c;\u672c\u671f\u5e26\u6765\u7684\u662f\u7ed9\u9762\u8bd5golang\u540e\u7aef\u5f00\u53d1\u5c97\u7684\u670b\u53cb\u4eec&#xff0c;\u6700\u8be6\u7ec6\u7684golang\u9762\u8bd5\u516b\u80a1\u3002golang\u7684\u516b\u80a1\u4e3b\u8981\u662f\u56f4\u7ed5\u534f\u7a0b&#xff0c;\u5185\u5b58\u6a21\u578b&#xff0c;Channel\u7b49\u5185\u5bb9\u5c55\u5f00\u3002\u8bdd\u4e0d\u591a\u8bf4&#xff0c;\u5f00\u59cb\u4eca\u5929\u7684\u6c89\u6d78\u5f0f\u516b\u80a1\u4e4b\u65c5 \u76ee\u5f55 \u5f15\u8a00 golang\u7684\u534f\u7a0b\u8bf4\u4e00\u4e0b gmp\u5185\u5b58\u6a21\u578b \u4e09\u8272\u6807\u8bb0\u6cd5\u5783\u573e\u56de\u6536 make\u548cnew\u7684\u533a\u522b golang\u7684map\u7ed3\u6784 \u6709\u7f13\u51b2Channel\u548c\u65e0\u7f13\u51b2Channel\u533a\u522b gol","og_url":"https:\/\/www.wsisp.com\/helps\/60485.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-15T10:06:03+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115100602-6968bc0a0786f.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/60485.html","url":"https:\/\/www.wsisp.com\/helps\/60485.html","name":"\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-15T10:06:03+00:00","dateModified":"2026-01-15T10:06:03+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/60485.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/60485.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/60485.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u6700\u8be6\u7ec6\u7684\u540e\u7aef\u9762\u8bd5\u7cfb\u5217\u5185\u5bb9\u2014golang\u516b\u80a1\uff08\u6301\u7eed\u66f4\u65b0\u4e2d\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/60485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=60485"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/60485\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/60484"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=60485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=60485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=60485"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=60485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}