Open In App

reflect.DeepEqual() Function in Golang with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.DeepEqual() Function in Golang is used to check whether x and y are “deeply equal” or not. To access this function, one needs to imports the reflect package in the program.

Syntax: 

func DeepEqual(x, y interface{}) bool

Parameters: This function takes two parameters with value of any type, i.e. x, y.
Return Value: This function returns the boolean value. 
 

Below examples illustrate the use of the above method in Golang:

Example 1: 

Go




// Golang program to illustrate
// reflect.DeepEqual() Function
 
package main
 
import (
    "fmt"
    "reflect"
)
 
 
// Main function
func main() {
 
    map_1 := map[int]string{
   
        200: "Anita",
        201: "Neha",
        203: "Suman",
        204: "Robin",
        205: "Rohit",
    }
    map_2 := map[int]string{
   
        200: "Anita",
        201: "Neha",
        203: "Suman",
        204: "Robin",
        205: "Rohit",
    }
     
    // DeepEqual is used to check
    // two interfaces are equal or not
    res1 := reflect.DeepEqual(map_1, map_2)
    fmt.Println("Is Map 1 is equal to Map 2: ", res1)
}


Output: 

Is Map 1 is equal to Map 2:  true

Example 2: 

Go




// Golang program to illustrate
// reflect.DeepEqual() Function
 
package main
 
import (
    "fmt"
    "reflect"
)
 
 
// Main function
func main() {
 
    src := reflect.ValueOf([]int{10, 20, 32})
    dest := reflect.ValueOf([]int{1, 2, 3})
     
    cnt := reflect.Copy(dest, src)
    cnt+=1
     
    // DeepEqual is used to check
    // two interfaces are equal or not
    res1 := reflect.DeepEqual(src, dest )
    fmt.Println("Is dest is equal to src: ", res1)
}


Output: 

Is dest is equal to src:  false

 



Last Updated : 15 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads