golang中,哪些数据类型是可比较的,哪些是不可比较的:
type S struct {
Name string
Age int
Address *int
}
func main() {
a := S{
Name: "aa",
Age: 1,
Address: new(int),
}
b := S{
Name: "aa",
Age: 1,
Address: new(int),
}
fmt.Println(a == b) // false
}
输出false,说明:
type S struct {
Name string
Age int
Address *int
Data []int
}
func main() {
a := S{
Name: "aa",
Age: 1,
Address: new(int),
Data: []int{1, 2, 3},
}
b := S{
Name: "aa",
Age: 1,
Address: new(int),
Data: []int{1, 2, 3},
}
fmt.Println(a == b) // invalid operation: a == b (struct containing []int cannot be compared)
}
报错:Slice,Map,Function不可比较
type S struct {
Name string
Age int
Address *int
Data []int
}
func main() {
a := S{
Name: "aa",
Age: 1,
Address: new(int),
Data: []int{1, 2, 3},
}
b := S{
Name: "aa",
Age: 1,
Address: new(int),
Data: []int{1, 2, 3},
}
fmt.Println(reflect.DeepEqual(a, b)) // true
}
DeepEqual函数用来判断两个值是否深度一致。具体比较规则如下: