News for hackers. Stuff that matters.
Re:When to use generics by tsimionescu on Wednesday April 13, 2022 @03:08AM (#31011952)
Go is only allowed to move variables if it knows there are not pointers to them, so only the compiler can take this decision. The Go runtime is very much not allowed to move variables.
Re:When to use generics by Dylan16807 on Wednesday April 13, 2022 @03:30AM (#31012061)
I don't think this is right. Every source I can find says that Go does not currently move heap variables, but it reserves the right to, and code that assumes no movement is wrong.

I also found code that demonstrates stack variables being moved despite a pointer to them, but it's version dependent so I can't test it.

When a variable is moved, then obviously pointers to it need to be updated, but that's not an intractable problem.

Looking further, Go doesn't even provide an API for pinning objects right now[1]. You have to do weird workarounds to allocate static objects, or write code that assumes nothing on the heap moves and will explode if that ever changes.

[1] https://github.com/golang/go/issues/46787