"Play turn Golang" reflect. Deepequal

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

If there are two map, the content is the same, only the order is different

m1:=map[string]int{"a":1,"b":2,"C":3};m2:=map[string]int{"a":1,"C":3,"b":2};

How do we judge whether the two are consistent?

If you're going to write this:

Fmt. Println ("m1==m2", m1==m2)

This does not work, go does not rewrite the map's = = operator, the compiler will report an error:

Invalid operation:m1 = = m2 (map can only is compared to nil)

This means that map variables can only be compared to null (nil), for example:

Fmt. Println ("m1 = = nil? ") ", m1==nil) fmt. Println ("m2! = nil? ", M2!=nil)

This is no problem and the result is:

Running ...

M1 = = nil? False
M2! = nil? True

How do you compare that? If you want to implement the program, it is really troublesome, such as my idea is: loop M1, see if each key is in the M2, and then compare M1[key] is equal to M2[key], if all OK, then loop m2 in turn. It's really trouble:

Func Cmpmap (m1,m2 map[string]int)BOOL{     forK1,V1: =Range m1{ifv2,has:=m2[k1];has{ifv1!=V2 {return false            }        }Else{            return false; }    }     fork2,v2:=Range m2{ifv1,has:=m1[k2];has{ifv1!=v2{return false; }        }Else{            return false; }    }    return true;}

In fact, Go's Reflex pack has a huge handy weapon reflect. Deepequal, can easily solve this problem, please see:

Package Mainimport ("FMT"        "reflect") Type TTstruct{Codeint}func Main () {m1:=map[string]int{"a":1,"b":2,"C":3}; M2:=map[string]int{"a":1,"C":3,"b":2}; Fmt. Println ("M1 = = nil?", m1==nil) fmt. Println ("m2! = nil?", m2!=Nil)//FMT. Println ("m1==m2", m1==m2)Fmt. Println ("Cmpmap (m1,m2) =", Cmpmap (m1,m2)); Fmt. Println ("reflect. Deepequal (m1,m2) =", reflect. Deepequal (m1,m2)) fmt. Println () M3:=map[string]int{"a":1,"b":2,"C":3,"D":1}; Fmt. Println ("Cmpmap (m1,m3) =", Cmpmap (M1,M3)); Fmt. Println ("reflect. Deepequal (m1,m3) =", reflect. Deepequal (M1,M3))}func cmpmap (m1,m2 map[string]int)BOOL{     forK1,V1: =Range m1{ifv2,has:=m2[k1];has{ifv1!=V2 {return false            }        }Else{            return false; }    }     fork2,v2:=Range m2{ifv1,has:=m1[k2];has{ifv1!=v2{return false; }        }Else{            return false; }    }    return true;}

Execution Result:

Falsetrue=  true=  truecmpmap (M1,M3)  false=  false0.

But the drawback is that because of reflect. The deepequal needs to be reflected, and the efficiency is much worse than the function we write ourselves, write a simple test:

        Start:=time. Now (); for I:=0;i<100000;i++{cmpmap (M1,M2)}end:=time. Now ();d u:=end. Sub (Start) fmt. Println ("100000 call Cmpmap (m1,m2) elapsed=", du) start=time. Now (); for I:=0;i<100000;i++{reflect. Deepequal (M1,M2);} End=time. Now ();d u=end. Sub (start); Fmt. Println ("100000 call reflect. Deepequal (m1,m2) elapsed= ", du)    

Look at the results, about 10 times times the gap

100000  the . 544992ms 100000 735. 577069ms

Of course, in general, craved performance loss is not what, especially when the uncertainty type needs reflection, it is we can not use a powerful tool.

Like what:

Func Main () {m1:=map[string]Interface{}{"a":"1","b":2,"C":3}; M2:=map[string]Interface{}{"a":1,"C":"3","b":2}; Fmt. Println (' reflect. Deepequal (m1["a"],m2["a"] ', reflect. Deepequal (m1["a"],m2["a"])); Fmt. Println (' reflect. Deepequal (m1["b"],m2["b"] ', reflect. Deepequal (m1["b"],m2["b"]));}

Execution Result:

Running...reflect. Deepequal (m1["a"],m2["a"falsereflect. Deepequal (m1["b"],m2["b"true

In this case, if we write our own code comparisons, it is very cumbersome to use the switch type syntax, thanks to the go contains such a good tool.

Contact Us

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

  • 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.