You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: eBook/05.6.md
+26-25
Original file line number
Diff line number
Diff line change
@@ -20,33 +20,34 @@ I forked the last one and fixed some bugs, and use it in my short URL service(2
20
20
Let's see how to use the driver that I forked to operate database:
21
21
22
22
package main
23
-
23
+
24
24
import (
25
-
"github.com/astaxie/goredis"
26
-
"fmt"
25
+
"github.com/astaxie/goredis"
26
+
"fmt"
27
27
)
28
-
28
+
29
29
func main() {
30
-
var client goredis.Client
31
-
// string
32
-
var client goredis.Client
33
-
client.Set("a", []byte("hello"))
34
-
val, _ := client.Get("a")
35
-
fmt.Println(string(val))
36
-
client.Del("a")
37
-
38
-
// list
39
-
40
-
var client goredis.Client
41
-
vals := []string{"a", "b", "c", "d", "e"}
42
-
for _, v := range vals {
43
-
client.Rpush("l", []byte(v))
44
-
}
45
-
dbvals,_ := client.Lrange("l", 0, 4)
46
-
for i, v := range dbvals {
47
-
println(i,":",string(v))
48
-
}
49
-
client.Del("l")
30
+
var client goredis.Client
31
+
32
+
// Set the default port in Redis
33
+
client.Addr = "127.0.0.1:6379"
34
+
35
+
// string manipulation
36
+
client.Set("a", []byte("hello"))
37
+
val, _ := client.Get("a")
38
+
fmt.Println(string(val))
39
+
client.Del("a")
40
+
41
+
// list operation
42
+
vals := []string{"a", "b", "c", "d", "e"}
43
+
for _, v := range vals {
44
+
client.Rpush("l", []byte(v))
45
+
}
46
+
dbvals,_ := client.Lrange("l", 0, 4)
47
+
for i, v := range dbvals {
48
+
println(i,":",string(v))
49
+
}
50
+
client.Del("l")
50
51
}
51
52
52
53
We can see that it's quite easy to operate redis in Go, and it has high performance. Its client commands are almost the same as redis built-in commands.
@@ -105,4 +106,4 @@ We can see that there is no big different to operate database between mgo and be
105
106
##Links
106
107
-[Directory](preface.md)
107
108
- Previous section: [Develop ORM based on beedb](05.5.md)
0 commit comments