Skip to content

Instantly share code, notes, and snippets.

@romanitalian
Created February 15, 2022 16:18
Show Gist options
  • Save romanitalian/c9aa7fd9b8c4522ff340f222b53791fe to your computer and use it in GitHub Desktop.
Save romanitalian/c9aa7fd9b8c4522ff340f222b53791fe to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/go-redis/redis"
)
func NewRedisClient() *redis.Client {
return redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
}
func RedisHelloWorld() {
// step: 0 - connect.
redisClient := NewRedisClient()
// "ping" Redis
pong, err := redisClient.Ping().Result()
if err != nil {
log.Fatal(err)
}
fmt.Println(pong)
// step: 1 - write.
err = redisClient.Set("name", "redis is awesome", 0).Err()
if err != nil {
log.Fatal(err)
}
// step: 2 - read.
val, err := redisClient.Get("name").Result()
if err != nil {
log.Fatal(err)
}
fmt.Println(val)
}
func main() {
RedisHelloWorld()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment