go chapter 7 - 类型

Posted webglcn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go chapter 7 - 类型相关的知识,希望对你有一定的参考价值。

任意类型

interface{}

 

遍历并判断类型

func MyPrintf(args ...interface{}) {
    for _, arg := range args {
        switch arg.(type) {
            case int:
                fmt.Println(arg, "is an int value.")
            case string:
                fmt.Println(arg, "is a string value.")
            case float64:
                fmt.Println(arg, "is an float64 value.")
            case bool:
                fmt.Println(arg, "is an bool value.")
            default:
                fmt.Println(arg, "is an unknown type.")
         }
    }
}

 

以上是关于go chapter 7 - 类型的主要内容,如果未能解决你的问题,请参考以下文章