This is a creation in Article, where the information may have evolved or changed. Blog address: * [go-reflect-deepequal] (https://github.com/Chasiny/Blog/blob/master/blog/go/package/ GO-REFLECT-DEEPEQUAL.MD # # # # # # # # # # # # # # # # # # # # # 1. Values of different types are not depth equal values of distinct types are never deeply equal. "Gotype S1 str UCT {field Int}type S2 struct {field Int}func main () {FMT. Println (reflect. Deepequal (S1{1}, S2{1})} ' > corresponding output false### 2. When the elements of two arrays correspond to a depth equal, two arrays are of equal depth array values are deeply equal when their Corresponding elements is deeply equal. "Gofunc Main () {Array1: = []string{" Hello1 "," Hello2 "}array2: = []string{] Hello 1 "," Hello2 "}fmt. Println (reflect. Deepequal (Array1, Array2)} "> Corresponds to Output true### 3. When all the fields of two identical structures correspond to the same depth, two structure depth equals struct values is deeply equal if Their corresponding fields,both exported and unexported, is 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)} "> Corresponds to Output true### 4. When two functions are nil, two function depths are equal, other conditions are unequal (same function is not equal) Func ValuEs is deeply equal if both are nil; Otherwise they is 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)} "> corresponding output false with true### 5. When the true value depth of two interface is equal, two interface depths are equal interface values are deeply equal If they hold deeply equal concrete values. "Gofunc main () {var i1 interface{}i1 =" Hello "var i2 interface{}i2 =" HELLO "FM T.println (reflect. Deepequal (I1, I2)} "> corresponding output true### 6.go the comparison of the map needs to meet the following several * 1. Two maps are nil or nil, and are equal in length they is both nil or both Non-nil, they has the same length* 2. The same map object or all keys correspond to the same either they is the same map object or their corresponding keys * 3.map corresponds to the value of the same depth 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)} "> corresponds to Output true### 7. The pointer satisfies one of the following is the depth equal to * 1. Two pointers satisfying go = = operator Pointer values is deeply equal if they is Equa L using Go ' s = = operator* 2. Two pointersThe value pointed to is a depth equal to ' 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)} "> corresponds to the Output true### 8. Slicing, it is necessary to meet the following points is the depth equal * 1. Two slices are nil or nil, and are equal in length they is both nil or both Non-nil, they has the same length* 2. Two slices the underlying data points to the same or the underlying elements to be equal in depth either they point to the same initial entry of the S Ame 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)} "> corresponding output true,false#### Note: Empty slices are not depth equal to nil slices, for example ' Gofunc main () {s1: =[]byte{}s2: =[]byte (Nil) Fmt. Println (reflect. Deepequal (S1, S2)} ' > corresponding output false### other * Other types of values (numbers, bools, strings, channels) if the = = operator satisfies go, it is the same depth * Note that not all values are depth equal to themselves, such as functions, and nested structures containing these values, arrays, etc.---* go how to avoid recursive loops to judge the structure nesting itself: Access in reflect/by using a visit (type Map[visit]bool) tag Dee in the Deepequal.goThe Pvalueequal function has such a piece of code ' GOIF v1. CANADDR () && v2. CANADDR () && hard (v1. Kind ()) {addr1: = unsafe. Pointer (v1. UNSAFEADDR ()) ADDR2: = unsafe. Pointer (v2. UNSAFEADDR ())//compare a pointer to a sort, reduce the repetition comparison 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 is already SE En.typ: = v1. Type () V: = VISIT{ADDR1, ADDR2, typ}//for the accessed word return Trueif visited[v] {return true}//tag Two comparison variables are visited//Remember for later.visited [v] = true} "215 reads
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service