Redis with Golang

Read more articles on Go, Redis
github logo
Github Repository
Code for this articles is available on GitHub
Redis with Golang

Subscribe

Get updated 1 - 2 times a month when new articles are published, no spam ever.

In this article you are going to learn how to setup and connect to Redis in Go.

Setting up a Redis Instance #

Use docker to quickly setup a redis container on you host machine.

docker run --name redis -p 6379:6379 -d redis

The above command will start a redis container and make it available on localhost:6379.

Setting up a Go Project #

Create a new go project.

mkdir go-basic-redis
cd go-basic-redis
go mod init github.com/username/go-basic-redis

Install the redis dependency (check for the latest version here).

go get github.com/redis/go-redis/v9

Connecting to Redis #

Create a main.go file and setup the redis client and send a PING to the redis server.

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

func main() {
	ctx := context.Background()

	redisClient := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	pingResult, err := redisClient.Ping(ctx).Result()
	if err != nil {
		panic(err)
	}

	fmt.Println("PING:", pingResult)
}
main.go

When running this program you should see.

$ go run main.go
PING: PONG

Now you have successfully connected to Redis šŸŽ‰.

Issuing Commands #

Let's increment a counter each time the program is ran.

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

func main() {
	... // previous code hidden for bravity

	_, err = redisClient.Incr(ctx, "count").Result()
	if err != nil {
		panic(err)
	}

	count, err := redisClient.Get(ctx, "count").Int()
	if err != nil {
		panic(err)
	}
    
	fmt.Printf("Count: %d\n", count)
}
main.go

The above will increment the value of key count and print it out.

$ go run main.go
PING: PONG
Count: 1

$ go run main.go
PING: PONG
Count: 2

$ go run main.go
PING: PONG
Count: 3

Cleanup #

Most likley you will be connecting to redis in a long-running application (such as a web server), but for this instance its good to close the connection once its no longer needed.

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

func main() {
	... // previous code hidden for bravity

	err = redisClient.Close()
	if err != nil {
		panic(err)
	}
}
main.go

Below is the complete code sample.

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

func main() {
	ctx := context.Background()

	redisClient := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	pingResult, err := redisClient.Ping(ctx).Result()
	if err != nil {
		panic(err)
	}

	fmt.Println("PING:", pingResult)

	_, err = redisClient.Incr(ctx, "count").Result()
	if err != nil {
		panic(err)
	}

	count, err := redisClient.Get(ctx, "count").Int()
	if err != nil {
		panic(err)
	}

	fmt.Printf("Count: %d\n", count)

	err = redisClient.Close()
	if err != nil {
		panic(err)
	}
}
main.go

Subscribe

Get updated 1 - 2 times a month when new articles are published, no spam ever.

    TheDeveloperCafe Ā© 2022-2024