标签:map package his ack 否则 golang ace ... port
golang中Any类型使用及类型查询2.1 关于空接口的类型查询方式一,使用ok
package main
import "fmt"
//空接口可以接受任何值
//interface { }
func main() {
var v1 interface{ }
v1 = 6.78
//赋值一个变量v判断其类型是否为float64,是则为真,否则,为假
if v, ok := v1.(float64);ok{
fmt.Println(v, ok)
}else {
fmt.Println(v,ok)
}
}
2.2 关于空接口类型查询方式二,switch语句结合 var.type()
package main
import "fmt"
//空接口可以接受任何值
//interface { }
func main() {
var v1 interface{ }
v1 = "张三"
switch v1.(type) {
case float32:
case float64:
fmt.Println("this is float64 type")
case string:
fmt.Println("this is string type")
}
}
标签:map package his ack 否则 golang ace ... port
原文地址:http://blog.51cto.com/huwho/2307633