【问题标题】:Too many arguments in call通话中的参数过多
【发布时间】:2023-03-23 16:25:01
【问题描述】:

我正在尝试使用 mysql2redis 将 db 数据加载到 redis 集群。

当我尝试接受的解决方案时,即

} else if e.Command == "HMSET" {
    // Build up a string slice to hold the key value pairs
    args := make([]string, 0, len(e.MapData) * 2)
    for k, v := range e.MapData {
        args = append(args, k, v)
    }
    _,err := redis.StringMap(client.Do("HMSET", e.Key, args...))
    checkErr(err, "hmset error:")
}

我得到以下异常,

too many arguments in call to client.Do
    have (string, string, []string...)
    want (string, ...interface {})

我是 Go 的新手。那么围棋老手可以看看这个并提出解决方案吗?

【问题讨论】:

    标签: go redis


    【解决方案1】:

    在 Go 中,您可以使用切片作为可变参数。但是,切片必须包含您需要传递给函数的所有参数。您也不能扩展切片并传递其他参数。

    因此你的代码应该是这样的:

    args := make([]interface{}, 0, len(e.MapData) * 2 + 1)
    args = append(args, e.Key)
    for k, v := range e.MapData {
        args = append(args, k, v)
    }
    _,err := redis.StringMap(client.Do("HMSET", args...))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2018-08-29
      相关资源
      最近更新 更多