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")
}
}
![](https://img.haomeiwen.com/i18212768/9ae50e3db18632fc.png)
image.png
本文标题:Golang判断数据类型和获取数据类型
本文链接:https://www.haomeiwen.com/subject/atkouhtx.html
网友评论