go-可变参数

今天在尝试用go写一个简单的orm的时候 发现 在调用可变参数函数时,不是总能使用省略号将一个切片展开,有时候编译器可能会报错 再此用几个简单的例子作为说明

当不太确定数据类型的时候我们通常采用空接口

tests1(789)
fmt.Println("-------------")
tests1("789")
func tests1(arg interface{}) {
    fmt.Println("value:", arg)
    fmt.Println("type:", reflect.TypeOf(arg).Name())
}

输出结果

value: 789
type: int
-------------
value: 789
type: string

在使用相同类型的可变入参时

tests([]string{"4", "5", "6"}...)
func tests(args ...string) {
    for i, v := range args {
        fmt.Println(i, "----", v)
    }
}

输出结果

0 ---- 4
1 ---- 5
2 ---- 6

当使用interface{}作为可变入参时

func testParams(args ...interface{}) {
    for i, v := range args {
        if s, ok := v.(string); ok {
            fmt.Println("----", s)
        }
        if s, ok := v.([]string); ok {
            for i, v := range s {
                fmt.Println(i, "[]----", v)
            }
        }
        fmt.Println(i, v)
    }
}

出现错误

cannot use []string literal (type []string) as type []interface {} in argument to testParams      

当看到这里时候答案已经露出水面了
这里提供两种解决方案

第一种方法

s := []string{"4", "5", "6"}
var d []interface{} = []interface{}{s[0], s[1], s[2]}
testParams(d...)

结果

---- 4
0 4
---- 5
1 5
---- 6
2 6

第二种方法

s := []string{"4", "5", "6"}
var d []interface{}
d = append(d, s)
testParams(d...)

结果

0 []---- 4
1 []---- 5
2 []---- 6
0 [4 5 6]

总结: 在使用interface{}作为可变入参时 传入的参数要做类型转换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值