起因: 使用的Redis的时候,需要使用LPUSH 往一个key中一次写入多个value
我使用的是garyburd/redigo 这个库
函数定义如下
// Do sends a command to the server and returns the received reply.
func Do(commandName string,args ...interface{}) (reply interface{},err error)
显然函数是可变长参数
### 解决方法
#### 1. 列表
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
func main() {
dialOption1 := redis.DialDatabase(0)
dialOption2 := redis.DialPassword("xxxx")
rs,err := redis.Dial("tcp","127.0.0.1:6379",dialOption1,dialOption2)
if err != nil {
fmt.Println(err)
}
// redis的key是 "mykey"
args := []interface{}{"mykey"}
args = append(args,10,20)
args = append(args,30)
count,err := redis.Int(rs.Do("LPUSH",args ...))
fmt.Println("count:",count)
}
#### 2. 字典
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
func main() {
dialOption1 := redis.DialDatabase(0)
dialOption2 := redis.DialPassword("xxxx")
rs,dialOption2)
if err != nil {
fmt.Println(err)
}
test_map := make(map[string]int)
test_map["zhangsan"] = 1
test_map["lisi"] = 2
test_map["wangwu"] = 3
// redis的key是 "my_hash"
args := []interface{}{"my_hash"}
for f,v := range test_map {
args = append(args,f,v)
}
str,err := redis.String(rs.Do("HMSET",args ...))
fmt.Println(str)
}