如何连接到Redis(Sentinel)

时间:2019-12-24 16:07:38

标签: go redis

我尝试与REDIS SENTINEL连接的代码。由于我是Golang的新手,所以想弄清楚我怎么了? REDIS SENTINEL已配置并正常工作。

package main

import (
    "fmt"
    "github.com/go-redis/redis"
)

func main() {
    rdb:=redis.NewFailoverClient(&redis.FailoverOptions(
            MasterName: "mymaster",
            SentinelAddrs: "XXX.XXX.XXX.XXX:26379","XXX.XXX.XXX.XXX:26379","XXX.XXX.XXX.XXX:26379",
    ))
    rdb.Ping()
}

运行:

$ go run redis.go
**# command-line-arguments**
>./redif.go:11:12: syntax error: unexpected :, expecting comma or )**

1 个答案:

答案 0 :(得分:0)

该错误表明您使用了错误的格式化字符串切片。

参见“ go-redis” pkg中的FailoverOption{}。它说.SentinelAddrs[]string类型。

rdb:=redis.NewFailoverClient(&redis.FailoverOptions(
    MasterName: "mymaster",
    SentinelAddrs: []string{"XXX.XXX.XXX.XXX:26379","XXX.XXX.XXX.XXX:26379","XXX.XXX.XXX.XXX:26379"},
))