package config import ( "context" "github.com/redis/go-redis/v9" "github.com/spf13/viper" "go.uber.org/zap" ) var ctx = context.Background() var Rdb *redis.Client func init() { Rdb = redis.NewClient(&redis.Options{ Addr: viper.GetString("redis.host"), Password: viper.GetString("redis.password"), // no password set DB: viper.GetInt("redis.db"), // use default DB }) _, err := Rdb.Ping(ctx).Result() if err != nil { zap.L().Error("Redis连接出错 " + err.Error()) panic("Redis连接出错,请检查参数: " + err.Error()) } }