News for hackers. Stuff that matters.
Re:Go is Google's language, not ours by naikrovek on Wednesday May 22, 2019 @06:42AM (#19979735)
A slice isn't an array. A slice is a view into an array.

You don't look through a window in your house into the backyard, and plant a tree in the backyard by fiddling with the window. It's the same with arrays and slices in Go.

If you want to insert an item into a slice, insert it into the array (by copying to a new array and adding your new element to it while copying), then creating a new slice which includes your addition.

edit: (adding for clarity) In a lot of programming languages, whether they use slices or not, arrays are of a fixed size and must be copied to a new array if you want to add elements. Some languages have some syntax that makes it feel like you are modifying an array in-place, while doing the copy to a new array behind the scenes.

edit-edit: for an implementation example of the above, see Java's ArrayList class: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/...

Re:Go is Google's language, not ours by was_boring on Wednesday May 22, 2019 @08:12AM (#19980269)
I'm by no means a expert, but doesn't Go advocate using append with a slice which will create a new array? https://golang.org/pkg/builtin/#append
Re:Go is Google's language, not ours by asark on Wednesday May 22, 2019 @08:52AM (#19980596)
> edit: (adding for clarity) In a lot of programming languages, whether they use slices or not, arrays are of a fixed size and must be copied to a new array if you want to add elements. Some languages have some syntax that makes it feel like you are modifying an array in-place, while doing the copy to a new array behind the scenes.

It's that or some magic with larger-than-needed arrays that automatically grow by a bunch extra every time they hit their boundary to make appends faster, while blowing up memory use and making append performance unpredictable.

Lots of (especially) scripting language hide this behind automagic and you see tons of append-in-a-loop where it's not really necessary, as a result.

[EDIT] had insert two places I intended append. Me need coffee.

Re:Go is Google's language, not ours by naikrovek on Wednesday May 22, 2019 @01:54PM (#19983644)
Yep, you're right, that's the transparently resizeable array thing, and it's exactly how Java's ArrayList class gives the feel of a resizeable array while it actually manages fixed-size backing arrays for you. That's why I linked the source to that class. :)
Re:Go is Google's language, not ours by weberc2 on Wednesday May 22, 2019 @10:47AM (#19981747)
This is absolutely not best practice. It's perfectly idiomatic to insert an item into a slice (without the copy shenanigans you describe). The slice will manage the copy if necessary.
Re:Go is Google's language, not ours by hundt on Thursday May 23, 2019 @10:53AM (#19992290)
That's fine as long as you don't mind if the underlying array is modified. As the parent points out, a slice is a view into an array and there could be other views into the same array.

https://play.golang.org/p/goL1JtapY7q

Re:Go is Google's language, not ours by NateDad on Wednesday May 22, 2019 @04:39PM (#19985212)

    names = append(names, "Bob")
.... that's really it. Will it have the same backing array as it did before you did append? Maybe, maybe not. Should you care? Absolutely not, and if you do, you're probably doing something wrong.
Re:Go is Google's language, not ours by hundt on Thursday May 23, 2019 @10:51AM (#19992277)
Sometimes you need to care!

https://play.golang.org/p/goL1JtapY7q

Re:Go is Google's language, not ours by reflexer on Wednesday May 22, 2019 @04:17PM (#19985017)
I meant that go's append idiom to reallocate array behind the scenes seems to me somewhat complicated and error-prone.