-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.go
44 lines (36 loc) · 874 Bytes
/
test.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
41
42
43
44
package main
import (
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
func main() {
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 0})
println("rdb", rdb)
// Creating two channels
ch1 := make(chan string)
ch2 := make(chan string)
// Launching goroutines to send data to the channels after delays
go func() {
time.Sleep(2 * time.Second)
ch1 <- "message from ch1"
}()
go func() {
time.Sleep(4 * time.Second)
ch2 <- "message from ch2"
}()
// go func() {
// time.Sleep(1 * time.Second)
// ch2 <- "message from ch2 again"
// }()
go time.Sleep(time.Second * 4)
// Using select to wait on multiple channels
select {
case msg := <-ch1:
fmt.Println("Received:", msg)
case msg := <-ch2:
fmt.Println("Received:", msg)
case <-time.After(1 * time.Second):
fmt.Println("Timeout: No messages received within 3 seconds")
}
}