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.4k Golang : Display a text file line by line with line number example
+22.7k Golang : Upload to S3 with official aws-sdk-go package
+5.7k Elasticsearch : Shutdown a local node
+15.4k CodeIgniter/PHP : Create directory if does not exist example
+18.4k Golang : Compare floating-point numbers
+36.1k Golang : Read a text file and replace certain words
+4.6k Golang : Stop goroutine without channel
+6.5k Golang : Example of how to detect which type of script a word belongs to
+29.1k Golang : Calculate percentage change of two values
+3.8k MariaDB/MySQL : How to get version information
+7.4k Android Studio : Image button and button example
+4.9k Golang : Struct field tags and what is their purpose?