I googled "emulate c unions in go" to find examples of people wanting to do
the same thing. It appears that most (all?) of them are better solved using
Go interfaces. While I only spent a few minutes looking at the results of
my search I didn't see any examples where something like your trick was
n
Note that is not permitted by the rules of the unsafe package, and that in
particular it will break the garbage collector if any of the types stored
in the union contain pointer fields.
Ian
On Thu, Aug 24, 2023, 7:59 PM aind...@gmail.com wrote:
> I found this trick recently to replicate a C uni
I found this trick recently to replicate a C union in Go with
unsafe.Sizeof, arrays, and some constant arithmetic. This lets you
stack-allocate data that needs to be the max size of multiple other data
types. So given a type A and B that you want to union, you can say
const ASize = int64(unsafe