-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (33 loc) · 939 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// main.go
package main
import (
"context"
"fmt"
"log"
"github.com/redis/go-redis/v9"
)
var ctx = context.Background()
func main() {
// Create a new Redis client
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6380", // Address of the Redis server
})
// Ping the Redis server to check the connection
_, err := rdb.Ping(ctx).Result()
if err != nil {
log.Fatalf("could not connect to Redis: %v", err)
}
fmt.Println("Connected to Redis")
// Set a key-value pair
err = rdb.Set(ctx, "mykey", "myvalue", 0).Err()
if err != nil {
log.Fatalf("could not set key in Redis: %v", err)
}
fmt.Println("Key set successfully")
// Get the value of the key
val, err := rdb.Get(ctx, "mykey").Result()
if err != nil {
log.Fatalf("could not get key from Redis: %v", err)
}
fmt.Printf("The value of 'mykey' is: %s\n", val)
}