Created
February 15, 2022 16:18
-
-
Save romanitalian/c9aa7fd9b8c4522ff340f222b53791fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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