3 ms·
Go struct memory layout is currently undefined[0], so you're already not supposed to hand structs off to C, or to memcpy it to or from the wire. Unless you're u
by masklinn 11y ago
Go struct memory layout is currently undefined[0], so you're already not supposed to hand structs off to C, or to memcpy it to or from the wire. Unless you're using cgo maybe, I don't know if (or that) cgo provides any guarantee.
[0] https://github.com/golang/go/issues/10014 https://github.com/golang/go/issues/10014
- kibwen 11y agoIs this mentioned in the spec anywhere? That piece of information was precisely what I was looking for, but wasn't able to find.
- masklinn 11y agoThe spec doesn't define field ordering anywhere, which is why it's undefined. Russ Cox commented on the bug I linked essentially confirming it: > It seems fine to me for the spec not to guarantee anything about struct field order in memory. The spec doesn't operate at that level. And while he adds > That said, no Go compiler should probably ever reorder struct fields. the final result is struct memory layout is unspecified and implementation-dependent (essentially identical to repr(rust), as I understand it)
- kibwen 11y agoThe intent of a specification is to be explicit, and in doing so it's poor form to say "this is undefined, so we're simply not going to mention it in the spec at all". If it's undefined then the spec should say so, especially if the alternative is for one to cast about in confusion until hopefully stumbling across the relevant documentation in, of all places, the bug tracker.