fatal error: all goroutines are asleep - deadlock!
package main
import (
"fmt"
"time"
)
var numChan chan int
var labelChan <-chan string
func main() {
input := []int{3, 1, 8, 7, 5, 9, 2}
numChan = make(chan int)
go func() {
labelsReceiver(labelChan)
}()
time.Sleep(time.Millisecond) // Так попытался убедиться, что получатель стартует первым. Не знаю как правильно сделать.
go func() {
labelChan = labelsEmitter(numChan)
}()
for _, num := range input {
fmt.Println(num)
numChan <- num
}
}
func labelsEmitter(numbers chan int) <-chan string {
fmt.Println("go labelsEmitter")
out := make(chan string, 3)
defer close(out)
for num := range numbers {
time.Sleep(time.Millisecond * 200)
str := fmt.Sprintf("Number %d", num)
out <- str
}
return out
}
func labelsReceiver(labelChanRcv <-chan string) {
fmt.Println("go labelsReceiver")
for label := range labelChanRcv {
fmt.Println(label)
}
}
out := make(chan string, 3)
package main
import (
"fmt"
"time"
)
var numChan chan int
var outChan chan string
func main() {
input := []int{3, 1, 8, 7, 5, 9, 2}
numChan = make(chan int)
outChan = make(chan string)
go func() {
labelsReceiver(outChan)
}()
time.Sleep(time.Millisecond) // Так попытался убедиться, что получатель стартует первым. Не знаю как правильно сделать.
go func() {
labelsEmitter(numChan, outChan)
}()
for _, num := range input {
fmt.Println(num)
numChan <- num
}
close(numChan)
close(outChan)
}
func labelsEmitter(numbers chan int, outChan chan string) {
fmt.Println("go labelsEmitter")
//out := make(chan string, 3)
//defer close(out)
for num := range numbers {
time.Sleep(time.Millisecond * 200)
str := fmt.Sprintf("Number %d", num)
outChan <- str
}
return
}
func labelsReceiver(labelChanRcv <-chan string) {
fmt.Println("go labelsReceiver")
for label := range labelChanRcv {
fmt.Println("label", label)
}
}