Use Go as a Redis Client (go-redis, Ping, Get, Set, Hashes)

Connect a Go program to Redis with github.com/redis/go-redis/v9, Ping with context, Set and Get strings, and store hashes; run Redis locally and match password and DB settings.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Use Go as a Redis Client (go-redis, Ping, Get, Set, Hashes)

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/v9 and require a running redis-server.


Project layout and dependency

From an empty directory:

text
mkdir go-redis-app && cd go-redis-app
go mod init example.com/go-redis-app
go get github.com/redis/go-redis/v9

Create 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.

go
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

go
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)

go
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

1. 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.

2. 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.

3. 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.

4. 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.

5. Where can I learn a full REST API with Redis?

See the GoLinuxCloud walkthrough for a CRUD API backed by Redis in Go for a larger pattern.
Antony Shikubu

Systems Integration Engineer

Highly skilled software developer with expertise in Python, Golang, and AWS cloud services.