In this demonstration you’ll learn how to use Go with Redis. First and foremost go get garyburd/redigo for connecting with the Redis-server.

$ go get

Next make sure you have Redis installed and the redis-server running.

$ redis-server

Connecting to Redis

The function redis.Dial() returns a redis.Conn type that is used to send commands to the redis-server and get back a response.

package main

import "fmt"
import ""

func main() {
    // Connect to the default port
    conn, err := redis.Dial("tcp", ":6379")
    if err != nil {
    defer conn.Close()

    // Send Redis a ping command and wait for a pong
    pong, _ := conn.Do("PING")
    // Output: PONG

Storing and retrieving data

Try setting a string

ok, _ := conn.Do("SET", "name", "john")

// Output: OK

Now try retrieving that value

name, _ := conn.Do("GET", "name")

// Output: [106 111 104 110]

Oh no! That’s not correct.

Sometimes you have to explicitly convert the response to the correct type using either type assertion or the redigo helper functions.

// Notice the redis.String() wrapper
name, _ := redis.String(conn.Do("GET", "name"))

// Output: john

Encoding into json

You can store a more complex data type such as a struct by encoding it into json.

// Encode the struct as json and save in a list 

type User struct {
    Name string
    Age  int

user1 := User{"John", 22}

encoded, _ := json.Marshal(user1)

conn.Do("LPUSH", "users", encoded)

Now decode it into a struct

// Grab out of redis and decode into a struct 

type User struct {
    Name string
    Age  int

var unencoded *User 

// Grabs the entire users list into an []string named users
users, _ := redis.Strings(conn.Do("LRANGE", "users", 0, -1))
// Grab one string value and convert it to type byte
// Then decode the data into unencoded
json.Unmarshal([]byte(users[0]), &unencoded)

// Output: John

Lastly, you should use a Pool in your web app

Your http handlers should grab a connection from the Pool and close it when done.

package main

import ""
import "net/http"

// Global pool that handlers can grab a connection from
var pool = newPool()

// Pool configuration
func newPool() *redis.Pool {
	return &redis.Pool{
		MaxIdle:   80,
		MaxActive: 12000,
		Dial: func() (redis.Conn, error) {
			return redis.Dial("tcp", ":6379")

func home(res http.ResponseWriter, req *http.Request) {
	// Grab a connection and make sure to close it with defer 
	conn := pool.Get()
	defer conn.Close()

	pong, _ := redis.Bytes(conn.Do("PING"))

func main() {
	http.HandleFunc("/", home)
	http.ListenAndServe(":8080", nil)