Redigo: A Go Client for Redis
Redigo is a high-performance Go client for the Redis and Valkey databases. It provides a simple, flexible, and efficient API for interacting with Redis from your Go applications.
The client is designed to be lightweight, with the Go distribution being its only dependency. It offers both low-level control for maximum performance and high-level helpers for convenience.
Key Features
Redigo provides a comprehensive set of features for Redis development:
- A Simple, Print-like API: Execute any Redis command with a familiar, variadic function interface:
conn.Do("SET", "key", "value")
. - Pipelining: Batch multiple commands to improve performance by reducing network round-trips. This includes support for pipelined transactions.
- Publish/Subscribe: Full support for Redis Pub/Sub, with a convenient
PubSubConn
wrapper to handle message and subscription types. - Connection Pooling: A built-in, thread-safe connection pool (
redis.Pool
) to manage connections efficiently in concurrent applications. - Lua Scripting: An elegant
Script
helper that optimistically usesEVALSHA
and automatically falls back toEVAL
if the script is not cached, simplifying script management. - Reply Helpers: A rich set of functions (
redis.String
,redis.Int
,redis.Values
,redis.ScanStruct
) to easily convert Redis replies into native Go types.
Quick Example
Here's a quick look at how to use Redigo to set and get a key from Redis.
package main
import (
"fmt"
"log"
"github.com/gomodule/redigo/redis"
)
func main() {
// Connect to the default Redis server.
c, err := redis.Dial("tcp", ":6379")
if err != nil {
log.Fatalf("Could not connect: %v", err)
}
defer c.Close()
// Set a key
_, err = c.Do("SET", "greeting", "Hello, Redigo!")
if err != nil {
log.Fatalf("Failed to set key: %v", err)
}
// Get the key and convert the reply to a string.
greeting, err := redis.String(c.Do("GET", "greeting"))
if err != nil {
log.Fatalf("Failed to get key: %v", err)
}
fmt.Println(greeting)
// Output: Hello, Redigo!
}
For more detailed instructions, head to the Installation and Quick Start guides.