{
  "schema_version": 2,
  "id": "develop/clients/go",
  "title": "go-redis guide (Go)",
  "url": "https://redis.io/docs/latest/develop/clients/go/",
  "summary": "Connect your Go application to a Redis database",
  "aliases": [
    "/develop/connect/clients/go",
    "/connect/clients/go/",
    "/clients/go/",
    "/develop/clients/go-redis"
  ],
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-10-12T14:48:04+01:00",
  "children": [
    {
      "id": "develop/clients/go/connect",
      "summary": "Connect your Go application to a Redis database",
      "title": "Connect to the server",
      "url": "https://redis.io/docs/latest/develop/clients/go/connect/"
    },
    {
      "id": "develop/clients/go/amr",
      "summary": "Learn how to authenticate to an Azure Managed Redis (AMR) database",
      "title": "Connect to Azure Managed Redis",
      "url": "https://redis.io/docs/latest/develop/clients/go/amr/"
    },
    {
      "id": "develop/clients/go/queryjson",
      "summary": "Learn how to use Redis Search with JSON and hash documents.",
      "title": "Index and query documents",
      "url": "https://redis.io/docs/latest/develop/clients/go/queryjson/"
    },
    {
      "id": "develop/clients/go/vecsearch",
      "summary": "Learn how to index and query vector embeddings with Redis",
      "title": "Index and query vectors",
      "url": "https://redis.io/docs/latest/develop/clients/go/vecsearch/"
    },
    {
      "id": "develop/clients/go/vecsets",
      "summary": "Index and query embeddings with Redis vector sets",
      "title": "Vector set embeddings",
      "url": "https://redis.io/docs/latest/develop/clients/go/vecsets/"
    },
    {
      "id": "develop/clients/go/scan-buffer",
      "summary": "Scan go-redis command results into Go values and use buffers for large string payloads.",
      "title": "Handle command results efficiently",
      "url": "https://redis.io/docs/latest/develop/clients/go/scan-buffer/"
    },
    {
      "id": "develop/clients/go/transpipe",
      "summary": "Learn how to use Redis pipelines and transactions",
      "title": "Pipelines and transactions",
      "url": "https://redis.io/docs/latest/develop/clients/go/transpipe/"
    },
    {
      "id": "develop/clients/go/autopipeline",
      "summary": "Batch concurrent go-redis commands into pipelines automatically for high-throughput workloads.",
      "title": "Automatic pipelining",
      "url": "https://redis.io/docs/latest/develop/clients/go/autopipeline/"
    },
    {
      "id": "develop/clients/go/prob",
      "summary": "Learn how to use approximate calculations with Redis.",
      "title": "Probabilistic data types",
      "url": "https://redis.io/docs/latest/develop/clients/go/prob/"
    },
    {
      "id": "develop/clients/go/error-handling",
      "summary": "Learn how to handle errors when using go-redis",
      "title": "Error handling",
      "url": "https://redis.io/docs/latest/develop/clients/go/error-handling/"
    },
    {
      "id": "develop/clients/go/produsage",
      "summary": "Get your `go-redis` app ready for production",
      "title": "Production usage",
      "url": "https://redis.io/docs/latest/develop/clients/go/produsage/"
    },
    {
      "id": "develop/clients/go/observability",
      "summary": "Monitor your client's activity for optimization and debugging.",
      "title": "Observability",
      "url": "https://redis.io/docs/latest/develop/clients/go/observability/"
    }
  ],
  "page_type": "content",
  "content_hash": "9432ef8e79bf744f827759b5b8a087eb9a0a78ea1f859f49e12061b9ae5eed09",
  "sections": [
    {
      "id": "install",
      "title": "Install",
      "role": "setup",
      "text": "`go-redis` supports the last two Go versions. You can only use it from within\na Go module, so you must initialize a Go module before you start, or add your code to\nan existing module:\n\n[code example]\n\nUse the `go get` command to install `go-redis/v9`:\n\n[code example]"
    },
    {
      "id": "connect",
      "title": "Connect",
      "role": "content",
      "text": "The following example shows the simplest way to connect to a Redis server.\nFirst, import the `go-redis` package:\n\nFoundational: Import the go-redis package\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nThen connect to localhost on port 6379 and add a\n[context](https://golang.org/pkg/context/) object:\n\nFoundational: Connect to a Redis server and establish a client connection\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nYou can also connect using a connection URL:\n\n[code example]\n\n> [!NOTE]\n> go-redis supports [connection URLs](https://redis.io/docs/latest/develop/clients/connection-urls#go-redis-go) through `redis.ParseURL()`.\n> A URL can set the host, port, credentials, database, and whether to use TLS,\n> so you can use one instead of passing those options separately.\n\nAfter connecting, you can test the connection by  storing and retrieving\na simple [string](https://redis.io/docs/latest/develop/data-types/strings):\n\nFoundational: Set and retrieve string values using SET and GET commands\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nYou can also easily store and retrieve a [hash](https://redis.io/docs/latest/develop/data-types/hashes):\n\nFoundational: Store and retrieve hash data structures using HSET and HGET commands\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\n Use\n [struct tags](https://stackoverflow.com/questions/10858787/what-are-the-uses-for-struct-tags-in-go)\n of the form `redis:\"<field-name>\"` with the `Scan()` method to parse fields from\n a hash directly into corresponding struct fields:\n\nFoundational: Parse hash data structures into struct fields using Scan()\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nClose the connection when you're done using a `Close()` call:\n\nFoundational: Close the Redis client connection\n\n**Difficulty:** Beginner\n\n**Available in:** Go\n\n##### Go\n\n[code example]\n\n\n\nIn the common case where you want to close the connection at the end of the\nfunction where you opened it, you may find it convenient to use a `defer`\nstatement right after connecting:\n\n[code example]"
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "See the other pages in this section for more information and examples.\nFurther examples are available at the [`go-redis`](https://redis.uptrace.dev/guide/) website\nand the [GitHub repository](https://github.com/redis/go-redis)."
    }
  ],
  "examples": [
    {
      "id": "install-ex0",
      "language": "plaintext",
      "code": "go mod init github.com/my/repo",
      "section_id": "install"
    },
    {
      "id": "install-ex1",
      "language": "plaintext",
      "code": "go get github.com/redis/go-redis/v9",
      "section_id": "install"
    },
    {
      "id": "connect-ex0",
      "language": "go",
      "code": "import (\n\t\"context\"\n\t\"fmt\"\n\n\t\"github.com/redis/go-redis/v9\"\n)",
      "section_id": "connect"
    },
    {
      "id": "connect-ex1",
      "language": "go",
      "code": "rdb := redis.NewClient(&redis.Options{\n\t\tAddr:     \"localhost:6379\",\n\t\tPassword: \"\", // no password\n\t\tDB:       0,  // use default DB\n\t\tProtocol: 2,\n\t})\n\n\tctx := context.Background()",
      "section_id": "connect"
    },
    {
      "id": "connect-ex2",
      "language": "go",
      "code": "opt, err := redis.ParseURL(\"redis://<user>:<pass>@localhost:6379/<db>\")\nif err != nil {\n\tpanic(err)\n}\n\nclient := redis.NewClient(opt)",
      "section_id": "connect"
    },
    {
      "id": "connect-ex3",
      "language": "go",
      "code": "err := rdb.Set(ctx, \"foo\", \"bar\", 0).Err()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err := rdb.Get(ctx, \"foo\").Result()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(\"foo\", val) // >>> foo bar",
      "section_id": "connect"
    },
    {
      "id": "connect-ex4",
      "language": "go",
      "code": "hashFields := []string{\n\t\t\"model\", \"Deimos\",\n\t\t\"brand\", \"Ergonom\",\n\t\t\"type\", \"Enduro bikes\",\n\t\t\"price\", \"4972\",\n\t}\n\n\tres1, err := rdb.HSet(ctx, \"bike:1\", hashFields).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res1) // >>> 4\n\n\tres2, err := rdb.HGet(ctx, \"bike:1\", \"model\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res2) // >>> Deimos\n\n\tres3, err := rdb.HGet(ctx, \"bike:1\", \"price\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res3) // >>> 4972\n\n\tres4, err := rdb.HGetAll(ctx, \"bike:1\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res4)\n\t// >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]",
      "section_id": "connect"
    },
    {
      "id": "connect-ex5",
      "language": "go",
      "code": "type BikeInfo struct {\n\t\tModel string `redis:\"model\"`\n\t\tBrand string `redis:\"brand\"`\n\t\tType  string `redis:\"type\"`\n\t\tPrice int    `redis:\"price\"`\n\t}\n\n\tvar res4a BikeInfo\n\terr = rdb.HGetAll(ctx, \"bike:1\").Scan(&res4a)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"Model: %v, Brand: %v, Type: %v, Price: $%v\\n\",\n\t\tres4a.Model, res4a.Brand, res4a.Type, res4a.Price)\n\t// >>> Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972",
      "section_id": "connect"
    },
    {
      "id": "connect-ex6",
      "language": "go",
      "code": "rdb.Close()",
      "section_id": "connect"
    },
    {
      "id": "connect-ex7",
      "language": "go",
      "code": "func main() {    \n    rdb := redis.NewClient(&redis.Options{\n        ...\n    })\n    defer rdb.Close()\n    ...\n}",
      "section_id": "connect"
    }
  ]
}
