Golang reflect.Kind()函数及示例
reflectKindreflect.TypeKind
基本用法
reflect.Kind()
package main
import (
"fmt"
"reflect"
)
type MyInt int
func main() {
var num MyInt = 1
fmt.Println(reflect.TypeOf(num).Kind()) // 输出 int
}
MyIntintvar num MyInt=1reflect.TypeOf(num)Kind()
intreflect.Kind()
复杂类型
reflect.Kind()
package main
import (
"fmt"
"reflect"
)
func main() {
s := []int{1, 2, 3}
fmt.Println(reflect.TypeOf(s).Kind()) // 输出 slice
}
slicereflect.Kind()
reflect.Kind()
package main
import (
"fmt"
"reflect"
)
func main() {
m := map[string]int{"a": 1, "b": 2}
fmt.Println(reflect.TypeOf(m).Kind()) // 输出 map
}
mapreflect.Kind()
reflect.Kind()
自定义类型
reflect.Kind()MyIntreflect.Kind()