判斷相等的deepequal

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。部落格地址: * [go-reflect-deepequal](https://github.com/Chasiny/Blog/blob/master/blog/go/package/go-reflect-deepequal.md)# DeepEqual## 規則### 1.不同類型的值不會深度相等Values of distinct types are never deeply equal.```gotype S1 struct {Field int}type S2 struct {Field int}func main() {fmt.Println(reflect.DeepEqual(S1{1}, S2{1}))}```>對應輸出false### 2.當兩個數組的元素對應深度相等時,兩個數組深度相等Array values are deeply equal when their corresponding elements are deeply equal.```gofunc main() {Array1 := []string{"hello1", "hello2"}Array2 := []string{"hello1", "hello2"}fmt.Println(reflect.DeepEqual(Array1, Array2))}```>對應輸出true### 3.當兩個相同結構體的所有欄位對應深度相等的時候,兩個結構體深度相等Struct values are deeply equal if their corresponding fields,both exported and unexported, are deeply equal.```gotype S struct {Field1 intfield2 string}func main() {s1 := S{Field1: 1, field2: "hello"}s2 := S{Field1: 1, field2: "hello"}fmt.Println(reflect.DeepEqual(s1, s2))}```>對應輸出true### 4.當兩個函數都為nil時,兩個函數深度相等,其他情況不相等(相同函數也不相等)Func values are deeply equal if both are nil; otherwise they are not deeply equal.```gofunc main() {f1 := func(a int) int {return a * 2}fmt.Println(reflect.DeepEqual(f1, f1))f1 = nilfmt.Println(reflect.DeepEqual(f1, f1))}```>對應輸出false跟true### 5.當兩個interface的真實值深度相等時,兩個interface深度相等Interface values are deeply equal if they hold deeply equal concrete values.```gofunc main() {var i1 interface{}i1 = "hello"var i2 interface{}i2 = "hello"fmt.Println(reflect.DeepEqual(i1, i2))}```>對應輸出true### 6.go中map的比較需要同時滿足以下幾個* 1.兩個map都為nil或者都不為nil,並且長度要相等 they are both nil or both non-nil, they have the same length* 2.相同的map對象或者所有key要對應相同 either they are the same map object or their corresponding keys* 3.map對應的value也要深度相等 map to deeply equal values```gofunc main() {m1 := map[string]int{"a": 1,"b": 2,}m2 := map[string]int{"a": 1,"b": 2,}fmt.Println(reflect.DeepEqual(m1, m2))}```>對應輸出true### 7.指標,滿足以下其一即是深度相等* 1.兩個指標滿足go的==操作符 Pointer values are deeply equal if they are equal using Go's == operator* 2.兩個指標指向的值是深度相等的 ```gofunc main() {m1 := map[string]int{"a": 1,"b": 2,}m2 := map[string]int{"a": 1,"b": 2,}M1:=&m1M2:=&m2fmt.Println(reflect.DeepEqual(M1, M2))}```>對應輸出true### 8.切片,需要同時滿足以下幾點才是深度相等* 1.兩個切片都為nil或者都不為nil,並且長度要相等 they are both nil or both non-nil, they have the same length* 2.兩個切片底層資料指向的第一個位置要相同或者底層的元素要深度相等 either they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal.```gofunc main() {s1 :=[] int {1,2,3,4,5}s2 :=s1[0:3]s3 :=s1[0:3]fmt.Println(reflect.DeepEqual(s2, s3))s4 :=s1[1:4]fmt.Println(reflect.DeepEqual(s2, s4))}```>對應輸出true,false#### 註:空的切片跟nil切片是不深度相等的,例如```gofunc main() {s1 :=[]byte{}s2 :=[]byte(nil)fmt.Println(reflect.DeepEqual(s1, s2))}```>對應輸出false### 其他* 其他類型的值(numbers, bools, strings, channels)如果滿足go的==操作符,則是深度相等的* 要注意不是所有的值都深度相等於自己,例如函數,以及嵌套包含這些值的結構體,數組等---* go如何避免遞迴迴圈判斷結構體嵌套自身:通過使用一個visit(類型為map[visit]bool)標記訪問 在reflect/deepequal.go中的deepValueEqual函數中有這麼一段代碼```goif v1.CanAddr() && v2.CanAddr() && hard(v1.Kind()) {addr1 := unsafe.Pointer(v1.UnsafeAddr())addr2 := unsafe.Pointer(v2.UnsafeAddr())//對指標進行比較排序,減少重複比較if uintptr(addr1) > uintptr(addr2) {// Canonicalize order to reduce number of entries in visited.// Assumes non-moving garbage collector.addr1, addr2 = addr2, addr1}// Short circuit if references are already seen.typ := v1.Type()v := visit{addr1, addr2, typ}//為已訪問的話返回trueif visited[v] {return true}//標記兩個比較的變數為已訪問// Remember for later.visited[v] = true}```215 次點擊  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.