我如何在围棋中找到对象的类型?在Python中,我只使用typeof来获取对象的类型。在围棋中也有类似的情况,有没有办法实现同样的情况?

下面是我正在迭代的容器:

for e := dlist.Front(); e != nil; e = e.Next() {
    lines := e.Value
    fmt.Printf(reflect.TypeOf(lines))
}

在这种情况下,我无法获得对象行的类型,这是一个字符串数组。


当前回答

获取struct中字段的类型

package main

import (
  "fmt"
  "reflect"
)

type testObject struct {
  Name   string
  Age    int
  Height float64
}

func main() {
   tstObj := testObject{Name: "yog prakash", Age: 24, Height: 5.6}
   val := reflect.ValueOf(&tstObj).Elem()
   typeOfTstObj := val.Type()
   for i := 0; i < val.NumField(); i++ {
       fieldType := val.Field(i)
       fmt.Printf("object field %d key=%s value=%v type=%s \n",
          i, typeOfTstObj.Field(i).Name, fieldType.Interface(),
          fieldType.Type())
   }
}

输出

object field 0 key=Name value=yog prakash type=string 
object field 1 key=Age value=24 type=int 
object field 2 key=Height value=5.6 type=float64

参见IDE https://play.golang.org/p/bwIpYnBQiE

其他回答

简而言之,请使用fmt。Printf(“%T”,var1)或它在fmt包中的其他变体。

我整理了以下内容。

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)
        }
    }
}

去操场

我找到了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”

最好的方法是在谷歌中使用反射概念。 反映。TypeOf在给出包名的同时给出类型 reflect.TypeOf().Kind()给出下划线类型

你可以在运行时使用"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)
}