码迷,mamicode.com
首页 > 其他好文 > 详细

go chapter 4 - 不定长参数

时间:2018-08-01 18:26:18      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:cas   htm   ase   unknown   blog   win   case   接口   定长参数   

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

 

go chapter 4 - 不定长参数

标签:cas   htm   ase   unknown   blog   win   case   接口   定长参数   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!