我有以下一段代码,我在其中尝试将数组添加到 redis 集中,但它给了我一个错误。
package main
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis"
)
type Info struct {
Name string
Age int
}
func (i *Info) MarshalBinary() ([]byte, error) {
return json.Marshal(i)
}
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
ReadTimeout: -1,
})
pong, err := client.Ping().Result()
fmt.Print(pong, err)
infos := [2]Info{
{
Name: "tom",
Age: 20,
},
{
Name: "john doe",
Age: 30,
},
}
pipe := client.Pipeline()
pipe.Del("testing_set")
// also tried this
// pipe.SAdd("testing_set", []interface{}{infos[0], infos[1]})
pipe.SAdd("testing_set", infos)
_, err = pipe.Exec()
fmt.Println(err)
}
我得到错误can't marshal [2]main.Info (implement encoding.BinaryMarshaler)
我还尝试将每个信息转换为[]byte
并传递[][]byte...
给SAdd
但相同的错误。我将如何以惯常的方式做到这一点?