golang interface 转 string,int,float64

func interface2String(inter interface{}) {

    switch inter.(type) {

    case string:
        fmt.Println("string", inter.(string))
        break
    case int:
        fmt.Println("int", inter.(int))
        break
    case float64:
        fmt.Println("float64", inter.(float64))
        break
    }

}

func main() {
    interface2String("jack")
    interface2String(1)
    interface2String(12.223)
}
string jack
int 1
float64 12.223

参考:

https://www.yuque.com/docs/share/fc577a9b-90d0-4d8d-9a10-43ee14237b62

你可能感兴趣的:(Go)