Redis is an in-memory data store often used as a cache or database. Go pairs well with Redis for services and workers. This guide uses the maintained client github.com/redis/go-redis/v9 with context.Context for calls like Ping, Set, Get, and HSet. Install Redis first—for example, follow install Redis on Rocky Linux 9—then create a small module on your machine.
Tested with Go 1.24 on Linux against Redis 7.x locally. Examples use
go-redis/v9and require a runningredis-server.
Project layout and dependency
From an empty directory:
mkdir go-redis-app && cd go-redis-app
go mod init example.com/go-redis-app
go get github.com/redis/go-redis/v9Create main.go for the snippets below, or split into files as your app grows. For a larger API, see CRUD REST API with Redis in Go.
Connect with Ping
Set Addr to your instance (default localhost:6379). If Redis requires a password, set Password in redis.Options. Use DB to pick the logical database index.
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func main() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // set when Redis uses AUTH
DB: 0,
})
if err := rdb.Ping(ctx).Err(); err != nil {
fmt.Println("Redis:", err)
return
}
fmt.Println("Redis is working.")
}Run with go run . after Redis is up. Wrong or missing passwords typically surface as NOAUTH Authentication required.
Set and Get
package main
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
func main() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 1})
if err := rdb.Set(ctx, "greeting", "hello", time.Minute).Err(); err != nil {
panic(err)
}
val, err := rdb.Get(ctx, "greeting").Result()
if err != nil {
panic(err)
}
fmt.Println(val)
}The third argument to Set is a TTL (0 means no expiry). After one minute in this example, Get returns redis.Nil when the key has expired.
Hash example (HSet / HMGet)
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func main() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 0})
err := rdb.HSet(ctx, "user:1", map[string]interface{}{
"username": "amitkumar",
"email": "amit@example.com",
}).Err()
if err != nil {
panic(err)
}
vals, err := rdb.HMGet(ctx, "user:1", "username", "email").Result()
if err != nil {
panic(err)
}
fmt.Println(vals[0], vals[1])
}Summary
Use github.com/redis/go-redis/v9 with context.Background() (or a request-scoped context), configure redis.NewClient, verify with Ping, then use Get/Set, TTLs, and HSet/HMGet for hashes. Match password and DB index to your server. The go install toolchain uses different TLS and proxy settings than your app’s net/http client; fixing module download TLS is separate from Redis client code.

