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
+17.2k Golang : How to get hour, minute, second from time?
+24.9k Golang : Convert(cast) string to uint8 type and back to string
+6.3k Golang : alternative to os.Exit() function
+4.4k Golang : Issue HTTP commands to server and port example
+11.8k Python : Convert IPv6 address to decimal and back to IPv6
+12.2k Golang : Read XML elements data with xml.CharData example
+8k Golang : Intercept and compare HTTP response code example
+11.1k Golang : How to parse plain email text and process email header?
+7.8k Golang : Random integer with rand.Seed() within a given range
+13.6k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+9.6k Golang : Fix go.exe is not compatible with the version of Windows you're running
+6.8k Golang : Error reading timestamp with GORM or SQL driver