Created
April 25, 2023 07:13
-
-
Save msadakov/d85a248e7cf700f51c6a5b5a55461c8f to your computer and use it in GitHub Desktop.
Выход из бесконечного цикла, если оба канала закрыты
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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