Go 语言提供了运行时反射的内置支持实现,并允许程序在反射包的帮助下操作任意类型的对象。 Golang 中的reflect.DeepEqual()函数用于检查x 和y 是否“深度相等”。要访问此函数,需要在程序中导入反射包。
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.
下面的例子说明了上述方法在 Golang 中的使用:
示例 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)
}
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)
}
输出:
Is Map 1 is equal to Map 2: true
示例 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 equal or not
res1 := reflect.DeepEqual(src, dest )
fmt.Println("Is dest is equal to src: ", res1)
}
输出:
Is dest is equal to src: false