TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

a[low:high:max] in Golang – A Rare Slice Trick

130 点作者 weird_user大约 2 年前

8 条评论

gus_massa大约 2 年前
This paragraph is scary. It looks like a good idea for the Golang version of the Underhanded C Contest.<p>&gt; <i>This trick is useful for returning a slice from an immutable array; if you accidentally append to the supposedly immutable slice, a copy is forced and no data is overwritten because there is no more capacity left.</i>
评论 #35211184 未加载
H4ZB7大约 2 年前
why do people write articles about go features? when PHP was in its prime, almost nobody wrote blogs explaining how they found some philosophy in PHP. there&#x27;s a reason for that.<p>when you see someone open their article explaining a language feature by talking of the implementation details or specific use cases, that&#x27;s a language smell (of course all industrial PLs stink).<p>ironically go is the only post 80s language that uses &quot;memory safe&quot; as a marketing point (even though they all are), yet go has the most memory unsafety of post 80s industrial languages. you can parse something and pass on a slice somewhere. if you mistakenly slice that slice with a bigger size - this incorrect size being the programmer bounds check error - you restore some of the original array that was supposed to be cut off and teh next operation working on that slice will thus modify or leak data:<p><pre><code> package main import &quot;fmt&quot; func main(){ a := [3]int{1,2,3} b := a[0:2] fmt.Println(b[1]) c := b[0:3] fmt.Println(c[2]) } $ go run a.go 2 3 </code></pre> the other example of memory unsafety in go being that modifying slices between threads can lead to actual memory corruption, not just simulated memory corruption as above<p>the point here is that this footgun doesnt even have a real point outside of some insane performance argument. nobody would ever design something like this without massive cognitive dissonance (aside from industrial PLs, which just copy and modify the previous industrial PL, C in this case). all go&#x27;s primitives are rigged like this with unintuitive behaviors. its amazing how much such a simple language with small scope can get wrong. and i expect nothing less from people who go around saying &quot;zeroeth&quot;. DAY OF THE BOBCAT SOON
评论 #35216144 未加载
评论 #35215281 未加载
评论 #35215790 未加载
评论 #35215874 未加载
评论 #35218187 未加载
jjice大约 2 年前
Tangential, but if OP is the owner of the site, could you talk a little bit about your book writing process?<p>- How you write<p>- How you render the PDF<p>- How you develop your plans<p>That kind of thing. I find it super interesting to self-publish software books and I&#x27;ve been slowly writing one for about a year now. Really curious about this stuff in general and it looks like you&#x27;ve got a solid process down.
评论 #35216853 未加载
nutate大约 2 年前
My fave golang slice trick is the len of an empty slice is 0, but the slice itself is == to nil, but the len of nil won&#x27;t compile. Can&#x27;t understand that one.<p><a href="https:&#x2F;&#x2F;go.dev&#x2F;play&#x2F;p&#x2F;MslCkBphl7q?v=gotip" rel="nofollow">https:&#x2F;&#x2F;go.dev&#x2F;play&#x2F;p&#x2F;MslCkBphl7q?v=gotip</a>
评论 #35212825 未加载
评论 #35212639 未加载
评论 #35214458 未加载
评论 #35223012 未加载
tommodev大约 2 年前
Question: what is the reason for the silent copy when append exceeds the original slice cap?<p>It&#x27;s a footgun avoided by reading the spec and (maybe) remembering it in practice, but it feels like it would be safer to throw a comp error and force the user to deal with it when a user is trying to exceed the cap of the underlying array?<p>Alternative is defensively using len() and cap() for slice ops in which case error-ing out feels more ergonomic.
评论 #35214177 未加载
评论 #35216512 未加载
评论 #35214090 未加载
yarg大约 2 年前
This to me is a glaring case of a poorly named variable.<p>a[low:high:capacity] would be much easier to understand at first sight.
评论 #35216488 未加载
sacnoradhq大约 2 年前
0. Only 2 of 3 values are necessary.<p>1. Can 1 value be unspecified?<p>2. Can 2 values be unspecified?<p>3. Can 3 values be unspecified?<p>4. Are conflicting values interpreted as intersection of ranges rather than union?<p>Note 1-3. With 2 values, I believe x[:] is how to lift a sized array into a more generic slice type.
评论 #35213293 未加载
评论 #35212847 未加载
operator-name大约 2 年前
So this is the same as<p><pre><code> a[low:min(high, max)] ?</code></pre>
评论 #35214122 未加载