douhuan4699 2014-02-06 01:14 采纳率: 100%
浏览 47
已采纳

我要如何输入

I'd like to get (reflect)type from type name.
http://play.golang.org/p/c-9IpSafx0

package main

import (
    "fmt"
    "reflect"
)

type Name string

func main() {
    fmt.Println("Hello, playground")

    var name Name = "Taro"
    fmt.Println(name)

    fmt.Println(getType(name))
    // fmt.Println(getType(Name))  // want to same as getType(name)
}

func getType(v interface{}) reflect.Type {
    return reflect.TypeOf(v)
}

How do I rewrite getType function.

  • 写回答

2条回答 默认 最新

  • dongshanxun6479 2014-02-06 01:50
    关注

    There is no way to pass a type as an argument to a function in Go, so what you ask is not possible. If you want to use the reflect module to work with types, you will need to have a value as a starting point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?