# Connecting with Go

This guide explains how to establish a connection between a Go application and a Valkey database using the <span class="s3">go-redis</span> package. It walks through the necessary setup, configuration, and execution of a simple Valkey command.

## **Variables**

Certain parameters must be provided to establish a successful connection to a Valkey database. Below is a breakdown of each required variable, its purpose, and where to find it. Here’s what each variable represents:

<table border="1" id="bkmrk-variable-description" style="width: 100%; border-collapse: collapse; border-color: rgb(0, 0, 0);"><thead><tr><th style="width: 10.7246%; border-color: rgb(0, 0, 0);">**Variable**

</th><th style="width: 42.5531%; border-color: rgb(0, 0, 0);">**Description**

</th><th style="width: 46.7223%; border-color: rgb(0, 0, 0);">**Purpose**

</th></tr></thead><tbody><tr><td style="width: 10.7246%; border-color: rgb(0, 0, 0);">`HOST`

</td><td style="width: 42.5531%; border-color: rgb(0, 0, 0);">Valkey hostname, from the Elestio service overview page

</td><td style="width: 46.7223%; border-color: rgb(0, 0, 0);">The address of the server hosting your Valkey instance.

</td></tr><tr><td style="width: 10.7246%; border-color: rgb(0, 0, 0);">`PORT`

</td><td style="width: 42.5531%; border-color: rgb(0, 0, 0);">Port for Valkey connection, from the Elestio service overview page

</td><td style="width: 46.7223%; border-color: rgb(0, 0, 0);">The network port used to connect to Valkey. The default port is 6379.

</td></tr><tr><td style="width: 10.7246%; border-color: rgb(0, 0, 0);">`PASSWORD`

</td><td style="width: 42.5531%; border-color: rgb(0, 0, 0);">Valkey password, from the Elestio service overview page

</td><td style="width: 46.7223%; border-color: rgb(0, 0, 0);">The authentication key required to connect securely to Valkey.

</td></tr></tbody></table>

These values can usually be found in the Elestio service overview details as shown in the image below, make sure to take a copy of these details and add it to the code moving ahead.

[![Screenshot 2025-07-04 at 4.12.37 PM.jpg](https://docs.elest.io/uploads/images/gallery/2025-07/scaled-1680-/0k7screenshot-2025-07-04-at-4-12-37-pm.jpg)](https://docs.elest.io/uploads/images/gallery/2025-07/0k7screenshot-2025-07-04-at-4-12-37-pm.jpg)

## **Prerequisites**

**Install Go**

Check if Go is installed by running:

```bash
go version
```

If not installed, download it from golang.org and install.

**Install the go-redis Package**

The <span class="s2">go-redis</span> package enables Go applications to interact with Valkey. Install it using:

```bash
go get github.com/redis/go-redis/v9
```

## **Code**

Once all prerequisites are set up, create a new file named `<span class="s2">valkey.go</span>` and add the following code:

```go
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/redis/go-redis/v9"
)

func main() {
	opt := &redis.Options{
		Addr:     "HOST:PORT",     
		Password: "PASSWORD",      
		DB:       0,           
	}

	valkey := redis.NewClient(opt)
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	err := valkey.Set(ctx, "testKey", "Hello Valkey", 0).Err()
	if err != nil {
		fmt.Println("Valkey operation failed:", err)
		return
	}

	val, err := valkey.Get(ctx, "testKey").Result()
	if err != nil {
		fmt.Println("Valkey operation failed:", err)
		return
	}

	fmt.Println("Connected to Valkey")
	fmt.Println("Retrieved value:", val)

	if err := valkey.Close(); err != nil {
		fmt.Println("Error closing connection:", err)
	}
}
```

To execute the script, open the terminal or command prompt and navigate to the directory where `<span class="s1">valkey.go</span>` is located. Once in the correct directory, run the script with the command:

```bash
go run valkey.go
```

If the connection is successful, the terminal will display output similar to:

```
Connected to Valkey  
Retrieved value: Hello Valkey
```