Quick Start: Your First Redigo Program

This guide will walk you through writing a simple Go program that connects to a Redis server, sets a key, retrieves it, and prints the result to the console. It's a "Hello, World!" for Redigo.

Prerequisites

  1. Go: You must have Go 1.17 or later installed.
  2. Redis: You need a Redis server running on its default port (6379) on localhost. You can install it via a package manager or run it with Docker:
    docker run --name some-redis -p 6379:6379 -d redis

Step 1: Set up Your Project

Create a new directory for your project and initialize a Go module.

mkdir redigo-hello
cd redigo-hello
go mod init example.com/redigo-hello

Next, add Redigo as a dependency:

go get github.com/gomodule/redigo/redis

Step 2: Write the Code

Create a file named main.go and add the following code:

package main

import (
    "fmt"
    "log"

    "github.com/gomodule/redigo/redis"
)

func main() {
    // 1. Establish a connection to the Redis server.
    conn, err := redis.Dial("tcp", "localhost:6379")
    if err != nil {
        log.Fatalf("Failed to connect to Redis: %v", err)
    }
    // 2. Ensure the connection is closed when the function exits.
    defer conn.Close()

    // 3. Define the key and value we want to set.
    key := "message"
    value := "Hello from Redigo!"

    // 4. Send the SET command to Redis.
    // The first return value is a reply, which we ignore for SET.
    _, err = conn.Do("SET", key, value)
    if err != nil {
        log.Fatalf("Failed to set key '%s': %v", key, err)
    }

    fmt.Printf("Successfully set key '%s' to '%s'\n", key, value)

    // 5. Send the GET command and use a reply helper to convert the result to a string.
    retrievedValue, err := redis.String(conn.Do("GET", key))
    if err != nil {
        log.Fatalf("Failed to get key '%s': %v", key, err)
    }

    // 6. Print the retrieved value.
    fmt.Printf("Retrieved value for key '%s': %s\n", key, retrievedValue)
}

Step 3: Run the Program

Execute the code from your terminal:

go run main.go

You should see the following output:

Successfully set key 'message' to 'Hello from Redigo!'
Retrieved value for key 'message': Hello from Redigo!

Code Explanation

  1. redis.Dial: Opens a TCP connection to the Redis server at the specified address.
  2. defer conn.Close(): This is a crucial step. It ensures that the connection is properly closed when the main function finishes, releasing the network resources.
  3. conn.Do(command, args...): This is the primary method for executing commands. It sends the command and its arguments to Redis and returns the server's reply and any error that occurred.
  4. redis.String(...): This is a reply helper. The Do method returns interface{}, error. Helpers convert the generic interface{} reply to a specific Go type. Here, we convert the bulk string reply from GET into a Go string.

Congratulations! You've successfully used Redigo to interact with Redis. To learn more, explore the pages in the Usage Guide.