美文网首页
Go类型断言

Go类型断言

作者: 腿长袖子短 | 来源:发表于2021-09-05 15:14 被阅读0次
func TypeAssertion(arg ...interface{}){
    for _,v := range arg{
        switch reflect.TypeOf(v).Kind() {
        case reflect.String:
            fmt.Printf("arg %v type of string\n",v)
        case reflect.Int64:
            fmt.Printf("arg %v type of Int64\n",v)
        case reflect.Int32:
            fmt.Printf("arg %v type of Int32\n",v)
        case reflect.Int16:
            fmt.Printf("arg %v type of Int16\n",v)
        case reflect.Int8:
            fmt.Printf("arg %v type of Int8\n",v)
        case reflect.Int:
            fmt.Printf("arg %v type of Int\n",v)
        case reflect.Uint64:
            fmt.Printf("arg %v type of Uint64\n",v)
        case reflect.Uint32:
            fmt.Printf("arg %v type of Uint32\n",v)
        case reflect.Uint16:
            fmt.Printf("arg %v type of Uint16\n",v)
        case reflect.Uint8:
            fmt.Printf("arg %v type of Uint8\n",v)
        case reflect.Uint:
            fmt.Printf("arg %v type of Uint\n",v)
        case reflect.Bool:
            fmt.Printf("arg %v type of Bool\n",v)
        case reflect.Float64:
            fmt.Printf("arg %v type of Float64\n",v)
        case reflect.Float32:
            fmt.Printf("arg %v type of Float64\n",v)
        case reflect.Slice:
            fmt.Printf("arg %v type of Slice\n",v)
        case reflect.Map:
            fmt.Printf("arg %v type of Map\n",v)
        case reflect.Struct:
            fmt.Printf("arg %v type of Struct\n",v)
        case reflect.Chan:
            fmt.Printf("arg %v type of Chan\n",v)
        case reflect.Array:
            fmt.Printf("arg %v type of Array\n",v)
        case reflect.Func:
            fmt.Printf("arg %v type of Func\n",v)
        case reflect.Interface:
            fmt.Printf("arg %v type of Interface\n",v)
        case reflect.Complex64:
            fmt.Printf("arg %v type of Complex64\n",v)
        case reflect.Complex128:
            fmt.Printf("arg %v type of Complex128\n",v)
        case reflect.UnsafePointer:
            fmt.Printf("arg %v type of UnsafePointer\n",v)
        default:
            fmt.Printf("arg %v type of Unknow\n",v)
        }
    }
}

相关文章

  • 第04天(面对对象编程)_04

    16_接口的继承.go 17_接口转换.go 18_空接口.go 19_类型断言:if.go 20_类型断言:sw...

  • Go类型断言

    语法 <目标类型的值>,<布尔参数> := <表达式>.( 目标类型 ) // 安全类型断言<目标类型的值> :=...

  • go类型断言

    1.类型断言就是将接口类型的值(x),转换成类型(T)。格式为:x.(T);2.类型断言的必要条件就是x是接口类型...

  • Go类型断言

  • Go语言类型转换和类型断言

    Go语言的类型转换和类型断言: 类型转换在编译期完成,包括强制转换和隐式转换 类型断言在运行时确定,包括安全类型断...

  • go笔记 - 类型转换

    go 存在 4 种类型转换分别为:断言、强制、显式、隐式。 通常说的类型转换是指断言,强制在日常不会使用到、显示是...

  • Go语言类型断言简述

    参考:http://c.biancheng.net/view/4281.html 关键点: 类型断言是组什么的?用...

  • go语言的类型断言

    一、基本介绍 类型断言,由于接口是一般类型,不知道具体类型,如果要转成具体类型,就需要使用类型断言,具体的如下: ...

  • Go语言 类型转换,类型断言,类型开关

    类型转换Go语言中提供了一种不同类型但是相互兼容的可以相互转换的方式,这种方式是非常有用且安全的。非数值间相互转换...

  • 类型转换和类型断言

    go语言不允许隐式类型转换,而类型转换和类型断言的本质,就是把一个类型转换到另一个类型。 一、类型转换 1.语法:...

网友评论

      本文标题:Go类型断言

      本文链接:https://www.haomeiwen.com/subject/gymjwltx.html