Skip to main content

Connect with Go

Learn how to establish a connection to an Aiven for Caching service using Go. This example demonstrates how to establish a connection to an Aiven for Caching service from Go using the go-redis/redis library, which is compatible with the Redis protocol.


Replace the following placeholders in the code sample with actual values from your service overview page:

SERVICE_URIURI for the Aiven for Caching service connection


Install the go-redis/redis library with the following command:

go get


Create a file named main.go and insert the code below, substituting the placeholder with your Aiven for Caching URI:

package main

import (


var ctx = context.Background()

func main() {
redisURI := "REDIS_URI"

addr, err := redis.ParseURL(redisURI)
if err != nil {

rdb := redis.NewClient(addr)

err = rdb.Set(ctx, "key", "hello world", 0).Err()
if err != nil {

val, err := rdb.Get(ctx, "key").Result()
if err != nil {
fmt.Println("The value of key is:", val)

This code creates a key named key with the value hello world without an expiration. It then retrieves this key from the caching service and outputs its value.

Execute the script with:

go run main.go

Successful execution results in the following output:

The value of key is: hello world