{"id":10006799,"date":"2022-11-04T10:35:10","date_gmt":"2022-11-04T02:35:10","guid":{"rendered":"https:\/\/ityet.com\/?p=10006799"},"modified":"2022-11-04T10:35:10","modified_gmt":"2022-11-04T02:35:10","slug":"go-for-%e5%be%aa%e7%8e%af%e6%9c%89%e6%97%b6%e5%80%99%e7%9c%9f%e7%9a%84%e5%be%88%e5%9d%91%e3%80%82%e3%80%82%e3%80%82","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006799","title":{"rendered":"Go for \u5faa\u73af\u6709\u65f6\u5019\u771f\u7684\u5f88\u5751\u3002\u3002\u3002"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002<\/p>\n<p>\u4e0d\u77e5\u9053\u6709\u591a\u5c11 Go \u7684\u9762\u8bd5\u9898\u548c\u6cc4\u9732\uff0c\u90fd\u548c for \u5faa\u73af\u6709\u5173\u3002\u4eca\u5929\u6211\u5728\u5468\u672b\u8ba4\u771f\u4e00\u770b\uff0c\u53d1\u73b0\u4e86 <a href=\"https:\/\/link.segmentfault.com\/?enc=4FWgMryOvzJRJXctCqCw3A%3D%3D.KcqY4xdDNoQv%2FYA6yBftLuj84cZFqHbpFzfNR0TxFJcK6loND6AOtMqfD8126d7v\" rel=\"nofollow\" title=\"redefining for loop variable semantics\">redefining for loop variable semantics<\/a> \u3002<\/p>\n<p>\u8457\u540d\u7684\u786c\u6838\u5927\u4f6c Russ Cox \u8868\u793a\u4ed6\u4e00\u76f4\u5728\u7814\u7a76\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e76\u8868\u793a\u5341\u5e74\u7684\u7ecf\u9a8c\u8868\u660e\u4e86\u5f53\u524d\u8bed\u4e49\u7684\u4ee3\u4ef7\u662f\u5f88\u5927\u7684\u3002<\/p>\n<h2>\u95ee\u9898<\/h2>\n<h3>\u6848\u4f8b\u4e00\uff1a\u53d6\u5730\u5740\u7b26<\/h3>\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u5199 for \u8bed\u53e5\u65f6\u6709\u65f6\u4f1a\u51fa\u73b0\u8fd0\u884c\u548c\u731c\u60f3\u7684\u7ed3\u679c\u4e0d\u4e00\u81f4\u3002\u4f8b\u5982\u4ee5\u4e0b\u7b2c\u4e00\u4e2a\u6848\u4f8b\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">var all []*Item\nfor _, item := range items {\n    all = append(all, &amp;item)\n}<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6709\u95ee\u9898\u5417\uff1f\u53d8\u91cf all \u5185\u7684 item \u53d8\u91cf\uff0c\u5b58\u50a8\u8fdb\u53bb\u7684\u662f\u4ec0\u4e48\uff1f \u662f\u6bcf\u6b21\u5faa\u73af\u7684 item \u503c\u5417\uff1f<\/p>\n<p>\u5b9e\u9645\u4e0a\u5728 for \u5faa\u73af\u65f6\uff0c\u6bcf\u6b21\u5b58\u5165\u53d8\u91cf all \u7684\u90fd\u662f\u76f8\u540c\u7684 item\uff0c\u4e5f\u5c31\u662f\u6700\u540e\u4e00\u4e2a\u5faa\u73af\u7684 item \u503c\u3002<\/p>\n<p>\u8fd9\u662f Go \u9762\u8bd5\u91cc\u7ecf\u5e38\u51fa\u73b0\u7684\u9898\u76ee\uff0c\u7ed3\u5408 goroutine \u66f4\u98ce\u9a9a\uff0c\u6bd5\u7adf\u8fd8\u4f1a\u5b58\u5728\u4e71\u5e8f\u8f93\u51fa\u7b49\u95ee\u9898\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5c31\u9700\u8981\u628a\u7a0b\u5e8f\u6539\u5199\u6210\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">var all []*Item\nfor _, item := range items {\n    item := item\n    all = append(all, &amp;item)\n}<\/code><\/pre>\n<p>\u8981\u91cd\u65b0\u58f0\u660e\u4e00\u4e2a item \u53d8\u91cf\u628a for \u5faa\u73af\u7684 item \u53d8\u91cf\u7ed9\u5b58\u50a8\u4e0b\u6765\u518d\u8ffd\u52a0\u8fdb\u53bb\u3002<\/p>\n<h3>\u6848\u4f8b\u4e8c\uff1a\u95ed\u5305\u51fd\u6570<\/h3>\n<p>\u63a5\u4e0b\u6765\u662f\u7b2c\u4e8c\u4e2a\u6848\u4f8b\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"go\">var prints []func()\nfor _, v := range []int{1, 2, 3} {\n    prints = append(prints, func() { fmt.Println(v) })\n}\nfor _, print := range prints {\n    print()\n}<\/code><\/pre>\n<p>\u8fd9\u6bb5\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f\u6ca1\u6709 &#038; \u53d6\u5730\u5740\u7b26\uff0c\u662f\u8f93\u51fa 1\uff0c2\uff0c3 \u5417\uff1f<\/p>\n<p>\u8f93\u51fa\u7ed3\u679c\u662f 3\uff0c3\uff0c3\u3002\u8fd9\u53c8\u662f\u4e3a\u4ec0\u4e48\uff1f<\/p>\n<p>\u95ee\u9898\u7684\u91cd\u70b9\u4e4b\u4e00\uff0c\u5173\u6ce8\u5230\u95ed\u5305\u51fd\u6570\uff0c\u5b9e\u9645\u4e0a\u6240\u6709\u95ed\u5305\u90fd\u6253\u5370\u7684\u662f\u76f8\u540c\u7684 v\u3002\u8f93\u51fa 3\uff0c\u662f\u56e0\u4e3a\u5728 for \u5faa\u73af\u7ed3\u675f\u540e\uff0c\u6700\u540e v \u7684\u503c\u88ab\u8bbe\u7f6e\u4e3a\u4e86 3\uff0c\u4ec5\u6b64\u800c\u5df2\u3002<\/p>\n<p>\u5982\u679c\u60f3\u8981\u8fbe\u5230\u9884\u671f\u7684\u6548\u679c\uff0c\u4f9d\u7136\u662f\u4f7f\u7528\u4e07\u80fd\u7684\u518d\u8d4b\u503c\u3002\u6539\u5199\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">for _, v := range []int{1, 2, 3} {\n        v := v\n        prints = append(prints, func() { fmt.Println(v) })\n    }<\/code><\/pre>\n<p>\u589e\u52a0 <code>v := v<\/code> \u8bed\u53e5\uff0c\u7a0b\u5e8f\u8f93\u51fa\u7ed3\u679c\u4e3a 1\uff0c2\uff0c3\u3002<br \/>\u4ed4\u7ec6\u7ffb\u7ffb\u4f60\u5199\u8fc7\u7684 Go \u5de5\u7a0b\uff0c\u662f\u4e0d\u662f\u90fd\u5f88\u719f\u6089\uff1f\u5c31\u8fd9\u6539\u9020\u65b9\u6cd5\uff0c\u8d62\u4e86\u3002<\/p>\n<p>\u5c24\u5176\u662f\u914d\u5408\u4e0a Goroutine \u7684\u5199\u6cd5\uff0c\u5f88\u591a\u540c\u5b66\u4f1a\u66f4\u5bb9\u6613\u5728\u6b64\u7ffb\u8f66\u3002<\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<h3>\u4fee\u590d\u601d\u8def<\/h3>\n<p>\u5b9e\u9645\u4e0a Go \u6838\u5fc3\u56e2\u961f\u5728\u5185\u90e8\u548c\u793e\u533a\u5df2\u7ecf\u8ba8\u8bba\u8fc7\u8bb8\u4e45\uff0c\u5e0c\u671b\u91cd\u65b0\u5b9a\u4e49 for \u5faa\u73af\u7684\u8bed\u6cd5\u3002\u8981\u8fbe\u5230\u7684\u76ee\u7684\u662f\uff1a<strong>\u4f7f\u5faa\u73af\u53d8\u91cf\u6bcf\u6b21\u8fed\u4ee3\u800c\u4e0d\u662f\u6bcf\u6b21\u5faa\u73af<\/strong>\u3002<\/p>\n<p>\u89e3\u51b3\u7684\u529e\u6cd5\u662f\uff1a\u5728\u6bcf\u4e2a\u8fed\u4ee3\u53d8\u91cf x \u7684<strong>\u6bcf\u4e2a\u5faa\u73af\u4f53\u5f00\u5934\uff0c\u52a0\u4e00\u4e2a\u9690\u5f0f\u7684\u518d\u8d4b\u503c<\/strong>\uff0c\u4e5f\u5c31\u662f <code>x := x<\/code>\uff0c\u5c31\u80fd\u591f\u89e3\u51b3\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\u6240\u9690\u542b\u7684\u5751\u3002\u548c\u6211\u4eec\u73b0\u5728\u505a\u7684\u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec\u662f\u81ea\u5df1\u624b\u52a8\u52a0\u7684\uff0cGo \u56e2\u961f\u505a\u7684\u662f\u5e0c\u671b\u5728\u7f16\u8bd1\u5668\u5185\u9690\u5f0f\u5904\u7406\u3002<\/p>\n<h3>\u8ba9\u7528\u6237\u81ea\u5df1\u51b3\u5b9a<\/h3>\n<p>\u6bd4\u8f83\u5c34\u5c2c\u7684\u662f Go \u56e2\u961f\u5728 <a href=\"https:\/\/link.segmentfault.com\/?enc=sS9SlUalqJBkUy4NhnKciQ%3D%3D.%2Flnns6Kx5a2lrzYD4EQ1ozXZ68jJs0FpPE6224kp0VlM7IWvY1C8ksZr0raD7bfhFibgZSK%2BxvxohgdjYXgYErCTtO5EzO9YGpXhpJ5grr%2Fj4TSVJ7JyNU0mFn4dpqpn\" rel=\"nofollow\" title=\"Proposal: Go 2 transition\">Proposal: Go 2 transition<\/a> \u4e2d\u7981\u6b62\u91cd\u65b0\u5b9a\u4e49\u8bed\u8a00\uff0c\u6240\u4ee5 rsc \u4e0d\u80fd\u76f4\u63a5\u8fd9\u4e48\u5e72\u3002<\/p>\n<p>\u56e0\u6b64\u5c06\u4f1a\u7531\u7528\u6237\u81ea\u5df1\u51b3\u5b9a\u63a7\u5236\u8fd9\u4e2a \u201c\u7834\u574f\u201d\uff0c\u65b9\u5f0f\u5c06\u4f1a\u662f\u6839\u636e\u6bcf\u4e2a\u5305\u7684 go.mod \u6587\u4ef6\u4e2d\u7684 go \u884c\u66f4\u6539\u8bed\u4e49\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u662f\u5728 Go1.30 \u5bf9\u672c\u6587\u8ba8\u8bba\u7684 for \u5faa\u73af\u6539\u4e3a\u8fed\u4ee3\uff0c\u90a3\u4e48\u5728 go.mod \u6587\u4ef6\u4e2d\u7684 go \u7248\u672c\u58f0\u660e\u662f\u5c06\u662f\u4e00\u4e2a\u5173\u952e\u3002<\/p>\n<p>\u5982\u4e0b\u56fe\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042730006\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Go 1.30 \u6216\u66f4\u9ad8\u7248\u672c\u5c06\u4f1a\u6bcf\u6b21\u8fed\u4ee3\u53d8\u91cf\uff0c\u800c\u65e9\u671f Go \u7248\u672c\u7684\u5c06\u6bcf\u6b21\u5faa\u73af\u53d8\u91cf\u3002<\/p>\n<p>\u5982\u6b64\u4e00\u6765\u4e0a\u8ff0\u63d0\u5230\u7684 for \u5faa\u73af\u95ee\u9898\u90fd\u4f1a\u5728\u4e00\u5b9a\u8303\u56f4\u5185\u88ab\u89e3\u51b3\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>for \u5faa\u73af\u65f6\u7684\u53d8\u91cf\u95ee\u9898\uff0c\u4e00\u76f4\u662f\u5404\u5927 Go \u8003\u5b98\u7231\u8003\u7684\u9898\u76ee\uff0c\u53e6\u5916\u4e5f\u786e\u5b9e\u5728\u5b9e\u9645\u7f16\u7a0b Go \u4ee3\u7801\u65f6\u4f1a\u9047\u5230\u8fd9\u7c7b\u5751\u3002<\/p>\n<p>\u867d\u7136 rsc \u5e0c\u671b\u5728 go.mod \u6587\u4ef6\u4e0a\u5f00\u521b\u5148\u6cb3\uff0c\u5229\u7528 go \u7248\u672c\u7684\u58f0\u660e\uff0c\u53bb\u4fee\u6539\u8bed\u4e49\uff08\u4e0d\u5141\u8bb8\u6dfb\u52a0\u548c\u5220\u9664\uff09\u3002\u8fd9\u65e0\u7591\u662f\u7ed9 Go1 \u517c\u5bb9\u6027\u4fdd\u969c\u5f00\u4e86\u4e00\u4e2a\u540e\u95e8\u3002<\/p>\n<p>\u5982\u679c\u5b9e\u65bd\uff0c\u672c\u6b21\u53d8\u66f4\u4f1a\u5bfc\u81f4 Go \u7684\u524d\u540e\u7248\u672c\u8bed\u4e49\u6709\u6240\u4e0d\u540c\u3002\u8fd8\u4e0d\u5982\u53d8\u6210\u4e00\u4e2a go.mod \u6587\u4ef6\u7684\u4e00\u4e2a\u8bed\u4e49\u5f00\u5173\u3002<\/p>\n<p>\u8fd9\u663e\u7136\u662f\u4e00\u4e2a\u5f88\u6298\u817e\u7684\u601d\u8003\u9898\u3002<\/p>\n<blockquote><p>\u6587\u7ae0\u6301\u7eed\u66f4\u65b0\uff0c\u53ef\u4ee5\u5fae\u4fe1\u641c\u3010\u8111\u5b50\u8fdb\u714e\u9c7c\u4e86\u3011\u9605\u8bfb\uff0c\u672c\u6587 <strong>GitHub<\/strong> <a href=\"https:\/\/link.segmentfault.com\/?enc=li386Rp4etzUyKON5ZXEtA%3D%3D.dRyNp7xPGr08K3rfGbCTfkEu52S0MbdRtXF%2FEnyNBJo%3D\" rel=\"nofollow\">github.com\/eddycjy\/blog<\/a> \u5df2\u6536\u5f55\uff0c\u5b66\u4e60 Go \u8bed\u8a00\u53ef\u4ee5\u770b <a href=\"https:\/\/link.segmentfault.com\/?enc=mrpQuZUof6JTS9Z27Ai9iQ%3D%3D.E4D8C2eRprB9aIqgQih%2Flx1PpRAP%2Bc0AMYeYN90GAqa%2BbDbrji3Ngs%2BDzIYuwHtb\" rel=\"nofollow\">Go \u5b66\u4e60\u5730\u56fe\u548c\u8def\u7ebf<\/a>\uff0c\u6b22\u8fce Star \u50ac\u66f4\u3002<\/p><\/blockquote>\n<h2>\u63a8\u8350\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=ZS%2BY64oRXJi7CCrqSp%2FTkw%3D%3D.cmUJLBBg4flfoyq4WniBiTOeZ39m6xjN13HXn7Fp9iRi%2FVjV7DcBXtyKPrc6k%2BK95RirKeeDwKARigyAQO5xnw%3D%3D\" rel=\"nofollow\">Go \u53ea\u4f1a if err != nil\uff1f\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u5206\u4eab\u8fd9\u4e9b\u4f18\u96c5\u7684\u5904\u7406\u59ff\u52bf\u7ed9\u4f60\uff01<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=UXzIB1oJ0QoXmTtOl01Qmg%3D%3D.Pg7xfVswtDpa9N7nRihMb4D6JT47oNEW4QrT9%2FLdLkUqOzBpzSvdTMz7bEBOsZHZ5UAjnp73uj1UpEDnZ1MEmA%3D%3D\" rel=\"nofollow\">Go \u9519\u8bef\u5904\u7406\u65b0\u601d\u8def\uff1f\u7528\u5de6\u4fa7\u51fd\u6570\u548c\u8868\u8fbe\u5f0f<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=rsoJMYF10OQol%2BGKiNX57g%3D%3D.Vz9hD%2Bcs4zCGTjxlSkIzvn7wQiMZpQidwVmTjPgOD9NJTL%2FMogZlZEIge9m4BVB3ahMgiZAeMMaOQPsmFjubZw%3D%3D\" rel=\"nofollow\">\u5148\u7779\u4e3a\u5feb\uff0cGo2 Error \u7684\u6323\u624e\u4e4b\u8def<\/a><\/li>\n<\/ul>\n<h3>Go \u56fe\u4e66\u7cfb\u5217<\/h3>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=3Sm%2BONpsg8nz9QZ%2ByeAlhg%3D%3D.xJLTiUgXh71SzySUBdrWGPU7RJddd7Q1HaiXMjyQw6FsA9VcO1%2FZm6gJ3veLyMyf\" rel=\"nofollow\">Go \u8bed\u8a00\u5165\u95e8\u7cfb\u5217\uff1a\u521d\u63a2 Go \u9879\u76ee\u5b9e\u6218<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=gloNSghDrCMczIm8nhId2w%3D%3D.CjVVhk2bsqXUMwISEnWYuRI5UfCRJ%2Br5NcIB4OOonnQ%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u7f16\u7a0b\u4e4b\u65c5\uff1a\u6df1\u5165\u7528 Go \u505a\u9879\u76ee<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=5f%2FqRhwhlDoQcO4tmvL1nw%3D%3D.%2BCdIqtxXhoyQQ4tJ8AT6UNrGJHHjVNDgsxeeBoOmA8w%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8bbe\u8ba1\u54f2\u5b66\uff1a\u4e86\u89e3 Go \u7684\u4e3a\u4ec0\u4e48\u548c\u8bbe\u8ba1\u601d\u8003<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=P7P1GBO2XP1Q2HUGRpo0RQ%3D%3D.gEFNIu56x5B9wplfWPioU9FlXwNppAWBx1Jcjof2W%2F8%3D\" rel=\"nofollow\">Go \u8bed\u8a00\u8fdb\u9636\u4e4b\u65c5\uff1a\u8fdb\u4e00\u6b65\u6df1\u5165 Go \u6e90\u7801<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042730004\">https:\/\/segmentfault.com\/a\/1190000042730004<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u714e\u9c7c\u3002 \u4e0d\u77e5\u9053\u6709\u591a\u5c11 Go \u7684\u9762\u8bd5\u9898\u548c\u6cc4\u9732\uff0c\u90fd\u548c for \u5faa\u73af\u6709\u5173\u3002\u4eca\u5929\u6211\u5728\u5468\u672b\u8ba4\u771f\u4e00\u770b\uff0c\u53d1\u73b0\u4e86  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006799","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006799"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006799\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}