@@ -39,24 +39,17 @@ go get github.com/redis/go-redis/v9
3939
4040## Connect
4141
42- The following example shows the simplest way to connect to a Redis server:
42+ The following example shows the simplest way to connect to a Redis server.
43+ First, import the ` go-redis ` package:
4344
44- ``` go
45- import (
46- " context"
47- " fmt"
48- " github.com/redis/go-redis/v9"
49- )
45+ {{< clients-example set="landing" step="import" lang_filter="Go" >}}
46+ {{< /clients-example >}}
5047
51- func main () {
52- client := redis.NewClient (&redis.Options {
53- Addr: " localhost:6379" ,
54- Password: " " , // No password set
55- DB: 0 , // Use default DB
56- Protocol: 2 , // Connection protocol
57- })
58- }
59- ```
48+ Then connect to localhost on port 6379 and add a
49+ [ context] ( https://golang.org/pkg/context/ ) object:
50+
51+ {{< clients-example set="landing" step="connect" lang_filter="Go" >}}
52+ {{< /clients-example >}}
6053
6154You can also connect using a connection string:
6255
@@ -72,101 +65,37 @@ client := redis.NewClient(opt)
7265After connecting, you can test the connection by storing and retrieving
7366a simple [ string] ({{< relref "/develop/data-types/strings" >}}):
7467
75- ``` go
76- ctx := context.Background ()
77-
78- err := client.Set (ctx, " foo" , " bar" , 0 ).Err ()
79- if err != nil {
80- panic (err)
81- }
82-
83- val , err := client.Get (ctx, " foo" ).Result ()
84- if err != nil {
85- panic (err)
86- }
87- fmt.Println (" foo" , val)
88- ```
68+ {{< clients-example set="landing" step="set_get_string" lang_filter="Go" >}}
69+ {{< /clients-example >}}
8970
9071You can also easily store and retrieve a [ hash] ({{< relref "/develop/data-types/hashes" >}}):
9172
92- ``` go
93- hashFields := []string {
94- " model" , " Deimos" ,
95- " brand" , " Ergonom" ,
96- " type" , " Enduro bikes" ,
97- " price" , " 4972" ,
98- }
99-
100- res1 , err := client.HSet (ctx, " bike:1" , hashFields).Result ()
101-
102- if err != nil {
103- panic (err)
104- }
105-
106- fmt.Println (res1) // >>> 4
107-
108- res2 , err := client.HGet (ctx, " bike:1" , " model" ).Result ()
109-
110- if err != nil {
111- panic (err)
112- }
113-
114- fmt.Println (res2) // >>> Deimos
115-
116- res3 , err := client.HGet (ctx, " bike:1" , " price" ).Result ()
117-
118- if err != nil {
119- panic (err)
120- }
121-
122- fmt.Println (res3) // >>> 4972
123-
124- res4 , err := client.HGetAll (ctx, " bike:1" ).Result ()
125-
126- if err != nil {
127- panic (err)
128- }
129-
130- fmt.Println (res4)
131- // >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]
132- ```
73+ {{< clients-example set="landing" step="set_get_hash" lang_filter="Go" >}}
74+ {{< /clients-example >}}
13375
13476 Use
13577 [ struct tags] ( https://stackoverflow.com/questions/10858787/what-are-the-uses-for-struct-tags-in-go )
13678 of the form ` redis:"<field-name>" ` with the ` Scan() ` method to parse fields from
13779 a hash directly into corresponding struct fields:
13880
139- ``` go
140- type BikeInfo struct {
141- Model string ` redis:"model"`
142- Brand string ` redis:"brand"`
143- Type string ` redis:"type"`
144- Price int ` redis:"price"`
145- }
146-
147- var res4a BikeInfo
148- err = client.HGetAll (ctx, " bike:1" ).Scan (&res4a)
81+ {{< clients-example set="landing" step="get_hash_scan" lang_filter="Go" >}}
82+ {{< /clients-example >}}
14983
150- if err != nil {
151- panic (err)
152- }
84+ Close the connection when you're done using a ` Close() ` call:
15385
154- fmt.Printf (" Model: %v , Brand: %v , Type: %v , Price: $%v \n " ,
155- res4a.Model , res4a.Brand , res4a.Type , res4a.Price )
156- // >>> Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972
157- ```
86+ {{< clients-example set="landing" step="close" lang_filter="Go" >}}
87+ {{< /clients-example >}}
15888
159- Close the connection when you're done using ` client.Close() ` . In the
160- common case where you want to close the connection at the end of the
89+ In the common case where you want to close the connection at the end of the
16190function where you opened it, you may find it convenient to use a ` defer `
16291statement right after connecting:
16392
16493``` go
16594func main () {
166- client := redis.NewClient (&redis.Options {
95+ rdb := redis.NewClient (&redis.Options {
16796 ...
16897 })
169- defer client .Close ()
98+ defer rdb .Close ()
17099 ...
171100}
172101```
0 commit comments