I am comparing two slices, both of type []int
. One is coming in to API in the form of json and parsed as go struct. In the struct, it is intialized as empty []int{}
. Second is saved in the database (MongoDb) and is fetched and mapped to same struct type.
In some cases, the two slices are completely blank. But comparison is returning false
.
reflect.DeepEqual(oldSettings.S1, newSettings.S1)
I have also checked both fields type using reflect.TypeOf(newSettings.S1)
. It is retuning []int
for both.
Please consider this playground link for structure example.
https://play.golang.org/p/1JTUCPImxwq
type OldSettings struct {
S1 []int
}
type NewSettings struct {
S1 []int
}
func main() {
oldSettings := OldSettings{}
newSettings := NewSettings{}
if reflect.DeepEqual(oldSettings.S1, newSettings.S1) == false {
fmt.Println("not equal")
} else {
fmt.Println("equal")
}
}
Thanks!