首页 > 其他 > 详细

go chapter 4 - 不定长参数

时间:2018-08-01 17:56:05      阅读:191      评论:0      收藏:0      [点我收藏+]

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 - 不定长参数

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!