reflect.DeepEqual() Function in Golang with Examples
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
package main
import (
"fmt"
"reflect"
)
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" ,
}
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
package main
import (
"fmt"
"reflect"
)
func main() {
src := reflect.ValueOf([]int{ 10 , 20 , 32 })
dest := reflect.ValueOf([]int{ 1 , 2 , 3 })
cnt := reflect.Copy(dest, src)
cnt+= 1
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
Share your thoughts in the comments
Please Login to comment...