Skip to content

Instantly share code, notes, and snippets.

@A9u
Last active April 29, 2023 07:18
Show Gist options
  • Save A9u/c45c93a8c5eeb644dc3ce02f5834588e to your computer and use it in GitHub Desktop.
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.
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