We are going to write a program to print natural numbers using two go routines in go. One go routine will be responsible to print odd numbers and another go routine will be responsible to print even numbers.
Algorithm:
- Create two go routines i.e odd and even from main with bool type channel
- In odd / even function, Add channel to receive value next to for loop
- In odd / even function, Add channel to send value at the end of for loop
Multiple example of program to print natural numbers using two go routines:
Code 1:
package main
import (
"fmt"
"sync"
)
func print_odd(no_chan chan int, is_done chan bool, wg *sync.WaitGroup) {
defer wg.Done()
for no := range no_chan {
if no%2 != 0 {
fmt.Println(no)
is_done <- true
} else {
no_chan <- no
}
}
}
func print_even(no_chan chan int, is_done chan bool, wg *sync.WaitGroup) {
defer wg.Done()
for no := range no_chan {
if no%2 == 0 {
fmt.Println(no)
is_done <- true
} else {
no_chan <- no
}
}
}
func main() {
var wg sync.WaitGroup
no_chan := make(chan int)
is_done := make(chan bool)
wg.Add(2)
go print_odd(no_chan, is_done, &wg)
go print_even(no_chan, is_done, &wg)
for i := 0; i <= 10; i++ {
no_chan <- i
<-is_done
}
close(no_chan)
close(is_done)
wg.Wait()
}
Code 2:
package main
import (
"fmt"
"sync"
)
func print_odd(ch chan int, is_done chan bool, wg *sync.WaitGroup) {
defer wg.Done()
for val := range ch {
fmt.Println(val)
is_done <- true
}
}
func print_even(ch chan int, is_done chan bool, wg *sync.WaitGroup) {
defer wg.Done()
for val := range ch {
fmt.Println(val)
is_done <- true
}
}
func main() {
var wg sync.WaitGroup
var odd_ch = make(chan int)
var even_ch = make(chan int)
var is_done = make(chan bool)
wg.Add(2)
go print_odd(odd_ch, is_done, &wg)
go print_even(even_ch, is_done, &wg)
for i := 0; i <= 100; i++ {
if i%2 == 0 {
even_ch <- i
} else {
odd_ch <- i
}
<-is_done
}
close(odd_ch)
close(even_ch)
wg.Wait()
}
Code 3:
package main
import (
"fmt"
"sync"
)
func even(ch chan int) {
for {
select {
case no := <- ch:
fmt.Println(no)
ch <- -1
}
}
}
func odd(ch chan int) {
for {
select {
case no := <- ch:
fmt.Println(no)
ch <- -1
}
}
}
func main() {
wg := sync.WaitGroup{}
m := sync.Mutex{}
wg.Add(2)
ch := make(chan int)
go even(ch)
go odd(ch)
for i := 1; i <= 10; i++ {
if (i & 1) == 0 {
// Adding lock because wait until this number prints
m.Lock()
ch <- i
<- ch
m.Unlock()
} else {
m.Lock()
ch <- i
<- ch
m.Unlock()
}
}
wg.Done(); wg.Done()
close(ch)
return
}
Code 4:
package main
import (
"fmt"
"sync"
)
func print_odd(odd chan int) {
for {
select {
case n := <- odd:
fmt.Println("Odd: ", n)
}
}
}
func print_even(even chan int) {
for {
select {
case n := <- even:
fmt.Println("Even: ", n)
}
}
}
func main() {
var wg sync.WaitGroup
wg.Add(2)
odd := make(chan int)
even := make(chan int)
go print_odd(odd)
go print_even(even)
for i := 0; i < 10; i++{
if i % 2 == 0 {
even <- i
} else {
odd <- i
}
}
close(odd); close(even)
wg.Done();
wg.Done()
wg.Wait()
}
Output:
1
2
3
4
5
..
..
..
To learn more about technical stuff, Please refer given below link.
https://techieindoor.com/go-lang-tutorial/
References:
https://golang.org/doc/
https://golang.org/pkg/