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.4k Golang : Convert an executable file into []byte example
+6.4k Golang : Another camera capture GUI application with GTK and OpenCV
+4.7k CodeIgniter : form input set_value cause " to become & quot
+8.1k Golang : Resolve domain name to IP4 and IP6 addresses.
+6.3k Golang : Append and add item in slice
+10.5k Golang : Validate email address
+24.9k Golang : Get time.Duration in year, month, week or day
+11.7k Golang : How to filter a map's elements for faster lookup
+12.9k Golang : Delete files by extension
+4.4k Golang : Missing Subversion command
+25k Golang : Detect (OS) Operating System
+4.6k Golang : Find the shortest line of text example