Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math/rand"
- "time"
- )
- func asChan(vs ...int) <-chan int {
- c := make(chan int)
- go func() {
- for _, v := range vs {
- c <- v
- time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
- }
- close(c)
- }()
- return c
- }
- func merge(a, b <-chan int) <-chan int {
- c := make(chan int)
- go func() {
- for {
- select {
- case v, ok := <-a: // Как только все данные будут прочитаны, будут слаться пустые значения
- c <- v
- fmt.Println(ok)
- case v, ok := <-b:
- c <- v
- fmt.Println(ok)
- }
- }
- }()
- return c
- }
- func main() {
- a := asChan(1, 3, 5, 7)
- b := asChan(2, 4, 6, 8)
- c := merge(a, b)
- for v := range c {
- fmt.Println(v)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment