判断变量类型

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

posted @ 2020-06-09 14:45  懶得取名  阅读(125)  评论(0)    收藏  举报