在Golang中,可以使用.(type)来比较变量的类型。

huangapple go评论71阅读模式
英文:

Can I compare variable types with .(type) in Golang?

问题

我对接口变量的.(type)语法感到很困惑。我可以像这样使用它吗:

var a, b interface{}
// 一些代码
if first.(type) == second.(type) {
}

或者只能使用reflect.TypeOf()来检查a和b的底层类型是否相同?上面的代码中我在做什么比较?

英文:

I'm quite confused about the .(type) syntax for interface variables. Is it possible to use it like this:

var a,b interface{}
// some code
if first.(type) == second.(type) {
}

or is reflect.TypeOf() the only option to check if the underlying types of a and b are the same type? What comparison am I making in the code above?

答案1

得分: 17

func isType(a, b interface{}) bool {
return fmt.Sprintf("%T", a) == fmt.Sprintf("%T", b)
}

"%T" fmt选项在底层使用反射,使得上述语句实际上与以下语句几乎相同:

func isType(a, b interface{}) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b)
}

两者都可以工作,并且不会像其他建议中的某些类型断言一样引发恐慌。

英文:
func isType(a, b interface{}) bool {
	return fmt.Sprintf("%T", a) == fmt.Sprintf("%T", b)
}

The "%T" fmt option uses reflection under the hood, which would make the above statement practically that same as:

func isType(a, b interface{}) bool {
    return reflect.TypeOf(a) == reflect.TypeOf(b)
}

Either one would work, and won't cause a panic trying to utilize any kind of type assertion like some of the other suggestions.

答案2

得分: 15

someInterface.(type) 只能在类型切换中使用。实际上,如果你尝试运行这段代码,你会在错误信息中看到这一点。

func main() {
    var a, b interface{}
    a = 1
    b = 1

    fmt.Println(a.(type) == b.(type))
}

>prog.go:10: 在类型切换之外使用了 .(type)

你可以改为使用 a.(int) == b.(int),这与 int(a) == int(b) 没有任何区别。

func main() {
    var a, b interface{}
    a = 1
    b = 1

    fmt.Println(a.(int) == b.(int))
}

>true

英文:

someInterface.(type) is only used in type switches. In fact if you tried to run that you'd see that in the error message.

func main() {
	var a, b interface{}
	a = 1
	b = 1

	fmt.Println(a.(type) == b.(type))
}

>prog.go:10: use of .(type) outside type switch

What you could do instead is a.(int) == b.(int), which is really no different from int(a) == int(b)

func main() {
	var a, b interface{}
	a = 1
	b = 1

	fmt.Println(a.(int) == b.(int))
}

>true

答案3

得分: 10

你需要指定类型。该语法用于对接口进行类型断言,而不是检查具体类型。

你需要使用reflect.TypeOf来进行检查。

你可以查看这个答案来了解正确使用类型断言的方法。

英文:

You'd need to specify the type. That syntax is used to make type assertions about interfaces, not about checking the specific type.
You'll have to use reflect.TypeOf for that.

You can view this answer for a proper use of type assertions.

答案4

得分: 3

请尝试这样做:

for key, value := range data {
    switch v := value.(type) {
    case string:
        fmt.Println(key, value, "(string)")
    case float64:
        fmt.Println(key, value, "(float64)")
    case []interface{}:
        fmt.Println(key, value, "(interface)")
    }
}
...
并且你可以在任何接口中使用所有类型...
英文:

Please try this:

for key, value := range data {
	switch v := value.(type) {
	case string:
		fmt.Println(key, value, "(string)")
	case float64:
		fmt.Println(key, value, "(float64)")
    case []interface{}:
        fmt.Println(key, value, "(interface)")
    }
 }

...
and you can use for all types in any interface...

huangapple
  • 本文由 发表于 2016年1月16日 05:50:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/34820469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定