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:

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:

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




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 :