How to check for an empty struct?
I define a struct ...
type Session struct {
playerId string
beehive string
timestamp time.Time
}
Sometimes I assign an empty session to it (because nil is not possible)
session = Session{};
Then I want to check, if it is empty:
if session == Session{} {
// do stuff...
}
Obviously this is not working. How do I write it?
You can use == to compare with a zero value composite literal because all fields are comparable:
if (Session{}) == session {
fmt.Println("is zero value")
}
playground example
Because of a parsing ambiguity, parentheses are required around the composite literal in the if condition.
The use of ==
above applies to structs where all fields are comparable. If the struct contains a non-comparable field (slice, map or function), then the fields must be compared one by one to their zero values.
An alternative to comparing the entire value is to compare a field that must be set to a non-zero value in a valid session. For example, if the player id must be != "" in a valid session, use
if session.playerId == "" {
fmt.Println("is zero value")
}
Here are 3 more suggestions or techniques:
With an Additional Field
You can add an additional field to tell if the struct has been populated or it is empty. I intentionally named it ready
and not empty
because the zero value of a bool
is false
, so if you create a new struct like Session{}
its ready
field will be automatically false
and it will tell you the truth: that the struct is not-yet ready (it's empty).
type Session struct {
ready bool
playerId string
beehive string
timestamp time.Time
}
When you initialize the struct, you have to set ready
to true
. Your isEmpty()
method isn't needed anymore (although you can create one if you want to) because you can just test the ready
field itself.
var s Session
if !s.ready {
// do stuff (populate s)
}
Significance of this one additional bool
field increases as the struct grows bigger or if it contains fields which are not comparable (e.g. slice, map
and function values).
Using the Zero Value of an Existing Field
This is similar to the previous suggestion, but it uses the zero value of an existing field which is considered invalid when the struct is not empty. Usability of this is implementation dependant.
For example if in your example your playerId
cannot be the empty string
""
, you can use it to test if your struct is empty like this:
var s Session
if s.playerId == "" {
// do stuff (populate s, give proper value to playerId)
}
In this case it's worth incorporating this check into an isEmpty()
method because this check is implementation dependant:
func (s Session) isEmpty() bool {
return s.playerId == ""
}
And using it:
if s.isEmpty() {
// do stuff (populate s, give proper value to playerId)
}
Use Pointer to your struct
The second suggestion is to use a Pointer to your struct: *Session
. Pointers can have nil
values, so you can test for it:
var s *Session
if s == nil {
s = new(Session)
// do stuff (populate s)
}