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
+34.5k Golang : Smarter Error Handling with strings.Contains()
+12.1k Golang : Get month name from date example
+30.7k Golang : Calculate percentage change of two values
+22.3k Golang : Convert Unix timestamp to UTC timestamp
+10.4k Golang : How to delete element(data) from map ?
+17.3k Golang : delete and modify XML file content
+32k Golang : Math pow(the power of x^y) example
+18.2k Golang : Read binary file into memory
+13.5k Golang : Check if an integer is negative or positive
+5.7k Unix/Linux : How to open tar.gz file ?
+21.1k Curl usage examples with Golang
+24.2k Golang : GORM read from database example