Created
March 17, 2022 11:50
-
-
Save ulexxander/3f5ec3418573e99a9f0aee7ff9e06b32 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
| "strconv" | |
| "time" | |
| ) | |
| func main() { | |
| intsChan := make(chan int) | |
| stringsChan := make(chan string) | |
| go sendInts(intsChan) | |
| go sendStrings(stringsChan) | |
| go receive(intsChan, stringsChan) | |
| time.Sleep(time.Second * 5) | |
| } | |
| func sendInts(intsChan chan int) { | |
| for i := 0; i < 10; i++ { | |
| time.Sleep(time.Millisecond * 100) | |
| intsChan <- i | |
| } | |
| } | |
| func sendStrings(stringsChan chan string) { | |
| for i := 0; i < 10; i++ { | |
| time.Sleep(time.Millisecond * 100) | |
| stringsChan <- strconv.Itoa(i) | |
| } | |
| } | |
| func receive(intsChan chan int, stringsChan chan string) { | |
| for i := 0; i < 5; i++ { | |
| select { | |
| case i := <-intsChan: | |
| println("int", i) | |
| intsChan = nil | |
| println("will not receive ints anymore") | |
| case s := <-stringsChan: | |
| println("string", s) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment