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.")
         }
    }
}

 

posted @ 2018-08-02 15:10  webglcn  阅读(84)  评论(0编辑  收藏  举报