In the following code, a1 and a2 are same. They have same fields but with different orders (Book A and Book B are in different order). When I compare then using DeepEqual() method, the result says they are not equal. How to compare them and get a result that they are equal?
package main
import (
"fmt"
"reflect"
)
type Author struct {
name string
Books []*Book
}
type Book struct {
id int
name string
}
func main() {
a1 := Author{
name: "Author Name",
Books: []*Book {
{
id: 1,
name: "Book A",
},
{
id: 2,
name: "Book B",
},
},
}
a2 := Author{
name: "Author Name",
Books: []*Book {
{
id: 2,
name: "Book B",
},
{
id: 1,
name: "Book A",
},
},
}
fmt.Println("Is a1 equal to a2: ", reflect.DeepEqual(a1, a2))
}
Result:
Is a1 equal to a2: false