Skip to content
Related Articles

Related Articles

reflect.DeepEqual() Function in Golang with Examples
  • Last Updated : 28 Apr, 2020
GeeksforGeeks - Summer Carnival Banner

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 above method in Golang:



Example 1:




// 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 eual 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:




// 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 eual or not
    res1 := reflect.DeepEqual(src, dest )
    fmt.Println("Is dest is equal to src: ", res1)
}

Output:

Is dest is equal to src:  false

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :