3 ms·
> 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]) //
by 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`.