Wednesday, November 30, 2022
HomeGolangNoob is questioning why slices must exist - Getting Assist

Noob is questioning why slices must exist – Getting Assist


as a noob to golang, i’m clueless as to why slices must exist. I’ve used arrays and lists all my coding life and by no means felt the urge to make something resembling a slice to interface with my arrays.
I can’t discover a phrase on the web about why go-slices must exist.
Can anybody level me on the definitive and easiest base case instance that cleanly reveals slices being higher in some helpful manner over straight array / listing use.
thanks

Slices are of variable dimension, arrays are fastened dimension.

Slices have a O(1) random entry, Lists have O(n).

An array with with n parts makes use of up n instances the info dimension of reminiscence. A slice the identical + a pointer for the beginning and the present dimension. A Record: one pointer per component!

Thats why every of it does exist, and every of it’s helpful in some, and never so helpful in different conditions.

1 Like

>Slices are of variable dimension, arrays are fastened dimension.
I already know. Nonetheless clueless why slices are higher than array references / pointers.

>Slices have a O(1) random entry, Lists have O(n).
I don’t consider it. Why doesn’t each language dev on the earth for each language ever invented, significantly DB languages, patch their language to work with slices, in order that random entry could be O(1) ?

>An array with with n parts makes use of up n instances the info dimension of reminiscence. A slice the identical + a pointer for the beginning and the present dimension. A Record: one pointer per component!
I’m conscious golang copies arrays to features as a substitute of references them. However then why not simply go a pointer to the array or an array component?

I repeat, since golang’s creation in 2009, no-one has been capable of show the advantage of slices over arrays / lists / pointers. If they’d, the demo code would have been fanfared for years everywhere in the web: “take a look at how a lot reminiscence / time / linesOfCode we’ve saved by utilizing slices as a substitute of arrays/lists/pointers!”

And the silence on this, a well-liked golang discussion board, confirms this.

The one wise conclusion is Ken Thompson was simply throwing another manner of dealing with arrays -slices- over his shoulder, not understanding if it will be helpful, however questioning if some devs someplace might discover a option to make slices higher than arrays/lists/pointers.

I think about coping with slices in go or vec in rust rather more ergonomic than coping with arrays straight in C, the place I all the time should do a whole lot of handbook e-book preserving after I need them dynamic, and subsequently mainly re-implement what’s already generally known as slice on go or vec in rust.

I do know that rusts vec or gos slices are applied in a manor that’s environment friendly sufficient for the commonest usecase.

I wouldn’t belief in my very own implementation, and possibly would use a library due to that then, which might make it simpler to cope with it, however I’ve to belief a 3rd celebration then.

I favor if I can simply belief the stdlib/runtime for this type of issues.

1 Like

I’d not say that it’s a query of which one is healthier. As a consequence of their completely different properties (arrays = fastened size & pass-by-value, slices = variable size, mapped onto arrays, almost-pass-by-reference), they merely have completely different use instances.

Slices could be seen as a comfort layer on high of arrays, whereas arrays could be helpful for preserving management over reminiscence format and heap allocations.

Here’s a fairly helpful distinction: In a 2-dimensional slice, every internal slice can have a distinct size.

1 Like

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments