Skip to content

Instantly share code, notes, and snippets.

@ulexxander
Created March 17, 2022 11:50
Show Gist options
  • Select an option

  • Save ulexxander/3f5ec3418573e99a9f0aee7ff9e06b32 to your computer and use it in GitHub Desktop.

Select an option

Save ulexxander/3f5ec3418573e99a9f0aee7ff9e06b32 to your computer and use it in GitHub Desktop.
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