Hacker News new | ask | show | jobs
by skybrian 1750 days ago
It would be more accurate to say that pointers don’t work with append() or any other way of growing an array, since they all depend on reallocating it sometimes.

Incidentally, this is equally true of creating additional pointers or slices pointing into an growable array. They aren’t safe after the next append(). If you grow an array then you need to refer to its elements using array indices.

But if you have a fixed-length array, or between appends, you can use both pointers and slices to point to parts of it, and it will work fine,

This all works the same as C if you think of a slice as a glorified pointer. If you’re thinking of a slice as a JavaScript array then you’ll have trouble.

1 comments

> If you’re thinking of a slice as a JavaScript array then you’ll have trouble.

The problem of Go is that it has you uses slices as that as well as actual slices, there is no vector type. So the confusion is very much understandable and to be expected.