go - Can I compare variable types with .(type) in Golang? -
i'm quite confused .(type) syntax interface variables. possible use this:
var a,b interface{} // code if first.(type) == second.(type) { }
or reflect.typeof() option check if underlying types of , b same type? comparison making in code above?
someinterface.(type)
only used in type switches. in fact if tried run you'd see in error message.
func main() { var a, b interface{} = 1 b = 1 fmt.println(a.(type) == b.(type)) }
prog.go:10: use of .(type) outside type switch
what instead a.(int) == b.(int)
, no different int(a) == int(b)
func main() { var a, b interface{} = 1 b = 1 fmt.println(a.(int) == b.(int)) }
true
Comments
Post a Comment