Re: [go-nuts] Replicating C unions with unsafe, arrays, and some constant arithmetic

2023-08-24 Thread Kurtis Rader
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

Re: [go-nuts] Replicating C unions with unsafe, arrays, and some constant arithmetic

2023-08-24 Thread Ian Lance Taylor
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

[go-nuts] Replicating C unions with unsafe, arrays, and some constant arithmetic

2023-08-24 Thread aind...@gmail.com
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 =