How to Compare Equality of Struct, Slice and Map in Golang?

In Golang, reflect.DeepEqual function is used to compare the equality of struct, slice, and map in Golang. It is used to check if two elements are “deeply equal” or not. Deep means that we are comparing the contents of the objects recursively. Two distinct types of values are never deeply equal. Two identical types are deeply equal if one of the following cases is true

1. Slice values are deeply equal when all of the following are true:

  • They are both nil or both non-nil.
  • Their length is same.
  • Either they have same initial entry (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal.

2. Struct values are deeply equal only if their corresponding fields (i.e. both exported and unexported) are deeply equal.

3. Map values are deeply equal when each of the following are true:

  • They both are nil or non-nil
  • Their length is same
  • Their corresponding keys have deeply equal values

Note: We need to import the reflect package to use DeepEqual.



Syntax:

func DeepEqual(x, y interface{}) bool

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to compare equality
// of struct, slice, and map
package main
  
import (
    "fmt"
    "reflect"
)
  
type structeq struct {
    X int
    Y string
    Z []int
}
  
func main() {
    s1 := structeq{X: 50,
        Y: "GeeksforGeeks",
        Z: []int{1, 2, 3},
    }
    s2 := structeq{X: 50,
        Y: "GeeksforGeeks",
        Z: []int{1, 2, 3},
    }
      
    // comparing struct
    if reflect.DeepEqual(s1, s2) {
        fmt.Println("Struct is equal")
    } else {
        fmt.Println("Struct is not equal")
    }
  
    slice1 := []int{1, 2, 3}
    slice2 := []int{1, 2, 3, 4}
      
    // comparing slice
    if reflect.DeepEqual(slice1, slice2) {
        fmt.Println("Slice is equal")
    } else {
        fmt.Println("Slice is not equal")
    }
  
    map1 := map[string]int{
        "x": 10,
        "y": 20,
        "z": 30,
    }
    map2 := map[string]int{
        "x": 10,
        "y": 20,
        "z": 30,
    }
      
    // comparing map
    if reflect.DeepEqual(map1, map2) {
        fmt.Println("Map is equal")
    } else {
        fmt.Println("Map is not equal")
    }
}

chevron_right


Output:

Struct is equal
Slice is not equal
Map is equal

However, cmp.Equal is a better tool for comparing structs. To use this, we need to import the “github.com/google/go-cmp/cmp” package.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import (
    "fmt"
    "github.com/google/go-cmp/cmp"
)
  
type structeq struct {
    X int
    Y string
    Z []int
}
  
func main() {
    s1 := structeq{X: 50,
        Y: "GeeksforGeeks",
        Z: []int{1, 2, 3},
    }
    s2 := structeq{X: 50,
        Y: "GeeksforGeeks",
        Z: []int{1, 2, 3},
    }
    // comparing struct
    if cmp.Equal(s1, s2) {
        fmt.Println("Struct is equal")
    } else {
        fmt.Println("Struct is not equal")
    }
}

chevron_right


Output:

Struct is equal



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.