0

I'm just fetching json from a Redis db and trying to append it to an array.

In Javascript I would do something like this:

var myarray = [];

//blah blah contact Redis and get the response

myarray.push(redisresponse);

I'm having trouble figuring out how to do that in Go.

Library suggestions welcome!

2
  • Can you share the code you have in Go even if it's incorrect? This library is most popular I believe: github.com/garyburd/redigo Commented Feb 28, 2017 at 23:21
  • @squiguy I don't really have anything having to do with the array and Redis response, I don't know where to start. I get the response and I can print it out, that's about it. edit: I'm using gopkg.in/redis.v5 which is working pretty good, I just don't know how to push json to an array in Go. Commented Feb 28, 2017 at 23:23

1 Answer 1

2

Let's say you want to get a string response from Redis. Using the redigo library you can send a command and receive the response back using it's helper methods.

This is a snippet of how you can do that:

import "github.com/garyburd/redigo/redis"

someCap := 10 // Make the slice however large you need it.
myarray := make([]string, someCap)
redisConn, err := redis.Dial("tcp" "your_redis_host:port")
if err != nil {
    // Handle your error accordingly.
}
defer redisConn.Close()

resp, err := redis.String(redisConn.Do("GET", "some_key"))
if err != nil {
    // Handle your error accordingly.
}
myarray = append(myarray, resp)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.