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
+5.2k Javascript : Generate random key with specific length
+12.5k Javascript : Prompt confirmation before exit
+4.8k Fix fatal error: evacuation not done in time problem
+14.9k Golang : Find out mime type from bytes in buffer
+3.8k Linux/MacOSX : How to symlink a file?
+5.9k Golang : Decode XML data from RSS feed
+7.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+18.3k Golang : Archive directory with tar and gzip
+6.2k Golang : Of hash table and hash map
+12.1k Golang : Read from buffered reader until specific number of bytes
+12.1k Facebook PHP getUser() returns 0
+14.5k Golang : How to reverse elements order in map ?