Golang反射type和kind有什么区别?
一、前言
1.1 Kind和Type区别
1.1.1 Kind
种类(Kind)指的是对象归属的品种,在 reflect 包中有如下定义:
type Kind uintconst (Invalid Kind = iota // 非法类型Bool // 布尔型Int // 有符号整型Int8 // 有符号8位整型Int16 // 有符号16位整型Int32 // 有符号32位整型Int64 // 有符号64位整型Uint // 无符号整型Uint8 // 无符号8位整型Uint16 // 无符号16位整型Uint32 // 无符号32位整型Uint64 // 无符号64位整型Uintptr // 指针Float32 // 单精度浮点数Float64 // 双精度浮点数Complex64 // 64位复数类型Complex128 // 128位复数类型Array // 数组Chan // 通道Func // 函数Interface // 接口Map // 映射Ptr // 指针Slice // 切片String // 字符串Struct // 结构体UnsafePointer // 底层指针)
二、举例说明
import ("fmt""reflect")type cat struct {name string}func main() {typeCat := reflect.TypeOf(cat{})fmt.Println(typeCat.Name(), typeCat.Kind())var a inttypeA := reflect.TypeOf(a)fmt.Println(typeA.Name(), typeA.Kind())}
输出结果:
cat structint int
THE END






