Skip to content

Instantly share code, notes, and snippets.

@msadakov
Created April 25, 2023 07:13
Show Gist options
  • Save msadakov/d85a248e7cf700f51c6a5b5a55461c8f to your computer and use it in GitHub Desktop.
Save msadakov/d85a248e7cf700f51c6a5b5a55461c8f to your computer and use it in GitHub Desktop.
Выход из бесконечного цикла, если оба канала закрыты
package main
import "fmt"
func main() {
var ch = make(chan int)
close(ch)
var ch2 = make(chan int)
go func() {
for i := 1; i < 10; i++ {
ch2 <- i
}
close(ch2)
}()
for {
select {
case x, ok := <-ch:
fmt.Println("ch1", x, ok)
if !ok {
ch = nil
}
case x, ok := <-ch2:
fmt.Println("ch2", x, ok)
if !ok {
ch2 = nil
}
}
if ch == nil && ch2 == nil {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment