我正在使用 Redigo Redis 库并尝试 json.Marshal 我从排序集中获得的结果,但我得到如下结果:"eyJmcm9tSWQiOjEsInRvSWQiOjUsInR5cGUiOjMsInBvc3RJZCI6MSwiY29tbWVudElkIjo0NCwiY3JlYXRlZFRpbWUiOjE0NjMxNTY0ODQsImlzVmlld2VkIjpmYWxzZSwidXNlcm5hbWUiOiJBZG1pbiIsImltYWdlIjoiaHc2ZE5EQlQtMzZ4MzYuanBnIn0="当我应该得到这个时:"{"fromId":5,"toId":1,"type":3,"postId":4,"commentId":49,"createdTime":1463161736,"isViewed":false,"username":"Alexander","image":"JZIfHp8i-36x36.png"}"我有一个通知结构type Notification struct { FromId int64 `json:"fromId"` ToId int64 `json:"toId"` OfType int64 `json:"type"` PostId int64 `json:"postId"` CommentId int64 `json:"commentId"` CreatedTime int64 `json:"createdTime"` IsViewed bool `json:"isViewed"` FromUsername string `json:"username"` FromImage string `json:"image"`}func New() *Notification { return &Notification{ CreatedTime: time.Now().Unix(), }}它有一个方法可以将一串 json 保存到 Redis 排序集中。func (n *Notification) Create(pool *redis.Pool, multicast chan<- []byte) error { var err error n.FromUsername, err = validation.FilterUsername(n.FromUsername) if err != nil { return err } // We can use the same validation as for a username here. n.FromImage, err = validation.FilterUsername(n.FromImage) if err != nil { return err }}这一切都很好。但后来我想获取这些结果,然后作为 json 格式的字符串数组发送到客户端。我保存在排序集中的相同 json 格式的字符串。我正在做这样简单的事情。func ByUserId(userId int64, pool *redis.Pool) (interface{}, error) { key := fmt.Sprintf("user:%d:notification", userId) c := pool.Get() defer c.Close() c.Send("ZREVRANGE", key, 0, -1) c.Flush() return c.Receive()}但它不起作用。我能做什么?
1 回答
莫回无
TA贡献1865条经验 获得超7个赞
函数ByUserId可以写成:
func ByUserId(userId int64, pool *redis.Pool) ([]string, error) {
key := fmt.Sprintf("user:%d:notification", userId)
c := pool.Get()
defer c.Close()
return redis.Strings(c.Do("ZREVRANGE", key, 0, -1))
}
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报
0/150
提交
取消