forked from steger/pr3-sose2026
select
parent
c47b686210
commit
78d18901c1
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -8,12 +9,46 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
ch1 := make(chan string)
|
ch1 := make(chan string)
|
||||||
|
ch2 := make(chan string)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
||||||
ch1 <- "Message from channel 1"
|
ch1 <- "Message from channel 1"
|
||||||
}()
|
}()
|
||||||
|
|
||||||
msg1 := <-ch1
|
go func() {
|
||||||
println("Received:", msg1)
|
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
||||||
|
ch2 <- "Message from channel 2"
|
||||||
|
}()
|
||||||
|
|
||||||
|
count := 10
|
||||||
|
var channels []chan string
|
||||||
|
for i := range count {
|
||||||
|
ch := make(chan string)
|
||||||
|
channels = append(channels, ch)
|
||||||
|
go func() {
|
||||||
|
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
||||||
|
ch <- fmt.Sprintf("Message from dynamic channel %d", i)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
fanIn := make(chan string)
|
||||||
|
for _, ch := range channels {
|
||||||
|
go func(c chan string) {
|
||||||
|
fanIn <- <-c
|
||||||
|
}(ch)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case msg1 := <-ch1:
|
||||||
|
println("Received:", msg1)
|
||||||
|
case msg2 := <-ch2:
|
||||||
|
println("Received:", msg2)
|
||||||
|
case msg := <-fanIn:
|
||||||
|
println("Received:", msg)
|
||||||
|
case <-time.After(3 * time.Second):
|
||||||
|
println("Timeout occurred")
|
||||||
|
default:
|
||||||
|
println("Nothing")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue