Go 连接示例

最近更新时间:2023-11-03 17:50:01

我的收藏

运行前必备

下载客户端 Go-redis

示例代码

package main

import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"log"
"time"
)
func main() {
const host = "127.0.0.1"
const port = 6379
const pwd = "*******"
newClient := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", host, port),
Password: pwd,
})
ctx, cancelFunc := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFunc()
statusCmd := newClient.Ping(ctx)
log.Printf(" ping status: %s", statusCmd.Val())
}