For loop structure

polaris · · 371 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey guys,</p> <p>I was going through <a href="https://github.com/golang/tour/blob/master/solutions/loops.go" rel="nofollow">this solution</a> from the Go Tour and saw this snippet of code:</p> <pre><code>for math.Abs(n-z) &gt; delta { n, z = z, z-(z*z-x)/(2*z) } </code></pre> <p>for loops are interesting in Go, and I&#39;ve seen a few different ways to construct them, but I don&#39;t see anywhere in the docs what it means to have parts of the body separated by commas like that.</p> <p>I tried stuff like the below just to replicate the previous structure:</p> <pre><code>package main import &#34;fmt&#34; func main() { x := 20 for i := 0; i &lt; 10; i++ { fmt.Println(i), fmt.Println(x) } } </code></pre> <p>but end up getting:</p> <pre><code>.\reddit.go:8:33: syntax error: unexpected newline, expecting := or = or comma </code></pre> <p>Can someone give me a simple exam using the structure above?</p> <hr/>**评论:**<br/><br/>nagai: <pre><p>It&#39;s just two assignments on one line, equivalent to</p> <p><code>n = z z = z-(z*z-x)/(2*z) </code></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

371 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传