go chapter 4

https://www.cnblogs.com/chase-wind/p/5644838.html

空接口可以指向任何数据对象,所以可以使用interface{}定义任意类型变量,同时interface{}也是类型安全的。

变长int类型的参数

Func f(args ...int){
    For _,arg :=range args{
        fmt.Println(arg)
    }
}

  

变长不定类型的参数, 判断参数类型  

arg.(type)只能在switch中使用

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 int64:
                fmt.Println(arg, "is an int64 value.")
            default:
                fmt.Println(arg, "is an unknown type.")
         }
    }
}

MyPrintf(2, "Go", 8, "language")

原文地址:https://www.cnblogs.com/webglcn/p/9402429.html