▲ 12 ▼ How to use slice capacity and length in go
This blog post discusses a common bug stemming from developers not understanding append and make, and then goes into the difference between slices and arrays so that it is easier to understand what capacity and length are and how they are used when making slices.
Register to comment or vote on this story
Author here - happy to answer any questions or fix anything that seems inaccurate/misleading. I believe I have a majority of the issues cleaned up, but the goal of this post was to help explain the difference between length and capacity and how they came to be in Go since I see a lot of beginners not really understanding make and append until it bites them.
Thanks for the article, the first question tripped me up for a minute, and is a good illustration of the difference between arrays and slices. Another useful article from the go blog on slices is this one: https://blog.golang.org/slices
I'm curious, what are the other mistakes you see beginners make frequently when learning Go?
Sorry for the delay - distractions galore today. Also, thanks for the link - I updated the post to include it in the summary.
A few other mistakes that come to mind:
1. Doing `var m map[string]bool` followed by `m["something"] = true` and then you get `panic: assignment to entry in nil map`. I intend to write something short about this soon-ish. I think what also complicates this is that you can do `var a []int` and it works because you tend to use `append`, but with maps you don't have an equivalent function to add a key and it can catch people off guard. A good friend of mine was just telling me about how this bit him a few times and I've heard that story many times before.
2. Happens a little later for most devs, but nearly everyone runs into errors where they create a pointer variable, assign it to nil, then return it as an interface and the code that receives it doesn't evaluate it as nil. eg: https://play.golang.org/p/uA87dTyR_E This is commonly talked about with regards to custom errors, but it happens in other cases. I think what makes this really confusing is that literally one thing changes in your code (the return type) yet its impact on other code is much larger than expected, as if statements suddenly start evaluating to false.
3. Structuring applications - Ben Johnson does a great job talking about this, but people coming from other languages tend to still struggle with this and end up making mistakes that lead to cyclical imports or poor structure and then they seek guidance on how to better structure things. I don't think this is a problem with Go necessarily, but it is a transition that takes some getting used to, especially if coming from langs like Ruby where cyclical imports are perfectly fine.
Edit: No markdown.. what!? =( I'll leave it there anyway. Hope it doesn't confuse you.
Thanks for the list, that's really interesting. I've definitely run into the pointer variable issue.
Re the markdown, I'm trialling WYSIWYG editing at the moment with content editable (admin only), should be rolling it out to all comments soon.
Another invaluable read on slices for people leaning Go is https://github.com/golang/go/wiki/SliceTricks especially for those coming from a language where actions like pop, push, delete, etc. are typically performed by invoking methods in a heavily OO style.
Updated to include this as well. Thanks!
Thanks for the link, might post that one too.
Very comprehensive thanks