您的位置 首页 技术

golang如何判断数据类型

使用Go的空接口:i.(type) 只能在switch中使用,函数没有返回值 func m_type(i interface{}) { switch i.(type) { case…

使用Go的空接口:i.(type) 只能在switch中使用,函数没有返回值

func m_type(i interface{}) {    switch i.(type) {    case string:        //...    case int:        //...    }    return}

使用反射:reflect.TypeOf(x)

package mainimport (    "fmt"    "reflect")func main() {    var x int32 = 20    fmt.Println("type:", reflect.TypeOf(x))}

总结:第一种方法需要先知道有几种类型,第二种可以对任意对象使用。

推荐学习《golang教程》

以上就是golang如何判断数据类型的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/24025.html

为您推荐

返回顶部