One option is to use embedded structs:
type UnmarshalStruct struct {
Foo string
}
type MarshalStruct struct {
UnmarshalStruct
FieldYouDontWantUnmarshaled string
}
When marshaling, you would marshal MarshalStruct
. Because UnmarshalStruct
is embedded, json.Marshal
automatically includes all the fields from UnmarshalStruct
.
When unmarshaling, you would unmarshal UnmarshalStruct
and then create a new MarshalStruct
like this:
var unmarshaled UnmarshalStruct
err := json.Unmarshal(&unmarshaled)
if err != nil {
panic(err)
}
return MarshalStruct {
UnmarshalStruct: unmarshaled
}
Although, if I may ask: why do you not want a field to be unmarshaled? You could always just set it back to the zero value after unmarshaling, which is a lot easier of a solution.
A bit of a cynical take, but it wouldn't surprise me if ICE started throwing away shoes and phones onto the roadside.