6 ms·
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 a
by leaveyou 5y 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 https://play.golang.org/p/-xDqaxvqWhm
- simiones 5y agoYes, 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 https://play.golang.org/p/UJ-t63bKyJJ