How to Use Redis Go Client go-redis/redis with GoLang

Introduction

Go-Redis is one of the most popular clusters for Redis, consistently receiving high marks and positive reviews from users. Go-Redis is a Redis client able to support a Redis cluster and is a Go language execution of the Redis client based on Redigo. Go-Redis is designed to store and update slot info automatically with a cluster change. The Redis client is designed to control a connection pool for each node, resulting in greater efficiency and reduced latency. This tutorial will explain how to use the Redis Go client Go-Redis with Golang.

Prerequisites

  • Redis must be properly installed and configured. Confirm the Redis installation using the redis-cli --version command. The result should resemble the following:
redis-cli 4.0.9
  • Golang must be properly installed and configured. Confirm the Golang installation using the go version command. The result should resemble the following:
go version go1.12.5 linux/amd64

The Go-Redis/Redis Client

Redis go-redis/redis is best describes as a Redis client that is able to support a Redis cluster and Redis Sentinel by default.

How to Download Go-Redis/Redis

Execute the following command to download the needed Go-Redis/Redis dependencies:

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

How to connect Redis to PHP using the Go-Redis/Redis Client

With the Redis client installed, create a function that will connect PHP and Redis.

How to create the Redis Connection

Execute the following Golang script to create a Redis connection:

func rClient() *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})

return client
}

The above script creates a new Redis client that will connect to the Redis server deployment. Additionally, the client can be defined using &redis.Options and provide details such as PoolSize, MaxRetries, Password and DB. In this example the Addr options is used to specify the hostname and port number of the Redis server.

How to check the connection with Redis server

Execute the following script to have Go check the connection status to the Redis server:

func ping(client *redis.Client) error {
pong, err := client.Ping().Result()
if err != nil {
return err
}
fmt.Println(pong, err)
// Output: PONG <nil>

return nil
}

The above Golang script is designed to send a ping connectivity test to Redis. If successful, this test will return a PONG <nil> as a result that confirms the connection to the Redis server has been established without an error.

How to use the Redis “SET” Command

With a connection established with the Redis server in the previous section, execute a basic Redis SET command with the following script:

func set(client *redis.Client) error {
err := client.Set("name", "risa", 0).Err()
if err != nil {
return err
}

err = client.Set("country", "Philippines", 0).Err()
if err != nil {
return err
}
return nil
}

The above Golang script will create a key-value pair within Redis. This will set a string value of “risa” to the key “name” and also set the key “country” with the value of “Philippines”.

The following section will detail how to perform error checking with the GET command.

How to use the Redis “GET” Command

This section will explain how to create a Go script to retrieve the values that were specified against the keys in the previous section. Execute the following script to employ the GET command:

func get(client *redis.Client) error {
nameVal, err := client.Get("name").Result()
if err != nil {
return (err)
}
fmt.Println("name", nameVal)

countryVal, err := client.Get("country").Result()
if err == redis.Nil {
fmt.Println("no value found")
} else if err != nil {
panic(err)
} else {
fmt.Println("country", countryVal)
}


return nil
}

An example Go script was also created in the above code to get a value from an unset key using the redis.ErrNil command. This will determine if anything was returned and, if not, how to properly handle the issue.

How to Test the Code

With the connection established and the getters/setters properly set up, execute the following script within the func main:

func main() {

// creates a client
client := rClient()

// check connection status
err := ping(client)
if err != nil {
fmt.Println(err)
}

// Using the SET command to set Key-value pair
err = set(client)
if err != nil {
fmt.Println(err)
}

// Using the GET command to get values from keys
err = get(client)
if err != nil {
fmt.Println(err)
}

}

The result should resemble the following:

PONG <nil>
name risa
country Philippines

Conclusion

This tutorial explained how to use the Redis Go Client Go-Redis with Golang. The tutorial focused on how to connect Redis with Golang using the go-redis/redis client and provided a few examples that demonstrated how to interact with Redis using Golang scripts. The tutorial specifically covered how to connect Redis to PHP using the go-redis/redis client, how to check the connection with Redis server, how to use the Redis “GET” and “SET” commands and how to test the code. Remember that when sending a ping connectivity test to Redis a successful connection will return a PONG <nil> as confirmation the connection to the Redis server was established without an error.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.