Cannot use ‘**‘ (type any) as the type string

 1. 场景再现


	addr := viper.Get("redis.addr")
	password := viper.Get("redis.password")
	db := viper.Get("redis.db")
	RedisCache = redis.NewClient(&redis.Options{
		Addr:     addr, // Cannot use 'addr' (type any) as the type string
		Password: "",
		DB:       0,
	})

2. 原因分析

类型不正确

3. 解决办法

转成对应的类型即可

.(string)
.(int)
addr := viper.Get("redis.addr")
	password := viper.Get("redis.password")
	db := viper.Get("redis.db")
	RedisCache = redis.NewClient(&redis.Options{
		Addr:     addr.(string),
		Password: password.(string),
		DB:       db.(int),
	})

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/133840230