|
| 1 | +--- |
| 2 | +id: index-golang |
| 3 | +title: Go and Redis |
| 4 | +sidebar_label: Go |
| 5 | +slug: /develop/golang/ |
| 6 | +--- |
| 7 | + |
| 8 | +import Tabs from '@theme/Tabs'; |
| 9 | +import TabItem from '@theme/TabItem'; |
| 10 | +import useBaseUrl from '@docusaurus/useBaseUrl'; |
| 11 | + |
| 12 | +Find tutorials, examples and technical articles that will help you to develop with Redis and Golang. |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +Golang community has built many client libraries that you can find [here](https://redis.io/clients#go). |
| 17 | + For your first steps with Golang and Redis, this article will show how to use the recommended library: [redigo](https://github.com/gomodule/redigo). |
| 18 | + |
| 19 | + |
| 20 | + <Tabs |
| 21 | + defaultValue="redigo" |
| 22 | + values={[ |
| 23 | + {label: 'Redigo', value: 'redigo'}, |
| 24 | + ]}> |
| 25 | + <TabItem value="redigo"> |
| 26 | + |
| 27 | +### Getting Started with redigo |
| 28 | + |
| 29 | + |
| 30 | +The `redigo` library is located in the `https://github.com/gomodule/redigo` that you must import in your application. |
| 31 | + |
| 32 | + |
| 33 | +1. Import the `redigo` module |
| 34 | + |
| 35 | + ```bash |
| 36 | + go get github.com/gomodule/redigo/redis |
| 37 | + ``` |
| 38 | + |
| 39 | + ```go |
| 40 | + import ( |
| 41 | + "fmt" |
| 42 | + "context" |
| 43 | + "github.com/gomodule/redigo/redis" |
| 44 | + ) |
| 45 | + ``` |
| 46 | + |
| 47 | +1. Create a connection pool |
| 48 | + |
| 49 | + ```go |
| 50 | + func newPool() *redis.Pool { |
| 51 | + return &redis.Pool{ |
| 52 | + MaxIdle: 80, |
| 53 | + MaxActive: 12000, |
| 54 | + Dial: func() (redis.Conn, error) { |
| 55 | + c, err := redis.Dial("tcp", ":6379") |
| 56 | + if err != nil { |
| 57 | + panic(err.Error()) |
| 58 | + } |
| 59 | + return c, err |
| 60 | + }, |
| 61 | + } |
| 62 | + } |
| 63 | + ``` |
| 64 | + |
| 65 | +1. Write your application code |
| 66 | + |
| 67 | + ```go |
| 68 | + package main |
| 69 | + |
| 70 | + import ( |
| 71 | + "fmt" |
| 72 | + |
| 73 | + "github.com/gomodule/redigo/redis" |
| 74 | + ) |
| 75 | + |
| 76 | + var pool = newPool() |
| 77 | + |
| 78 | + func main() { |
| 79 | + |
| 80 | + client := pool.Get() |
| 81 | + defer client.Close() |
| 82 | + |
| 83 | + _, err := client.Do("SET", "mykey", "Hello from redigo!") |
| 84 | + if err != nil { |
| 85 | + panic(err) |
| 86 | + } |
| 87 | + |
| 88 | + value, err := client.Do("GET", "mykey") |
| 89 | + if err != nil { |
| 90 | + panic(err) |
| 91 | + } |
| 92 | + |
| 93 | + fmt.Printf("%s \n", value) |
| 94 | + |
| 95 | + _, err = client.Do("ZADD", "vehicles", 4, "car") |
| 96 | + if err != nil { |
| 97 | + panic(err) |
| 98 | + } |
| 99 | + _, err = client.Do("ZADD", "vehicles", 2, "bike") |
| 100 | + if err != nil { |
| 101 | + panic(err) |
| 102 | + } |
| 103 | + |
| 104 | + vehicles, err := client.Do("ZRANGE", "vehicles", 0, -1, "WITHSCORES") |
| 105 | + if err != nil { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | + fmt.Printf("%s \n", vehicles) |
| 109 | + |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + func newPool() *redis.Pool { |
| 114 | + return &redis.Pool{ |
| 115 | + MaxIdle: 80, |
| 116 | + MaxActive: 12000, |
| 117 | + Dial: func() (redis.Conn, error) { |
| 118 | + c, err := redis.Dial("tcp", ":6379") |
| 119 | + if err != nil { |
| 120 | + panic(err.Error()) |
| 121 | + } |
| 122 | + return c, err |
| 123 | + }, |
| 124 | + } |
| 125 | + } |
| 126 | + ``` |
| 127 | + |
| 128 | +Find more information about Golang & Redis connections in the "[Redis Connect](https://github.com/redis-developer/redis-connect/tree/master/golang/redigo)". |
| 129 | + |
| 130 | + |
| 131 | + </TabItem> |
| 132 | +</Tabs> |
0 commit comments