go redis 集合操作--sadd
redis的go语言包,我们使用官方推荐的redigo,https://github.com/garyburd/redigo
安装redigo
1
|
$ go get github.com /garyburd/redigo |
带密码的redis操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package main import ( "log" "github.com/garyburd/redigo/redis" ) func main() { server := "127.0.0.1:6379" option := redis.DialPassword("123456") c, err := redis.Dial("tcp", server, option) if err != nil { log.Println("connect server failed:", err) return } defer c.Close() v, err := redis.Int64(c.Do("SADD", "myset", "10.8.37.98")) if err != nil { log.Println("SADD failed:", err) return } log.Println("value:", v) } |
批量添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package main import ( "log" "github.com/garyburd/redigo/redis" ) func main() { server := "127.0.0.1:6379" option := redis.DialPassword("123456") c, err := redis.Dial("tcp", server, option) if err != nil { log.Println("connect server failed:", err) return } defer c.Close() setKey := "myset" args := []interface{}{setKey} args = append(args, "10.1") args = append(args, "10.2") v, err := redis.Int64(c.Do("SADD", args...)) if err != nil { log.Println("SADD failed:", err) return } log.Println("value:", v) } |
无密码redis操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package main import ( "fmt" "github.com/gomodule/redigo/redis" ) func main() { c, err := redis.Dial("tcp", "127.0.0.1:6379") if err != nil { fmt.Println(err) return } defer c.Close() c.Do("SET", "hello", "world") s, err := redis.String(c.Do("GET", "hello")) |
redis封装包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package redis import ( "autumn/tools/cfg" "fmt" "github.com/gomodule/redigo/redis" "log" ) var REDIS redis.Conn func InitRedis() { REDIS = redis_connect("default") } func CloseRedis() { REDIS.Close() } func redis_connect(project string) redis.Conn { server := fmt.Sprintf("%s:%s", cfg.Get("redis", project+".host").String(), cfg.Get("redis", project+".port").String()) var err error option := redis.DialPassword(cfg.Get("redis", project+".passwd").String()) c, err := redis.Dial("tcp", server, option) if err != nil { log.Fatal("[GIN-MYSQL(" + project + ")] connect to redis error:" + err.Error()) } log.Println("[GIN-Redis(" + project + ")] connected success") return c } |
参考
redigo
https://github.com/garyburd/redigo
redigo API Reference
https://godoc.org/github.com/gomodule/redigo/redis
以上就是Go语言开发redis封装及简单使用详解的详细内容,更多关于Go语言开发redis封装及使用的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/ffzhihua/article/details/83176719