Using Redis in Go

2014-12-01 ·

Install redigo, one of recommended Go clients for the Redis database (its API Reference).

go get github.com/garyburd/redigo/redis

Open the connection to Redis instance

c, err := redis.Dial("tcp", ":6379")
if err != nil {
    panic(err)
}
defer c.Close()

Set values

c.Do("SET", "foo", "This is foo")
c.Do("HSET", "bar", "1", "First bar")

Read values

val1, err := redis.String(c.Do("GET", "foo"))
if err != nil {
    fmt.Println("key not found")
}

val2, err := redis.String(c.Do("HGET", "bar", "1"))
if err != nil {
    fmt.Println("key not found")
}

The snippet is available here. Don't forget to have a Redis instance running on port 6379.

Copyright © 2018 All rights reserved.
Powered by Kulfon.