4 ms·
Go pointers can point at the stack or inside objects just fine, they are exactly as expressive as C# unsafe pointers (i.e. more expressive than `ref`). What Go
by int_19h 1y ago
Go pointers can point at the stack or inside objects just fine, they are exactly as expressive as C# unsafe pointers (i.e. more expressive than `ref`).
What Go can't do is create a single-element slice out of a variable or pointer to it. But that just means code duplication if you need to cover both cases, not that it's not expressible at all.
- kbolino 1y ago> What Go can't do is create a single-element slice out of a variable or pointer to it. var x int s := unsafe.Slice(&x, 1) fmt.Println(&x == &s[0]) // Output: true
- int_19h 1y agoGood catch! That takes care of the unsafe pointer case, but not the safe ref case. There's no reason for this to be unsafe - you're asking for a 1-element slice, and the compiler knows that the variable is always going to be there as long as the reference exists. In C#, `Span<T>` has a (safe) constructor from `ref T`.