go-learning/5concurrency/channels2.go

44 lines
627 B
Go
Raw Normal View History

2023-08-26 10:51:33 +03:00
package main
import (
"fmt"
"time"
)
func producer(chnl chan int) {
for i := 0; i < 10; i++ {
time.Sleep(100 * time.Millisecond)
chnl <- i
}
close(chnl)
}
func outer(chn2 chan int) {
for i := 10; i < 100; i++ {
chn2 <- i
}
close(chn2)
}
func main() {
ch, ch2 := make(chan int), make(chan int)
go producer(ch)
go outer(ch2)
for {
v, ok := <-ch
if ok == false {
v, ok2 := <-ch2
if ok2 == false {
break
}
fmt.Println("Received2 ", v, ok)
continue
}
fmt.Println("Received ", v, ok)
}
ch3 := make(chan int)
go producer(ch3)
for v := range ch3 {
fmt.Println("Received ", v)
}
}