golang编程

golang的数据类型 - 分为基本数据类型 ini float string bool ... 和 派生数据类型 map slice struct ...

// 可比较:Integer,Floating-point,String,Boolean,Complex(复数型),Pointer,Channel,Interface,Array
// 不可比较:Slice,Map,Function
 
其中,map不可直接比较
a := map[string]string{"name": "jack"}
b := map[string]string{"name": "rose"}
// fmt.Println(a == b) // 语法错误
result := reflect.DeepEqual(a, b)
fmt.Println("result = ", result) //false

但是可以使用 reflect 比较

而 struct,相同的struct的实例化变量可以直接比较(限定 struct 的成员变量不存在不可比较变量 ),不同的无法比较(可以通过类型强转进行直接比较)     
 
type people struct {
    name string
}
type human struct {
    age int
}

o := &people{name: "jack"}
z := &people{name: "jack"}
t := &human{age: 18}
fmt.Println(t)
// fmt.Println(o == t) // 这里报告语法错误,两个mismatched types *people and *human
fmt.Println(o == z) //这里正常比较 - 相同的struct的实例化的两个对象 - 可以比较 (struct里面没有不可比较成员)

zzz := reflect.DeepEqual(o, z)

以上,可以断定

相同 struct 的实例化对象没有不可比较成员可以直接比较,而不一样的不可比较,但是可通过 reflect 进行比较

 
I can see a bigger world.
原文地址:https://www.cnblogs.com/xuweiqiang/p/14511717.html