Recently the company project, changed to Golang below to develop, met the problem of Redis storage chain list, troubled me for a few days, back to the heart, read a bit of source code, found the official example, and finally the wool out of the sheep
c, err := dial() if err != nil {
panic(err)
}
defer c.Close() var p1, p2 struct {
Title string `redis:"title"`
Author string `redis:"author"`
Body string `redis:"body"`
}
p1.Title = "Example" p1.Author = "Gary" p1.Body = "Hello" if _, err := c.Do("HMSET", redis.Args{}.Add("id1").AddFlat(&p1)...); err != nil {
panic(err)
}
m := map[string]string{ "title": "Example2", "author": "Steve", "body": "Map",
} if _, err := c.Do("HMSET", redis.Args{}.Add("id2").AddFlat(m)...); err != nil {
panic(err)
} for _, id := range []string{"id1", "id2"} {
v, err := redis.Values(c.Do("HGETALL", id)) if err != nil {
panic(err)
} if err := redis.ScanStruct(v, &p2); err != nil {
panic(err)
}
fmt.Printf("%+v\n", p2)
}
Golang Redigo hmset hset problem