我正在使用 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 關注
- 110 瀏覽
添加回答
舉報
0/150
提交
取消