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.
Variables
Replace the following placeholders in the code sample with actual values from your service overview page:
| Variable | Description | 
|---|---|
SERVICE_URI | URI for the Aiven for Caching service connection | 
Prerequisites
Install the go-redis/redis library with the following command:
go get github.com/go-redis/redis/v8
Code
Create a file named main.go and insert the code below, substituting the placeholder
with your Aiven for Caching URI:
package main
import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
func main() {
	redisURI := "REDIS_URI"
	addr, err := redis.ParseURL(redisURI)
	if err != nil {
		panic(err)
	}
	rdb := redis.NewClient(addr)
	err = rdb.Set(ctx, "key", "hello world", 0).Err()
	if err != nil {
		panic(err)
	}
	val, err := rdb.Get(ctx, "key").Result()
	if err != nil {
		panic(err)
	}
	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