redis example
A non-relational (NoSQL) database.
SET/GET a key/value pair.
Documentation and reference,
GitHub Webpage
MAKE SURE YOU HAVE REDIS RUNNING
Obviously you need a redis server running,
Check you have one running,
redis-cli ping
RUN
First, make sure you have the go library
go get -u -v github.com/go-redis/redis
Then run,
go run redis.go
Your output should be,
PONG <nil>
jeff monkey
key2 does not exist
CHECK KEY/VALUE USING REDIS-CLI
Check your key value using command line,
redis-cli
get jeff
SET/GET KEY/VALUES IN GO
Lets dive into the code.
CREATE A CONNECTION TO REDIS SERVER
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
pong, err := client.Ping().Result()
fmt.Println(pong, err)
// Output: PONG <nil>
SET A KEY/VALUE
err = client.Set("jeff", "monkey", 0).Err()
GET A KEY/VALUE
val, err := client.Get("jeff").Result()
fmt.Println("jeff", val)
// Output: jeff monkey