Skip to content

Commit 5f577de

Browse files
Merge pull request #2339 from redis/DOC-5921-update-go-landing-page
DOC-5921 update Go landing page for notebook
2 parents ba1f746 + ade3475 commit 5f577de

File tree

3 files changed

+126
-93
lines changed

3 files changed

+126
-93
lines changed

content/develop/clients/go/_index.md

Lines changed: 21 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6154
You can also connect using a connection string:
6255

@@ -72,101 +65,37 @@ client := redis.NewClient(opt)
7265
After connecting, you can test the connection by storing and retrieving
7366
a 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

9071
You 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
16190
function where you opened it, you may find it convenient to use a `defer`
16291
statement right after connecting:
16392

16493
```go
16594
func 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
```
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// EXAMPLE: landing
2+
// BINDER_ID go-landing
3+
package main
4+
5+
// STEP_START import
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/redis/go-redis/v9"
11+
)
12+
13+
// STEP_END
14+
15+
func main() {
16+
// STEP_START connect
17+
rdb := redis.NewClient(&redis.Options{
18+
Addr: "localhost:6379",
19+
Password: "", // no password
20+
DB: 0, // use default DB
21+
Protocol: 2,
22+
})
23+
24+
ctx := context.Background()
25+
// STEP_END
26+
27+
// STEP_START set_get_string
28+
err := rdb.Set(ctx, "foo", "bar", 0).Err()
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
val, err := rdb.Get(ctx, "foo").Result()
34+
if err != nil {
35+
panic(err)
36+
}
37+
fmt.Println("foo", val) // >>> foo bar
38+
// STEP_END
39+
40+
// STEP_START set_get_hash
41+
hashFields := []string{
42+
"model", "Deimos",
43+
"brand", "Ergonom",
44+
"type", "Enduro bikes",
45+
"price", "4972",
46+
}
47+
48+
res1, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
49+
50+
if err != nil {
51+
panic(err)
52+
}
53+
54+
fmt.Println(res1) // >>> 4
55+
56+
res2, err := rdb.HGet(ctx, "bike:1", "model").Result()
57+
58+
if err != nil {
59+
panic(err)
60+
}
61+
62+
fmt.Println(res2) // >>> Deimos
63+
64+
res3, err := rdb.HGet(ctx, "bike:1", "price").Result()
65+
66+
if err != nil {
67+
panic(err)
68+
}
69+
70+
fmt.Println(res3) // >>> 4972
71+
72+
res4, err := rdb.HGetAll(ctx, "bike:1").Result()
73+
74+
if err != nil {
75+
panic(err)
76+
}
77+
78+
fmt.Println(res4)
79+
// >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]
80+
// STEP_END
81+
82+
// STEP_START get_hash_scan
83+
type BikeInfo struct {
84+
Model string `redis:"model"`
85+
Brand string `redis:"brand"`
86+
Type string `redis:"type"`
87+
Price int `redis:"price"`
88+
}
89+
90+
var res4a BikeInfo
91+
err = rdb.HGetAll(ctx, "bike:1").Scan(&res4a)
92+
93+
if err != nil {
94+
panic(err)
95+
}
96+
97+
fmt.Printf("Model: %v, Brand: %v, Type: %v, Price: $%v\n",
98+
res4a.Model, res4a.Brand, res4a.Type, res4a.Price)
99+
// >>> Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972
100+
// STEP_END
101+
102+
// STEP_START close
103+
rdb.Close()
104+
// STEP_END
105+
}

local_examples/rust-async/dt-string.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// EXAMPLE: set_tutorial
32
#[cfg(test)]
43
mod tests {

0 commit comments

Comments
 (0)