Simple Example and Uses Of Redis with Golang

Simple Example and Uses Of Redis with Golang

This golang tutorial help to integrate Redis client with golang to access key-value pair data, The Redis is the most popular database, its no SQL database and stored non-volatile data.

Redis is an open source, in-memory data structure store, used as a database, cache and message broker. Redis supports a number of data structures including strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and more. The advantages of using Redis over other caching stores, such as Memcached, is that Redis offers persistence.

There are many ways the use of Redis in your Golang application.

  • Use as NOSQL database
  • Caching of application of Session
  • You can use for queue
  • Pub/Sub

I am using go-redis client to connect with redis server and perform operation on data.There are many libs available but this is too simple and easy to use.You can download from GO-Redis Github.

Install go-redis using below command:
go get -u

We will import go-redis into top of the file:
import ""

Now we will create connection with redis server:

client := redis.NewClient(&redis.Options{
                Addr:     "localhost:6379",
                Password: "", // no password set
                DB:       0,  // use default DB

        pong, err := client.Ping().Result()
        fmt.Println(pong, err)

We need to pass Redis server name, port, database and password to connect Redis server.

How To Set Key name in Golang

Set() method is use to set key with value into redis.

err := client.Set("key", "value", 0).Err()
if err != nil {

How To Get Key Value in Redis Using Go

Get() method is use to get value against the key name from redis.

val, err := client.Get("key").Result()
if err != nil {
fmt.Println("key", val)