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
+4.8k Linux : How to set root password in Linux Mint
+35.5k Golang : Save image to PNG, JPEG or GIF format.
+13.9k Elastic Search : Mapping date format and sort by date
+8.5k Golang : Combine slices but preserve order example
+11.4k Golang : How to detect a server/machine network interface capabilities?
+34.7k Golang : Upload and download file to/from AWS S3
+13.9k Golang : Convert IP version 6 address to integer or decimal number
+7.2k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+6.3k Golang : Map within a map example
+13k Android Studio : Password input and reveal password example
+15.6k Golang : Read large file with bufio.Scanner cause token too long error
+14.6k Golang : Search folders for file recursively with wildcard support