一共两种,直接看代码
import (
"fmt"
)
func main() {
//var i interface{} = "TT"
var i interface{} = 77
value, ok := i.(int)
if ok {
fmt.Printf("类型匹配int:%d
", value)
} else {
fmt.Println("类型不匹配int
")
}
if value, ok := i.(int); ok {
fmt.Println("类型匹配整型:%d
", value)
} else if value, ok := i.(string); ok {
fmt.Printf("类型匹配字符串:%s
", value)
}
}