Hey Techie! 🌸
Welcome to my Go series! I'll be sharing what I'm learning in ways that make sense to me, the mistakes I make and the "aha!" moments that help everything click. Whether you're learning Go too or just curious about it, I hope you'll pick up something along the way.
Feel free to add any insights or experiences in the comments.
Today's topic is... drumroll, please! Slices. Let's dive in!
So, what exactly is a slice?
When I first came across slices in Go, I thought they were another name for arrays. Turns out, they're not!
A slice is a dynamic view into an underlying array. It is internally represented by a small data structure called a slice header. Instead of storing the elements themselves, the slice header stores a pointer to the underlying array, along with its length and capacity. This realization helped me understand why modifying a slice can also modify the original array.
Another interesting and convenient thing is how flexible slices are compared to arrays which are fixed size. Slices can grow using functions like append() or be resliced to work with a smaller portion of the underlying array. This flexibility is one of the reasons slices are used so frequently in Go.
A little heads up, I used AI to clean up my grammar and typos. The technical ideas and thoughts in this article are 100% my own.
Top comments (10)
Good work Bernadette!
Thank you 🌸
I appreciated the clarification on the internal representation of slices in Go, specifically the concept of a slice header storing a pointer to the underlying array, along with its length and capacity. This explanation helped solidify my understanding of why modifying a slice can affect the original array, which I've encountered in my own projects when using functions like
append()to dynamically resize slices. One aspect I'd like to explore further is how this implementation affects performance, particularly in scenarios where slices are repeatedly appended or resliced - do you think the flexibility of slices comes with a significant performance trade-off in certain cases?Thank you so much! I'm really glad the explanation helped.
From what I understand so far, slices are designed to be efficient for most use cases, but repeated
append()operations can cause Go to allocate a new underlying array once the slice runs out of capacity. When that happens, the existing elements are copied over to the new array, which does have a performance cost.I haven't explored performance in depth yet, but you've definitely given me an idea for a future article.
That is exactly how a dynamic array is done in all langs which support it.
You'll eventually run out of space, you allocate new space, you copy over.
There's no deeper insight into it.
How big should the new one be? Well... there's no science behind it really. It is rule of thumb, and depends on the language.
Why Go calls it slice? Beats me. It is - I think - the creators of Go trying to be smart and funny.
I mean there are certain real troublesome aspects of it, when you open up the specs, but... in general it is like dynamic array, just with hard to catch bugs due to overengineering.
More interesting question though:
Let's say you have to solve AoC 2022 Day 05...
How would you do it?
Wold you use slices?
Or... would you do something much more crazier?
Would you be bold, and go beyond a naive solution?
Yup. I think you can solve it in a non-naive way.
I just want to let you know that, you can ask clarifying questions etc. It is not an exam, but a game.
Yaddi daddi dada. Solve the puzzle then we can talk.
Hey, this article appears to have been generated with the assistance of ChatGPT or possibly some other AI tool.
We allow our community members to use AI assistance when writing articles as long as they abide by our guidelines. Please review the guidelines and edit your post to add a disclaimer.
Failure to follow these guidelines could result in DEV admin lowering the score of your post, making it less visible to the rest of the community. Or, if upon review we find this post to be particularly harmful, we may decide to unpublish it completely.
We hope you understand and take care to follow our guidelines going forward!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.