{"id":1010,"date":"2025-01-16T00:52:49","date_gmt":"2025-01-15T16:52:49","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1010"},"modified":"2025-01-16T00:52:49","modified_gmt":"2025-01-15T16:52:49","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9c%a8-go-%e4%b8%ad-for-%e5%be%aa%e7%8e%af%e5%88%9d%e5%a7%8b%e5%8c%96%e5%99%a8%e4%b8%ad%e4%bd%bf%e7%94%a8%e7%bb%93%e6%9e%84%e4%bd%93%e4%bc%9a%e5%af%bc%e8%87%b4","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/16\/%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9c%a8-go-%e4%b8%ad-for-%e5%be%aa%e7%8e%af%e5%88%9d%e5%a7%8b%e5%8c%96%e5%99%a8%e4%b8%ad%e4%bd%bf%e7%94%a8%e7%bb%93%e6%9e%84%e4%bd%93%e4%bc%9a%e5%af%bc%e8%87%b4\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u5728 Go \u4e2d\u00a0for\u00a0\u5faa\u73af\u521d\u59cb\u5316\u5668\u4e2d\u4f7f\u7528\u7ed3\u6784\u4f53\u4f1a\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\uff1f"},"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\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u65f6\uff0c\u521d\u59cb\u5316\u5668\u90e8\u5206\u901a\u5e38\u7528\u4e8e\u58f0\u660e\u548c\u521d\u59cb\u5316\u53d8\u91cf\u3002Go \u8bed\u8a00\u4e2d\u7684 <code>for<\/code> \u5faa\u73af\u8bed\u6cd5\u4e0e\u5176\u4ed6\u8bed\u8a00\u76f8\u6bd4\u6709\u4e00\u4e9b\u72ec\u7279\u4e4b\u5904\uff0c\u5176\u4e2d\u521d\u59cb\u5316\u5668\u90e8\u5206\u5e94\u8be5\u662f\u6709\u6548\u7684\u8bed\u53e5\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u5728 <code>for<\/code> \u5faa\u73af\u7684\u521d\u59cb\u5316\u5668\u4e2d\u4f7f\u7528\u7ed3\u6784\u4f53\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8bed\u6cd5\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a Go \u8bed\u8a00\u7684\u8bed\u6cd5\u89c4\u5219\u4e0d\u5141\u8bb8\u7ed3\u6784\u4f53\u7684\u76f4\u63a5\u5b9e\u4f8b\u5316\u51fa\u73b0\u5728 <code>for<\/code> \u5faa\u73af\u7684\u521d\u59cb\u5316\u5668\u90e8\u5206\uff0c\u9664\u975e\u5b83\u662f\u4e00\u4e2a\u6709\u6548\u7684\u58f0\u660e\u6216\u8d4b\u503c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Go \u4e2d <code>for<\/code> \u5faa\u73af\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u4e2d\u7684 <code>for<\/code> \u5faa\u73af\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for initialization; condition; post {\n    \/\/ loop body\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u521d\u59cb\u5316\u5668<\/strong>\uff1a\u5728\u5faa\u73af\u5f00\u59cb\u524d\u6267\u884c\u7684\u8bed\u53e5\uff0c\u901a\u5e38\u7528\u4e8e\u58f0\u660e\u548c\u521d\u59cb\u5316\u5faa\u73af\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u6761\u4ef6<\/strong>\uff1a\u6bcf\u6b21\u5faa\u73af\u8fed\u4ee3\u524d\u8bc4\u4f30\u3002\u5982\u679c\u6761\u4ef6\u4e3a <code>true<\/code>\uff0c\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u9000\u51fa\u5faa\u73af\u3002<\/li>\n\n\n\n<li><strong>\u540e\u7f6e\u8bed\u53e5<\/strong>\uff1a\u6bcf\u6b21\u5faa\u73af\u8fed\u4ee3\u540e\u6267\u884c\u7684\u8bed\u53e5\uff0c\u901a\u5e38\u7528\u4e8e\u66f4\u65b0\u5faa\u73af\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u7ed3\u6784\u4f53\u5b9e\u4f8b\u5316\u4e0e\u521d\u59cb\u5316\u5668<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u5728 <code>for<\/code> \u5faa\u73af\u7684\u521d\u59cb\u5316\u5668\u4e2d\u4f7f\u7528\u7ed3\u6784\u4f53\u65f6\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5c06\u7ed3\u6784\u4f53\u5b9e\u4f8b\u5316\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u5b83\u5c06\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9519\u8bef\u793a\u4f8b\uff1a<\/h4>\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 main() {\n    for Person{\"Alice\", 30}; ; {\n        fmt.Println(\"Looping\")\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002\u9519\u8bef\u539f\u56e0\u5728\u4e8e\uff0c<code>Person{\"Alice\", 30}<\/code> \u4f5c\u4e3a\u4e00\u4e2a\u7ed3\u6784\u4f53\u5b9e\u4f8b\u5316\u8bed\u53e5\u5e76\u6ca1\u6709\u8d4b\u503c\u7ed9\u4efb\u4f55\u53d8\u91cf\uff0c\u4e14\u5b83\u4e0d\u80fd\u4f5c\u4e3a\u6709\u6548\u7684\u58f0\u660e\u8bed\u53e5\u51fa\u73b0\u5728\u521d\u59cb\u5316\u5668\u90e8\u5206\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u6b63\u786e\u7684\u65b9\u5f0f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u5728 <code>for<\/code> \u5faa\u73af\u7684\u521d\u59cb\u5316\u5668\u4e2d\u4f7f\u7528\u7ed3\u6784\u4f53\uff0c\u4f60\u9700\u8981\u5c06\u7ed3\u6784\u4f53\u5b9e\u4f8b\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u521d\u59cb\u5316\u5668\u90e8\u5206\u662f\u4e00\u4e2a\u6709\u6548\u7684\u8bed\u53e5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u786e\u793a\u4f8b\uff1a<\/h4>\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 main() {\n    p := Person{\"Alice\", 30}  \/\/ \u6b63\u786e\u7684\u7ed3\u6784\u4f53\u521d\u59cb\u5316\u548c\u8d4b\u503c\n    for p.Name != \"Bob\"; p.Name = \"Bob\" {\n        fmt.Println(\"Looping\")\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>p := Person{\"Alice\", 30}<\/code> \u6b63\u786e\u5730\u5c06\u7ed3\u6784\u4f53\u5b9e\u4f8b\u8d4b\u503c\u7ed9\u4e86\u53d8\u91cf <code>p<\/code>\uff0c\u5e76\u4e14\u8fd9\u4e2a\u521d\u59cb\u5316\u8bed\u53e5\u662f\u6709\u6548\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4e3a\u4ec0\u4e48\u4f1a\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u8981\u6c42\u5728 <code>for<\/code> \u5faa\u73af\u7684\u521d\u59cb\u5316\u5668\u90e8\u5206\u4f7f\u7528\u6709\u6548\u7684\u58f0\u660e\u6216\u8d4b\u503c\u8bed\u53e5\u3002\u76f4\u63a5\u5b9e\u4f8b\u5316\u7ed3\u6784\u4f53\u5e76\u6ca1\u6709\u5c06\u5b83\u8d4b\u503c\u7ed9\u4efb\u4f55\u53d8\u91cf\uff0c\u6240\u4ee5\u8fd9\u4e0d\u7b26\u5408 Go \u7684\u8bed\u6cd5\u89c4\u5219\u3002<\/p>\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<p class=\"wp-block-paragraph\">\u5728 Go \u4e2d\uff0c<code>for<\/code> \u5faa\u73af\u7684\u521d\u59cb\u5316\u5668\u90e8\u5206\u5e94\u5305\u542b\u6709\u6548\u7684\u8bed\u53e5\uff0c\u5982\u53d8\u91cf\u58f0\u660e\u3001\u8d4b\u503c\u6216\u8868\u8fbe\u5f0f\u3002\u5982\u679c\u4f60\u5728\u5176\u4e2d\u4f7f\u7528\u7ed3\u6784\u4f53\u5b9e\u4f8b\u5316\uff0c\u5fc5\u987b\u5c06\u5176\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u4e2d\uff0c\u4f7f\u7528 for \u5faa\u73af\u65f6\uff0c\u521d\u59cb\u5316\u5668\u90e8\u5206\u901a\u5e38\u7528\u4e8e\u58f0\u660e\u548c\u521d\u59cb\u5316\u53d8\u91cf\u3002Go \u8bed\u8a00\u4e2d\u7684 for \u5faa\u73af\u8bed\u6cd5\u4e0e\u5176 [&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-1010","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1010","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=1010"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1011,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1010\/revisions\/1011"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}