I am passing values to a struct in which the value has omitempty
Offset uint64 'json:"offset,omitempty"'
However when I pass 0 as the value of offset it is also omitted.
Can I somehow declare 0 as a value which is not defined as null?
I am passing values to a struct in which the value has omitempty
Offset uint64 'json:"offset,omitempty"'
However when I pass 0 as the value of offset it is also omitted.
Can I somehow declare 0 as a value which is not defined as null?
Structures for serialization often use pointer to indicate a nullable field. It can make working with the structure a little more cumbersome to work with, but has the advantage of discerning between nil
and 0
type T struct {
Offset *uint64 `json:"offset,omitempty"`
}
With a nil pointer
t := T{}
// marshals to "{}"
And after allocating a zero value
t.Offset = new(uint64)
// marshals to `{"offset":0}`