我正在使用go-redis包。我在下面的查询中触发:
rd.LLen("queue_1")
在下面返回我的结果是类型 *redis.IntCmd
:
llen queue_1: 100001
计数是完美的,但我只想用int
类型计数。谁能帮忙?
LLen
func看起来像:func (c *cmdable) LLen(key string) *IntCmd
因此,它返回一个IntCmd
类型。 IntCmd
提供此功能:func (cmd *IntCmd) Val() int64
所以你可以做例如
cmd := rd.LLen("queue_1")
i := cmd.Val() //i will be an int64 type
IntCmd 的文档显示以下内容:
func (cmd *IntCmd) Result() (int64, error)
func (cmd *IntCmd) Val() int64
因此,您似乎可以获取 int64 值和要检查的任何错误,或者只获取 int64 值。这还不够吗?
您可以按如下方式调用它们:
// Get the value and check the error.
if val, err := res.Result(); err != nil {
panic(err)
} else {
fmt.Println("Queue length: ", val)
}
// Get the value, but ignore errors (probably a bad idea).
fmt.Println("Queue length: ", res.Val())
使用 Result 或 Val 方法。如前所述,它们返回从命令返回的int64
值。