判断变量类型
package main
import (
"fmt"
"reflect"
)
func main(){
// 获取变量类型
str := "ASDFG"
num := 123
arr := []int{1,2,3}
fmt.Println("方法一")
fmt.Printf("str type = %T \n",str)
fmt.Printf("num type = %T \n",num)
fmt.Printf("arr type = %T \n",arr)
fmt.Println("方法二")
fmt.Printf("str type = %v \n",reflect.TypeOf(str))
fmt.Printf("num type = %v \n",reflect.TypeOf(num))
fmt.Printf("arr type = %v \n",reflect.TypeOf(arr))
}
结果:
方法一
str type = string
num type = int
arr type = []int
方法二
str type = string
num type = int
arr type = []int

浙公网安备 33010602011771号