最近在学习go 想要判断两个变量是否相等 发现如果这两个变量赋值时如果类型不同 使用if判断时会直接报错
.\test.go:123:8: invalid operation: c == d (mismatched types int and float64)
以下是我的代码 请教各位大佬 如果这种情况应该如何进行判断
package main
import (
"fmt"
"reflect"
)
func main() {
c := 1
d := 1.1
ct := reflect.TypeOf(c)
dt := reflect.TypeOf(d)
fmt.Println("!!!!!!!!!!!!!!!!!!")
fmt.Println(ct)
fmt.Println(dt)
if ct == dt {
fmt.Println("相同类型")
if c == d {
fmt.Println("对")
} else {
fmt.Println("错")
}
} else {
fmt.Println("不同类型")
}
}
根据题主代码的意图,可以直接使用reflect.DeepEqual函数来比较,即: