Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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")
    }
}


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:




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")
    }
}


Output:

Struct is equal


Last Updated : 17 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads