type Animal struct {
Name string
LegCount int
}
snake := Animal{Name: "snake", LegCount: 0}
worm := Animal{Name: "worm"}
Question: How can I check snake
and worm
after they've been set, to tell that:
snake
was explicitly set with a LegCount
of 0.worm
's LegCount
was not explicitly set (and therefore based off of its default value)?1) To check if the structure is empty:fmt. Println( "It is an empty structure." ) fmt. Println( "It is not an empty structure." )
A zero value struct is simply a struct variable where each key's value is set to their respective zero value. This article is part of the Structs in Go series. We can create a zero value struct using the var statement to initialize our struct variable.
Struct types The default zero value of a struct has all its fields zeroed.
It is simply impossible to distinguish.
If you are unmarshalling data from XML or JSON, use pointers.
type Animal struct {
Name *string
LegCount *int
}
You will get nil
values for absent fields.
You can use the same convention in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With