Go - 操作redis

启动redis服务

$ sudo service redis-server start
Starting redis-server: redis-server.
$ redis-cli
127.0.0.1:6379> ping
PONG

下载redis驱动包

$ go get -u github.com/gomodule/redigo/redis

操作测试

$ cd redis && go mod init redis
$ vi redis-demo.go
// redis-demo.go
package main

import(
    "fmt"
    "github.com/gomodule/redigo/redis"
)

func main(){

    // access redis's conn
    conn, err := redis.DialURL("redis://127.0.0.1:6379")
    if err != nil{
        panic(err)
    }
    defer conn.Close()

    // set
    res, err := redis.String(conn.Do("set", "k", "v"))
    fmt.Println(res, err)

    // get
    res, err = redis.String(conn.Do("get", "k"))
    fmt.Println(res, err)

    // del
    n, err := redis.Int64(conn.Do("del", "k"))
    fmt.Println(n, err)

    res, err = redis.String(conn.Do("get", "k"))
    fmt.Println(res, err)

}
$ go run redis-demo.go                      
OK 
v 
1 
 redigo: nil returned

你可能感兴趣的:(goredis)