最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Golang中对interface{}做type assertion和type switch学习笔记
时间:2022-06-25 04:43:21 编辑:袖梨 来源:一聚教程网
interface{}是一个通用类型,可以储存任意类型的值。如下方法来获取值的实际类型:
如果你比较确定类型可以使用type assertion:
var num interface{} = 100
if val,ok := num.(int); ok {
fmt.Println(val)
}
如果你不确定interface{}的具体类型,使用type switch:
var str interface{} = "abc"
switch v := str.(type) {
case string:
fmt.Println(v)
case int32, int64:
fmt.Println(v)
default:
fmt.Println("unknown")
}
相关文章
- SimilarWeb官网入口 - 2026最新在线访问地址 06-22
- 蜜疯直播app如何关闭听筒模式 06-22
- 抖音来客官网登录入口 - 2026最新版官方平台 06-22
- 《淘宝》510周年庆刮刮乐免单活动入口 06-22
- 觅长生气道找谁 06-22
- 国家电网招聘报名官网入口 - 2026年校园招聘通道 06-22