我如何在围棋中找到对象的类型?在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))
}
在这种情况下,我无法获得对象行的类型,这是一个字符串数组。
当前回答
如果我们有这些变量:
var counter int = 5
var message string = "Hello"
var factor float32 = 4.2
var enabled bool = false
1: fmt。Printf %T格式:要使用此功能,您应该导入“fmt”
fmt.Printf("%T \n",factor ) // factor type: float32
2:反映。TypeOf函数:要使用这个特性,你应该导入"reflect"
fmt.Println(reflect.TypeOf(enabled)) // enabled type: bool
3: reflect. valueof (X).Kind():要使用这个特性,你应该导入"reflect"
fmt.Println(reflect.ValueOf(counter).Kind()) // counter type: int
其他回答
最好的方法是在谷歌中使用反射概念。 反映。TypeOf在给出包名的同时给出类型 reflect.TypeOf().Kind()给出下划线类型
我整理了以下内容。
fmt %T:值类型的go语法表示 reflect.TypeOf.String () reflect.TypeOf.Kind () 类型的断言
例子
package _test
import (
"fmt"
"reflect"
"testing"
)
func TestType(t *testing.T) {
type Person struct {
name string
}
var i interface{}
i = &Person{"Carson"}
for idx, d := range []struct {
actual interface{}
expected interface{}
}{
{fmt.Sprintf("%T", "Hello") == "string", true},
{reflect.TypeOf("string").String() == "string", true},
{reflect.TypeOf("string").Kind() == reflect.String, true},
{reflect.TypeOf(10).String() == "int", true},
{reflect.TypeOf(10).Kind() == reflect.Int, true},
{fmt.Sprintf("%T", 1.2) == "float64", true},
{reflect.TypeOf(1.2).String() == "float64", true},
{reflect.TypeOf(1.2).Kind() == reflect.Float64, true},
{reflect.TypeOf([]byte{3}).String() == "[]uint8", true},
{reflect.TypeOf([]byte{3}).Kind() == reflect.Slice, true},
{reflect.TypeOf([]int8{3}).String() == "[]int8", true},
{reflect.TypeOf([]int8{3}).Kind() == reflect.Slice, true},
{reflect.TypeOf(Person{"carson"}).Kind() == reflect.Struct, true},
{reflect.TypeOf(&Person{"carson"}).Kind() == reflect.Ptr, true},
{fmt.Sprintf("%v", i.(*Person)) == "&{Carson}", true},
{fmt.Sprintf("%+v", i.(*Person)) == "&{name:Carson}", true},
} {
if d.actual != d.expected {
t.Fatalf("%d | %s", idx, d.actual)
}
}
}
去操场
反映包来拯救:
reflect.TypeOf(obj).String()
检查这个演示
如果我们有这些变量:
var counter int = 5
var message string = "Hello"
var factor float32 = 4.2
var enabled bool = false
1: fmt。Printf %T格式:要使用此功能,您应该导入“fmt”
fmt.Printf("%T \n",factor ) // factor type: float32
2:反映。TypeOf函数:要使用这个特性,你应该导入"reflect"
fmt.Println(reflect.TypeOf(enabled)) // enabled type: bool
3: reflect. valueof (X).Kind():要使用这个特性,你应该导入"reflect"
fmt.Println(reflect.ValueOf(counter).Kind()) // counter type: int
Go反射包具有检查变量类型的方法。
下面的代码段将打印出字符串、整数和浮点数的反射类型。
package main
import (
"fmt"
"reflect"
)
func main() {
tst := "string"
tst2 := 10
tst3 := 1.2
fmt.Println(reflect.TypeOf(tst))
fmt.Println(reflect.TypeOf(tst2))
fmt.Println(reflect.TypeOf(tst3))
}
输出:
string
int
float64
参见:http://play.golang.org/p/XQMcUVsOja查看它的实际操作。
更多文档请访问:http://golang.org/pkg/reflect/#Type