Skip to main content

Connect with Go

This example demonstrates how to connect to Dragonfly® using Go, using the go-redis/redis library, which is officially supported with Dragonfly. For more information, see Dragonfly SDKs.


These are the placeholders you will need to replace in the code sample:

DRAGONFLY_URIURL for the Dragonfly® connection


First, install the go-redis/redis library:

go get


Create a file named main.go and add the following content, replacing the DRAGONFLY_URI placeholder with your Dragonfly instance's connection URI:

package main

import (

var ctx = context.Background()

func main() {
dragonflyURI := "DRAGONFLY_URI"

opts, err := redis.ParseURL(dragonflyURI)
if err != nil {

rdb := redis.NewClient(opts)

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 connects to Dragonfly, sets a key named key with the value hello world (with no expiration), and retrieves and prints the value of this key.

Run the code

To run the code, use the following command in your terminal:

go run main.go

If everything is set up correctly, the output should be:

The value of key is: hello world