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 Ubuntu or 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 (see create a Go module if go mod init is new to you):
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, or the primary endpoint from Amazon ElastiCache for Redis when the cluster runs in AWS). 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.
References
Frequently Asked Questions
Which Go Redis client should I use today?
Use the supported module github.com/redis/go-redis/v9 with a Go module. The older github.com/go-redis/redis v6 path is legacy; new projects should import redis/go-redis/v9.
Why does Ping return NOAUTH Authentication required?
Your Redis server has requirepass or ACL auth enabled. Pass the correct Password in redis.Options, or disable auth only on local dev machines you control.
Do I need context.Context for go-redis v9?
Yes. Commands are methods like Ping(ctx), Get(ctx, key), and Set(ctx, key, value, ttl) where the first argument is a context.
How do I store a hash from Go?
Use HSet with a map of fields, then HMGet or HGetAll to read. Prefer explicit field names rather than relying on implicit ordering in your app logic.
Where can I learn a full REST API with Redis?
The CRUD API walkthrough linked in the project layout section extends these primitives into a full REST service.

