<story><title>a[low:high:max] in Golang – A Rare Slice Trick</title><url>https://build-your-own.org/blog/20230316_go_full_slice/</url></story><parent_chain><item><author>H4ZB7</author><text>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&amp;#x27;s a reason for that.&lt;p&gt;when you see someone open their article explaining a language feature by talking of the implementation details or specific use cases, that&amp;#x27;s a language smell (of course all industrial PLs stink).&lt;p&gt;ironically go is the only post 80s language that uses &amp;quot;memory safe&amp;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:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    package main
    import &amp;quot;fmt&amp;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
&lt;/code&gt;&lt;/pre&gt;
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&lt;p&gt;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&amp;#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 &amp;quot;zeroeth&amp;quot;. DAY OF THE BOBCAT SOON</text></item></parent_chain><comment><author>beltsazar</author><text>Yeah, the most surprising thing about Go&amp;#x27;s slice expressions is that you can reslice a slice beyond its length, as long as it&amp;#x27;s still within its capacity.&lt;p&gt;I wonder how many off-by-one bugs have happened undetected because a slice is unintentionally resliced beyond its length. Instead of crashing, so that the issue is known early, the program will still run with inconsistent data.</text></comment>
<story><title>a[low:high:max] in Golang – A Rare Slice Trick</title><url>https://build-your-own.org/blog/20230316_go_full_slice/</url></story><parent_chain><item><author>H4ZB7</author><text>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&amp;#x27;s a reason for that.&lt;p&gt;when you see someone open their article explaining a language feature by talking of the implementation details or specific use cases, that&amp;#x27;s a language smell (of course all industrial PLs stink).&lt;p&gt;ironically go is the only post 80s language that uses &amp;quot;memory safe&amp;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:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    package main
    import &amp;quot;fmt&amp;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
&lt;/code&gt;&lt;/pre&gt;
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&lt;p&gt;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&amp;#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 &amp;quot;zeroeth&amp;quot;. DAY OF THE BOBCAT SOON</text></item></parent_chain><comment><author>silisili</author><text>I&amp;#x27;m not sure how your snippet above exemplifies memory unsafety.&lt;p&gt;Concurrent access does let you hit some &amp;#x27;fun&amp;#x27; behavior, but you have to be doing pretty dumb things to hit them.  And while the implementation may be able to save you from something like that, such things would likely bubble up elsewhere(disk i&amp;#x2F;o, network i&amp;#x2F;o, etc) if doing that kind of thing.</text></comment>