我如何在围棋中找到对象的类型?在Python中,我只使用typeof来获取对象的类型。在围棋中也有类似的情况,有没有办法实现同样的情况?
下面是我正在迭代的容器:
for e := dlist.Front(); e != nil; e = e.Next() {
lines := e.Value
fmt.Printf(reflect.TypeOf(lines))
}
在这种情况下,我无法获得对象行的类型,这是一个字符串数组。
我如何在围棋中找到对象的类型?在Python中,我只使用typeof来获取对象的类型。在围棋中也有类似的情况,有没有办法实现同样的情况?
下面是我正在迭代的容器:
for e := dlist.Front(); e != nil; e = e.Next() {
lines := e.Value
fmt.Printf(reflect.TypeOf(lines))
}
在这种情况下,我无法获得对象行的类型,这是一个字符串数组。
当前回答
最好的方法是在谷歌中使用反射概念。 反映。TypeOf在给出包名的同时给出类型 reflect.TypeOf().Kind()给出下划线类型
其他回答
如果你想检测if表达式中的类型:
if str, ok := myvar.(string); ok {
print("It's a string")
}
或者没有类型断言(可能会产生错误):
if reflect.TypeOf(myvar).String() == "string" {
print("It's a string")
}
你可以使用reflect.TypeOf。
基本类型(如。: int, string):它将返回它的名称(例如:int, string) 结构:它将返回格式为<包名>的内容。<struct name>(例如:main.test)
你可以在运行时使用"reflect"包的TypeOf函数或使用fmt.Printf()检查任何变量/实例的类型:
package main
import (
"fmt"
"reflect"
)
func main() {
value1 := "Have a Good Day"
value2 := 50
value3 := 50.78
fmt.Println(reflect.TypeOf(value1 ))
fmt.Println(reflect.TypeOf(value2))
fmt.Println(reflect.TypeOf(value3))
fmt.Printf("%T",value1)
fmt.Printf("%T",value2)
fmt.Printf("%T",value3)
}
反映包来拯救:
reflect.TypeOf(obj).String()
检查这个演示
最好的方法是在谷歌中使用反射概念。 反映。TypeOf在给出包名的同时给出类型 reflect.TypeOf().Kind()给出下划线类型