Hacker News new | ask | show | jobs
by Fire-Dragon-DoL 1748 days ago
s = append(s, elem)

I read this immediately as "create a new copy of the original slice with one additional element", so I presumed that was the case. It would actually be shocking the opposite, if I could end up modifying the original one (before the append) with a pointer to the new s, which seems to be the case!

Big gotcha there: treat slices as stateful at all time.

Since it has an assignment operation, it must be creating something new, otherwise it would have been a method of the slice itself

EDIT: I just realized the gotcha is not there at all, Go would consider the first slice to be of N length and the second slice of length N+1. Comparing the two slices would give an error at some point because one is shorter than the other, so the fact that the address changes or not is irrelevant. However I can see this becoming problematic with pointers, which proves the point of the article.