Hacker News new | ask | show | jobs
by leaveyou 1751 days ago
append(ref, 4) // error: first argument to append must be slice

append(*ref, 4) // error: append(*ref, 4) evaluated but not used

this is the correct version and it also removes the incertitude:

*ref = append(*ref, 4)

https://play.golang.org/p/-xDqaxvqWhm

1 comments

Yes, I made a significant error in forgetting what slice variables actually represent. In my example, even fixing the compilation errors (with `_ = append(*ref, 4)` ), v[3] would always be an array index out of bounds error, since v itself always points to just the first 3 elements of the array, regardless of resizing. This is another significant difference between slices and C++ vectors.

A more interesting example showing that resizing can be observed (getting rid of the pointer to the slice, since it's not useful anyway):

https://play.golang.org/p/UJ-t63bKyJJ