The thing about the SCAN command is that it does not just return a bunch of keys, but it returns the number of the "iterator" that you should put in the next SCAN call. therefore, the structure of the answer can be seen as
[ iterator, [k1, k2, ... k10] ]
You start with a call SCAN 0and in consecutive calls you need to call SCAN <iterator>.
Doing this with redigo is as follows (my error handling is wrong, but it just shows the idea):
iter := 0
var keys []string
for {
if arr, err := redis.MultiBulk(conn.Do("SCAN", iter)); err != nil {
panic(err)
} else {
iter, _ = redis.Int(arr[0], nil)
keys, _ = redis.Strings(arr[1], nil)
}
fmt.Println(keys)
if iter == 0 {
break
}
}
source
share