Golang redisgo uses lua scripts to get hash values in batches

problem description

use the redisgo command: redis.NewScript (keyCount, LuaSrcript) .Do (conn, args.)

but what is returned is [] interface {} how to convert a two-dimensional array?

related codes

luaScript: = "local rst= {}; for I in pairs (KEYS) do rst v in pairs (KEYS) do rst [I] = redis.call ("hgetall", v) end; return rst"
redis.NewScript (2, LuaSrcript) .Do (conn, "user:1", "user:2")

Apr.02,2021

to convert a to interface I , you can do this a. (I) .

redis returns the relationship between value type and go type

Redis type              Go type
error                   redis.Error
integer                 int64
simple string           string
bulk string             []byte or nil if value not present.
array                   []interface{} or nil if value not present.

reference code

package main

import (
    "fmt"

    "github.com/gomodule/redigo/redis"
)

func main() {
    c, _ := redis.Dial("tcp", ":6379")
    var script = redis.NewScript(2, `
        local rst={};
        for i,v in pairs(KEYS) do
          rst[i]=redis.call('get', v)
        end;
        return rst`)
    reply, _ := script.Do(c, "key1", "key2")

    array := reply.([]interface{})
    for i, item := range array {
        if item != nil {
            //  key1key2  bulk string
            fmt.Println(i, string(item.([]byte)))
        }
    }
}
Menu