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
+22.5k Golang : Strings to lowercase and uppercase example
+31.9k Golang : Validate email address with regular expression
+5.4k Golang : If else example and common mistake
+9.5k Javascript : Read/parse JSON data from HTTP response
+16.7k Golang : Get own process identifier
+31.2k Golang : bufio.NewReader.ReadLine to read file line by line
+8.1k Golang : Auto-generate reply email with text/template package
+11.9k Golang : Perform sanity checks on filename example
+12.5k Golang : Remove or trim extra comma from CSV
+27.7k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+14.6k Golang : Normalize unicode strings for comparison purpose
+5.7k Linux : Disable and enable IPv4 forwarding