我如何在围棋中找到对象的类型?在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))
}
在这种情况下,我无法获得对象行的类型,这是一个字符串数组。
当前回答
对于数组和切片,使用Type.Elem():
a := []string{"foo", "bar", "baz"}
fmt.Println(reflect.TypeOf(a).Elem())
其他回答
我整理了以下内容。
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)
}
}
}
去操场
最好的方法是在谷歌中使用反射概念。 反映。TypeOf在给出包名的同时给出类型 reflect.TypeOf().Kind()给出下划线类型
我找到了3种在运行时返回变量类型的方法:
使用字符串格式
func typeof(v interface{}) string {
return fmt.Sprintf("%T", v)
}
使用反射包
func typeof(v interface{}) string {
return reflect.TypeOf(v).String()
}
使用类型开关
func typeof(v interface{}) string {
switch v.(type) {
case int:
return "int"
case float64:
return "float64"
//... etc
default:
return "unknown"
}
}
每个方法都有不同的最佳用例:
字符串格式——短且占用空间小(不需要导入反射包) 反射包-当需要关于类型的更多细节时,我们可以访问完整的反射功能 类型切换-允许分组类型,例如将所有int32, int64, uint32, uint64类型识别为“int”
使用反射包:
包反射实现运行时反射,允许程序 操作任意类型的对象。典型的用法是取a 值与静态类型接口{}并提取其动态类型 信息调用TypeOf,返回一个类型。
package main
import (
"fmt"
"reflect"
)
func main() {
b := true
s := ""
n := 1
f := 1.0
a := []string{"foo", "bar", "baz"}
fmt.Println(reflect.TypeOf(b))
fmt.Println(reflect.TypeOf(s))
fmt.Println(reflect.TypeOf(n))
fmt.Println(reflect.TypeOf(f))
fmt.Println(reflect.TypeOf(a))
}
生产:
bool
string
int
float64
[]string
操场上
使用ValueOf(i interface{}).Kind()的示例:
package main
import (
"fmt"
"reflect"
)
func main() {
b := true
s := ""
n := 1
f := 1.0
a := []string{"foo", "bar", "baz"}
fmt.Println(reflect.ValueOf(b).Kind())
fmt.Println(reflect.ValueOf(s).Kind())
fmt.Println(reflect.ValueOf(n).Kind())
fmt.Println(reflect.ValueOf(f).Kind())
fmt.Println(reflect.ValueOf(a).Index(0).Kind()) // For slices and strings
}
生产:
bool
string
int
float64
string
操场上
如果你想检测if表达式中的类型:
if str, ok := myvar.(string); ok {
print("It's a string")
}
或者没有类型断言(可能会产生错误):
if reflect.TypeOf(myvar).String() == "string" {
print("It's a string")
}