Last active
September 11, 2022 11:12
-
-
Save shyamvlmna/1d8d75c511608898a2115ab8d7074d20 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 ( | |
"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