Skip to content

Instantly share code, notes, and snippets.

@shyamvlmna
Last active September 11, 2022 11:12
Show Gist options
  • Save shyamvlmna/1d8d75c511608898a2115ab8d7074d20 to your computer and use it in GitHub Desktop.
Save shyamvlmna/1d8d75c511608898a2115ab8d7074d20 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func main() {
intchan := make(chan int)
ticker := make(chan bool)
n := 100
go oddnums(n, intchan, ticker, &wg)
go evenums(n, intchan, ticker, &wg)
go func() {
for num := range intchan {
fmt.Println(num)
}
wg.Done()
}()
wg.Add(3)
ticker <- true
wg.Wait()
close(intchan)
}
func oddnums(n int, intchan chan int, ticker chan bool, wg *sync.WaitGroup) {
defer wg.Done()
i := 1
for i < n {
<-ticker
intchan <- i
ticker <- true
i += 2
}
}
func evenums(n int, intchan chan int, ticker chan bool, wg *sync.WaitGroup) {
defer wg.Done()
i := 2
for i < n {
<-ticker
intchan <- i
ticker <- true
i += 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment