美文网首页
Golang判断数据类型和获取数据类型

Golang判断数据类型和获取数据类型

作者: 懒人程序猿 | 来源:发表于2020-04-01 09:57 被阅读0次

使用.(type)及reflect.TypeOf()

func main() {
    demo.TypeOf(1)
    demo.TypeOf("golang")
    demo.TypeOf(true)
    demo.TypeOf(1.2)
}

func TypeOf(param interface{}) {
    // 打印数据类型
    fmt.Println(param, reflect.TypeOf(param))
    // 判断数据类型
    switch param.(type) {
    case int:
        fmt.Println("int")
    case string:
        fmt.Println("string")
    case bool:
        fmt.Println("bool")
    default:
        fmt.Println("other")
    }
}
image.png

相关文章

网友评论

      本文标题:Golang判断数据类型和获取数据类型

      本文链接:https://www.haomeiwen.com/subject/atkouhtx.html