如何将类型值传递给reflect.type Golang类型的变量

如何将类型值传递给reflect.type Golang类型的变量,go,types,reflection,Go,Types,Reflection,我需要创建StructField,我需要在其中传递类型字段的reflect.Type值。我希望将其他类型(如reflect.Bool、reflect.Int)传递给函数,该函数将用于构造StructField。我无法使用下面的代码执行此操作 reflect.StructField{ Name: strings.Title(v.Name), Type: reflect.Type(reflect.String), Tag: r

我需要创建StructField,我需要在其中传递类型字段的reflect.Type值。我希望将其他类型(如reflect.Bool、reflect.Int)传递给函数,该函数将用于构造StructField。我无法使用下面的代码执行此操作

reflect.StructField{
            Name: strings.Title(v.Name),
            Type: reflect.Type(reflect.String),
            Tag:  reflect.StructTag(fmt.Sprintf(`xml:"%v,attr"`, v.Name)),
        }
因为它

Cannot convert an expression of the type 'Kind' to the type 'Type'
如何实现它?

是一种类型,因此表达式

reflect.Type(reflect.String)
会是一种类型。
reflect.String
的类型为,它未实现接口类型
reflect.Type
,因此转换无效

表示
字符串的
reflect.Type
值为:

reflect.TypeOf("")
通常,如果您有一个值,则可以使用函数获取任何(非接口)类型的
reflect.Type
描述符:

var x int64
t := reflect.TypeOf(x) // Type descriptor of the type int64
如果你没有一个值,这也是可能的。从键入的
nil
指针值开始,调用
Type.Elem()
以获取指向的类型:


要从
reflect.Kind
值创建
reflect.Type
值,您应该打开
reflect.Kind
并根据大小写初始化相应的
reflect.Type
。i、 e.
开关k{case reflect.Int64:返回reflect.TypeOf(Int64(0))…
。或者您可以使用与上述开关等效的映射:在Go中,您不能传递类型。还要注意,
reflect.Bool
不是一个类型,而是一个值。(不知道类型。Bool,不知道您暗示的
types
包是什么)@他可能指的是mkopriva。@icza可能是的。
t := reflect.TypeOf((*int64)(nil)).Elem()      // Type descriptor of type int64

t2 := reflect.TypeOf((*io.Reader)(nil)).Elem() // Type descriptor of io.Reader