==下载依赖==
go get github.com/go-redis/redis
==代码样例==
package main
import (
"fmt"
"github.com/go-redis/redis"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "47.116.50.192:36379",
Password: "123456",
DB: 0,
})
ctx := client.Context()
pong, err := client.Ping(ctx).Result()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("连接成功。", pong)
key := "key1"
err = client.Set(ctx, key, "abc", 0).Err()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("写入成功。")
value, err := client.Get(ctx, key).Result()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("读取成功:" + value)
}
==执行结果==
API server listening at: 127.0.0.1:14905
连接成功。 PONG
写入成功。
读取成功:abc