<p>Needed a function to chunk a []int into a [][]int with a predefined chunk size. </p>
<p>The following code is what I came up with <a href="https://gist.github.com/aranw/5578b7fbdd6d12c7add9">https://gist.github.com/aranw/5578b7fbdd6d12c7add9</a></p>
<p>It has the following output </p>
<p>go run main.go</p>
<p>2015/06/19 23:34:19 [1 2 3 4 5]</p>
<p>2015/06/19 23:34:19 [[1 2 3 4] [5]]</p>
<p>Any suggestions on improvements, issues I should be aware of or should I do something differently? I've come from a mainly PHP and Javascript background but wanting to improvement my abilities with Go and use it more </p>
<hr/>**评论:**<br/><br/>Fwippy: <pre><p>Here's your code at a play.golang link: <a href="https://play.golang.org/p/wXWIerOIg-" rel="nofollow">https://play.golang.org/p/wXWIerOIg-</a> (It's a little easier to share Go code this way, as it allows people to run it and see how it works in the browser).</p>
<p>I generally prefer putting putting more work into the <code>for</code> statement, so I might do something like this: <a href="https://play.golang.org/p/CSBz_1jGP-" rel="nofollow">https://play.golang.org/p/CSBz_1jGP-</a> </p>
<p>Also, I should probably point out that the new slices simply point at the underlying array for the input slice. This is cool for performance reasons, but also means the input & output are pointing to the same data, and a change to one will affect the other. Depending on your needs, this might be bad, irrelevant, or desired behavior. If it's undesired, you can use the <code>copy()</code> built-in.</p>
<p>Overall, pretty good and not very different from how I'd write it. :)</p></pre>aranw: <pre><p>Ah I like the for loop in this manner more it makes the code much cleaner to understand thanks! </p>
<p>As for the <code>copy</code> I thought this could perhaps be done via a boolean flag passed into the function?</p></pre>aranw: <pre><p>This is the final code I came up with <a href="https://play.golang.org/p/seegaw0HDl" rel="nofollow">https://play.golang.org/p/seegaw0HDl</a> </p></pre>calebdoxsey: <pre><p>You can precompute the size of <code>chunks</code>.</p>
<p>Here's another way using a more standard for loop: <a href="https://play.golang.org/p/kaOA_y9Agz" rel="nofollow">https://play.golang.org/p/kaOA_y9Agz</a>. </p>
<p>The advantage of a standard for loop is its more obviously correct. There's nothing wrong with the code you wrote, but I had to read it carefully to make sure there wasn't an infinite loop in there.</p></pre>aranw: <pre><p>thanks, originally I was precomputing the size of <code>chunks</code> but I removed it as I didn't see a benefit code wise. But I guess there is a benefit in performance?</p></pre>calebdoxsey: <pre><p>Yes. <code>append</code> grows a slice by creating a new larger array and copying everything over. It does this by doubling the capacity, so it will only happen a few times, but in general it's a good idea to set the initial size if you can.</p>
<p>An alternative approach is to set the capacity of the slice instead of its length (<code>make([][]int, 0, n)</code>). This will yield the same performance but you could still use append.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传