Last active
April 29, 2023 07:18
-
-
Save A9u/c45c93a8c5eeb644dc3ce02f5834588e to your computer and use it in GitHub Desktop.
go routine concurrency example to print sequential numbers using 2 goroutines - one for odd and one for even.
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" | |
"time" | |
) | |
func printNum(waitCh, pauseCh chan int, start, end int) { | |
fmt.Println("Inside print") | |
<-waitCh | |
for i := start; i <= end; i += 2 { | |
fmt.Println(i) | |
pauseCh <- i | |
<-waitCh | |
} | |
close(pauseCh) | |
} | |
func main() { | |
pauseCh := make(chan int) | |
waitCh := make(chan int) | |
go printNum(waitCh, pauseCh, 0, 6) | |
go printNum(pauseCh, waitCh, 1, 6) | |
waitCh <- 1 | |
time.Sleep(2 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment