type Test struct { }
I knew this may be the way to check empty struct by (Test{}) == test. However, it seems not working for the struct which only contains one boolean field. Considering this example:
package main
import "fmt"
type Test struct {
Foo bool
}
func main() {
empty := Test{}
test1 := Test{Foo: true}
test2 := Test{Foo: false}
fmt.Println(Test{} == test1) //False yay
fmt.Println(Test{} == test2) //True oh no ...
fmt.Println(Test{} == empty) //True yay
}
Basically, == thinks the struct containing false field is same to empty struct.
Is there a better way to check the empty struct or am I missing something here ?
Also, you may wonder why only have one field in struct, well, because the struct is in development phase, it may have more fields later on.
emptycontains one field, you just didn't explicitly set it to anything.falsefield is same to empty struct. " This is because in Go they are the same. If you need to differentiate between zero values and "uninitialized" values then you'll have to work around this language feature by adding one more bit of information to the value/field. E.g. by making it a pointer or by using astructtype like thesql.NullXxxtypes found indatabase/sql.