$ sudo service redis-server start
Starting redis-server: redis-server.
$ redis-cli
127.0.0.1:6379> ping
PONG
$ 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 <nil>
v <nil>
1 <nil>
redigo: nil returned