Golang : Switch Redis database redis.NewClient
Problem : Your default Redis connection is using database 0 and you want to get some keys from different Redis database during runtime.
Solution : Create another Redis client with the database index.
// Create a new Redis client with different db
redisClient := redis.NewClient(&redis.Options{
Addr: os.Get("REDIS_HOST") + ":" + os.Get("REDIS_PORT"),
Password: os.Get("REDIS_PASSWORD"),
DB: 9, // use different database than 0
})
defer redisClient.Close()
// do what you want here
// for example
iter := redisClient.Scan(ctx, 0, "hscodes:*", 0).Iterator() // scan to get all the keys matching wildcard pattern.
Reference :
See also : Golang : PGX CopyFrom to insert rows into Postgres database
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.2k Android Studio : How to detect camera, activate and capture example
+8.1k Golang : Implementing class(object-oriented programming style)
+8k Golang : Reverse text lines or flip line order example
+7.3k Android Studio : AlertDialog to get user attention example
+16.6k Golang : Set up source IP address before making HTTP request
+13.8k Golang : concatenate(combine) strings
+7.3k Golang : Rot13 and Rot5 algorithms example
+21.9k Golang : Securing password with salt
+23.5k Golang : Fix type interface{} has no field or no methods and type assertions example
+20.2k Android Studio : AlertDialog and EditText to get user string input example
+18.2k Golang : Example for RSA package functions
+9.9k Golang : Compare files modify date example