Consider two types identical in structure but differing in their tags:
type Foo struct {
Id int64 `json:"-"`
}
type Bar struct {
Id int64 `json:"id"`
}
Unfortunately Go's idiom forbids casting between two types when their tags differ and for good reason. However I still need to be able to control the data that is serialised to JSON and don't want to use interface{}
types.
My question is, how safe is it to use golang's unsafe.Pointer
to perform casts between two types which are identical in structure (but not tags)? Something like:
rf := &Foo{1}
rb := (*Bar)(unsafe.Pointer(rf))
Is there any chance at all of a panic ensuing maybe because internally the data in each of the two types is held slightly differently due to the tags differing or is the information about tags held separate from the actual type data and data in each of the types is structurally identical?
EDIT
For clarification I should mention that although the example provided above employs single-field structs, the question is really about struct types containing multiple fields.