Back to Redis

go-redis guide (Go)

content/develop/clients/go/_index.md

latest3.3 KB
Original Source

go-redis is the Go client for Redis. The sections below explain how to install go-redis and connect your application to a Redis database.

go-redis requires a running Redis server. See [here]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis Open Source installation instructions.

Install

go-redis supports the last two Go versions. You can only use it from within a Go module, so you must initialize a Go module before you start, or add your code to an existing module:

go mod init github.com/my/repo

Use the go get command to install go-redis/v9:

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

Connect

The following example shows the simplest way to connect to a Redis server. First, import the go-redis package:

{{< clients-example set="landing" step="import" lang_filter="Go" description="Foundational: Import the go-redis package" difficulty="beginner" >}} {{< /clients-example >}}

Then connect to localhost on port 6379 and add a context object:

{{< clients-example set="landing" step="connect" lang_filter="Go" description="Foundational: Connect to a Redis server and establish a client connection" difficulty="beginner" >}} {{< /clients-example >}}

You can also connect using a connection string:

go
opt, err := redis.ParseURL("redis://<user>:<pass>@localhost:6379/<db>")
if err != nil {
	panic(err)
}

client := redis.NewClient(opt)

After connecting, you can test the connection by storing and retrieving a simple [string]({{< relref "/develop/data-types/strings" >}}):

{{< clients-example set="landing" step="set_get_string" lang_filter="Go" description="Foundational: Set and retrieve string values using SET and GET commands" difficulty="beginner" >}} {{< /clients-example >}}

You can also easily store and retrieve a [hash]({{< relref "/develop/data-types/hashes" >}}):

{{< clients-example set="landing" step="set_get_hash" lang_filter="Go" description="Foundational: Store and retrieve hash data structures using HSET and HGET commands" difficulty="beginner" >}} {{< /clients-example >}}

Use struct tags of the form redis:"<field-name>" with the Scan() method to parse fields from a hash directly into corresponding struct fields:

{{< clients-example set="landing" step="get_hash_scan" lang_filter="Go" description="Foundational: Parse hash data structures into struct fields using Scan()" difficulty="beginner" >}} {{< /clients-example >}}

Close the connection when you're done using a Close() call:

{{< clients-example set="landing" step="close" lang_filter="Go" description="Foundational: Close the Redis client connection" difficulty="beginner" >}} {{< /clients-example >}}

In the common case where you want to close the connection at the end of the function where you opened it, you may find it convenient to use a defer statement right after connecting:

go
func main() {    
    rdb := redis.NewClient(&redis.Options{
        ...
    })
    defer rdb.Close()
    ...
}

More information

See the other pages in this section for more information and examples. Further examples are available at the go-redis website and the GitHub repository.