I'm new in golang
and I faced with the problem.
I have several channel
s.
Some payload gets to this channels in different time.
How I can get all values one by one from channels in the time when channel ready to spit it.
For example I wrote this code:
package main
import (
"fmt"
"time"
"math/rand"
)
func main() {
arr1 := []int8{1,2,3,4,5}
arr2 := []int8{6,7,8,9,10}
c1 := make(chan int8)
c2 := make(chan int8)
go func() {
for _, val := range arr1 {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
c1 <- val
}
}()
go func() {
for _, val := range arr2 {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
c2 <- val
}
}()
select {
case res1 := <- c1:
fmt.Println(res1)
case res2 := <- c2:
fmt.Println(res2)
}
fmt.Println("Hello, test")
}
But in this case, I get only first value, from one of the channels.
Please, give me advise how to solve my issue.
Link to go-play https://play.golang.org/p/FOmkP57YCyR