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
+10.2k Golang : Get login name from environment and prompt for password
+10k Golang : Convert octal value to string to deal with leading zero problem
+11k Golang : Sieve of Eratosthenes algorithm
+14.4k Golang : How to shuffle elements in array or slice?
+26k Golang : How to read integer value from standard input ?
+6.4k Unix/Linux : Use netstat to find out IP addresses served by your website server
+16.5k Golang : Convert slice to array
+12.5k Golang : Search and extract certain XML data example
+13.9k Golang : Convert spaces to tabs and back to spaces example
+20.6k Golang : Pipe output from one os.Exec(shell command) to another command
+10k CodeIgniter : Load different view for mobile devices
+7.9k Golang : Load DSA public key from file example