I am using go-redis package. I fired below query:
rd.LLen("queue_1")
Which returning me below result with type *redis.IntCmd
:
llen queue_1: 100001
Count is perfect but I want only count with type of int
. Can anyone help?
the LLen
func looks like: func (c *cmdable) LLen(key string) *IntCmd
So it returns an IntCmd
type. IntCmd
provides this function: func (cmd *IntCmd) Val() int64
So you can do e.g.
cmd := rd.LLen("queue_1")
i := cmd.Val() //i will be an int64 type
Use the Result or Val method. As documented, these return the int64
value returned from the command.
The documentation for IntCmd shows the following:
func (cmd *IntCmd) Result() (int64, error)
func (cmd *IntCmd) Val() int64
So it seems you can either get the int64 value and any error to check, or just the int64 value. Is there a reason this is not sufficient?
You can call them as follows:
// 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())