类型断言,由于接口是一般类型,不知道具体类型,
如果要转成具体类型,就需要使用类型断言。
示例:
var x interface{}
var b float32 = 2.1
x = b
if y, ok := x.(float32); ok {
fmt.Printf("y 的类型是 %T; 值是%v ", y, y)
}
示例:
func TypeJudge(items... interface{} ) {
for index, x := range items {
switch x.(type) {
case bool:
fmt.Printf("第%v个参数类型是bool,值为%v\n", index, x)
case int:
fmt.Printf("第%v个参数类型是int,值为%v\n", index, x)
case float32:
fmt.Printf("第%v个参数类型是float32,值为%v\n", index, x)
case float64:
fmt.Printf("第%v个参数类型是float64,值为%v\n", index, x)
case int8, int32, int64:
fmt.Printf("第%v个参数类型是整数, x)
}
}
}