Golang之interface{}转换为其他类型

2,077 阅读1分钟

interface转换成具体类型调用方式:interfaceVar.(具体类型)
原理:断言
断言成功返回true,失败false
代码示例:

value, ok := a.(string)
if !ok {
    fmt.Println("failed")
     return
}
fmt.Println("The value is ", value)