Redigo: Where is a basic example?

Created on 3 Apr 2020  路  7Comments  路  Source: gomodule/redigo

Do I miss anything? I can't find a basil example just connect and set value and then get the same value. Without pool or cluster. I cannot figure out even how to connect to DB.

I know that for someone pro in Golang that all is obvious, but I am first week Go developer and not everything is obvious and I need examples.

All 7 comments

The examples in godoc cover this pretty well.
https://godoc.org/github.com/gomodule/redigo/redis#pkg-examples

Here's how to establish a new connection:
https://godoc.org/github.com/gomodule/redigo/redis#example-Dial

And this snippet shows you how to first write and then read a string:
https://godoc.org/github.com/gomodule/redigo/redis#example-String

Thank you. What I want is to get list rows that have keys p_1:*

Here is my code

       conn, err := redis.DialURL("redis://172.17.0.1:6379")
    defer conn.Close()

    if err != nil {
        panic(err.Error())
    }

    res, err := conn.Do("KEYS", "p_1:*")

    if err != nil {
        panic(err.Error())
    }

    res, err = redis.Values(conn.Do("MGET", res))
    if err != nil {
        panic(err.Error())
    }

    fmt.Println(res)

res has something that looks like array of byte when I get keys. But I cannot get those keys values.

Look at the docs that I linked to in my earlier post, when reading a string with "GET" you should use the conversion function redis.String() to convert the bytes result into a Golang string.

First, let me say how I am thankful for your time.

I looked at docs but I can't get it. When I get keys I have a result. But when I MGET I have nil

I do not use GET. I use KEYS and that supposed to return array so perhaps I have to convert KEYS to something else.

Could you look at my code and tell what is missing?

You want:

    keys, err := redis.Strings(conn.Do("KEYS", "p_1:*"))
    if err != nil {
        panic(err.Error())
    }

    for _, k := range keys {
        fmt.Println(k)
    }

@stevenh yes that 褕 can do. 褕 can list of keys. I cannot convert this list to appropriate format to use MGET

no conversion needed just pass the result using variadic expansion e.g.

vals, err := redis.Values(conn.Do("HGET", keys...))
Was this page helpful?
0 / 5 - 0 ratings

Related issues

V2Vz picture V2Vz  路  4Comments

garyburd picture garyburd  路  23Comments

smotes picture smotes  路  18Comments

lukasmalkmus picture lukasmalkmus  路  18Comments

lovegnep picture lovegnep  路  18Comments