6 ms·
wrt a "proper" way: adding item to a slice, uhmm?
by reflexer 7y ago
wrt a "proper" way: adding item to a slice, uhmm?
- naikrovek 7y agoA 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/classes/java/util/ArrayList.java http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/...
- was_boring 7y agoI'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 https://golang.org/pkg/builtin/#append
- asark 7y ago> 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.
- naikrovek 7y agoYep, 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. :)
- weberc2 7y agoThis 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.
- hundt 7y agoThat'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 https://play.golang.org/p/goL1JtapY7q
- reflexer 7y agoI meant that go's append idiom to reallocate array behind the scenes seems to me somewhat complicated and error-prone.
- NateDad 7y agonames = 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.
- hundt 7y agoSometimes you need to care! https://play.golang.org/p/goL1JtapY7q https://play.golang.org/p/goL1JtapY7q
- loosescrews 7y agoappend?