Golang reflect.DeepEqual() function is “used to check whether x and y are deeply equal.” Deep equality means that not only the top-level values are compared, but also their elements or fields, recursively.
Syntax
func DeepEqual(x, y interface{}) bool
Parameters
The function accepts two arguments, x, and y, of type interface{}. This means you can pass any value to the function, and it will attempt to compare them.
Return value
It returns a boolean value, suggesting the two values are deeply equal.
Example 1
package main
import (
"fmt"
"reflect"
)
type Person struct {
Name string
Age int
Hobby []string
}
func main() {
p1 := Person{
Name: "Khushi",
Age: 30,
Hobby: []string{"travelling", "hiking"},
}
p2 := Person{
Name: "Krunal",
Age: 30,
Hobby: []string{"investing", "dancing"},
}
if reflect.DeepEqual(p1, p2) {
fmt.Println("p1 and p2 are deeply equal")
} else {
fmt.Println("p1 and p2 are not deeply equal")
}
}
Output
p1 and p2 are not deeply equal
Example 2
package main
import (
"fmt"
"reflect"
)
type Person struct {
Name string
Age int
Hobby []string
}
func main() {
p1 := Person{
Name: "Khushi",
Age: 30,
Hobby: []string{"travelling", "hiking"},
}
p2 := Person{
Name: "Khushi",
Age: 30,
Hobby: []string{"travelling", "hiking"},
}
if reflect.DeepEqual(p1, p2) {
fmt.Println("p1 and p2 are deeply equal")
} else {
fmt.Println("p1 and p2 are not deeply equal")
}
}
Output
p1 and p2 are deeply equal
That’s it.
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.