## Question:

How do you compare two structs? What about two interfaces? Provide examples.

You can compare two structs with the `==` operator, as you would do with other simple types. Just make sure they do not contain any slices, maps, or functions, in which case the code will not be compiled.

``````type Foo struct {
A int
B string
C interface{}
}
a := Foo{A: 1, B: "one", C: "two"}
b := Foo{A: 1, B: "one", C: "two"}

println(a == b)
// Output: true

type Bar struct {
A []int
}
a := Bar{A: []int{1}}
b := Bar{A: []int{1}}

println(a == b)
// Output: invalid operation: a == b (struct containing []int cannot be compared)
``````

You can compare two interfaces with the `==` operator as long as the underlying types are “simple” and identical. Otherwise the code will panic at runtime:

``````var a interface{}
var b interface{}

a = 10
b = 10
println(a == b)
// Output: true

a = []int{1}
b = []int{2}
println(a == b)
// Output: panic: runtime error: comparing uncomparable type []int
``````

Both structs and interfaces which contain maps, slices (but not functions) can be compared with the `reflect.DeepEqual()` function:

``````var a interface{}
var b interface{}

a = []int{1}
b = []int{1}
println(reflect.DeepEqual(a, b))
// Output: true

a = map[string]string{"A": "B"}
b = map[string]string{"A": "B"}
println(reflect.DeepEqual(a, b))
// Output: true

temp := func() {}
a = temp
b = temp
println(reflect.DeepEqual(a, b))
// Output: false
``````

For comparing byte slices, there are nice helper functions in the `bytes` package: `bytes.Equal()``bytes.Compare()`, and `bytes.EqualFold()`. The latter is for comparing text strings disregarding the case, which are much faster than the `reflect.DeepEqual()`.

Keywords:
{"ft_lang_label":"__label__en","ft_lang_prob":0.71435416,"math_prob":0.9911057,"size":1649,"snap":"2020-24-2020-29","text_gpt3_token_len":442,"char_repetition_ratio":0.13738602,"word_repetition_ratio":0.09489051,"special_character_ratio":0.30867192,"punctuation_ratio":0.18208955,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9960234,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-05T03:22:32Z\",\"WARC-Record-ID\":\"<urn:uuid:97cfbfba-6c50-4540-af82-67779adc8bfe>\",\"Content-Length\":\"17822\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c221021f-2b94-4b7e-bb6a-92f055949eb5>\",\"WARC-Concurrent-To\":\"<urn:uuid:8f9571ae-8ef5-464b-9940-cb451c6a4da7>\",\"WARC-IP-Address\":\"172.67.155.70\",\"WARC-Target-URI\":\"http://quizbucket.org/how-do-you-compare-two-structs-what-about-two-interfaces-provide-examples-3792\",\"WARC-Payload-Digest\":\"sha1:7D5LWPCKUSTHY6Z337KC42PCMEESH5KS\",\"WARC-Block-Digest\":\"sha1:TMMMLJTMMCQ7HWP6WRN7DDO5OXE2EETK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655886865.30_warc_CC-MAIN-20200705023910-20200705053910-00458.warc.gz\"}"}