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

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

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

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


当前回答

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

其他回答

如果你想检测if表达式中的类型:

if str, ok := myvar.(string); ok {
    print("It's a string")
}

或者没有类型断言(可能会产生错误):

if reflect.TypeOf(myvar).String() == "string" {
    print("It's a string")
}

你可以像在这个游乐场一样使用:interface{}..(type)

package main
import "fmt"
func main(){
    types := []interface{} {"a",6,6.0,true}
    for _,v := range types{
        fmt.Printf("%T\n",v)
        switch v.(type) {
        case int:
           fmt.Printf("Twice %v is %v\n", v, v.(int) * 2)
        case string:
           fmt.Printf("%q is %v bytes long\n", v, len(v.(string)))
       default:
          fmt.Printf("I don't know about type %T!\n", v)
      }
    }
}

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

获取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 %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)
        }
    }
}

去操场