Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where is a basic example? #461

Closed
Serhioromano opened this issue Apr 3, 2020 · 7 comments
Closed

Where is a basic example? #461

Serhioromano opened this issue Apr 3, 2020 · 7 comments

Comments

@Serhioromano
Copy link

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.

@oliver006
Copy link

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

@Serhioromano
Copy link
Author

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.

@oliver006
Copy link

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.

@Serhioromano
Copy link
Author

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?

@stevenh
Copy link
Collaborator

stevenh commented Apr 3, 2020

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 stevenh closed this as completed Apr 5, 2020
@Serhioromano
Copy link
Author

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

@stevenh
Copy link
Collaborator

stevenh commented Apr 5, 2020

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

vals, err := redis.Values(conn.Do("HGET", keys...))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants