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
+6.9k Golang : A simple forex opportunities scanner
+7.3k Golang : Handling Yes No Quit query input
+21.9k Golang : Securing password with salt
+42.7k Golang : Convert []byte to image
+11.4k Golang : Surveillance with web camera and OpenCV
+16.2k Golang : How to implement two-factor authentication?
+6.9k Golang : Get Alexa ranking data example
+8.3k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+6.5k Golang : Derive cryptographic key from passwords with Argon2
+14.3k Golang : Reset buffer example
+23.4k Find and replace a character in a string in Go
+4.9k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version