
goLang Learning Blocks ~ Concurrency design pattern using Channels
Hello!
Today I demonstrating concurrency design pattern FANin.
In this sample concurrency design pattern FANin is being demonstrated using GoRoutine, Channels, Range, Select
Imp points
Range will keep iterating and fetching the value until channel will be closed
Select is useful which we are dealing with multiple channels together and we need to fetch from the channel whichever is ready with value.
In this design pattern a single channel accepts inputs from multiple output channels.
package main
import "fmt"
func main() {
even := make(chan int)
odd := make(chan int)
fanin := make(chan int)
// Send, this will alunch a go routine, which will execute saparatly from mai routine
go sendChannel(even, odd)
go receiveChannel(even, odd, fanin)
// range on channel will iterate until channel will be closed
for v := range fanin {
fmt.Printf("FanIn Channel value %v \n", v)
}
fmt.Printf("About to Exit")
}
func sendChannel(e, o chan<- int) {
defer close(e)
defer close(o)
for index := 0; index < 10; index++ {
if index%2 == 0 {
e <- index
} else {
o <- index
}
}
}
func receiveChannel(e, o <-chan int, f int) {
var okay int
for {
select {
case v, ok := <-e: // v will capture the channel value and ok will capture the state of channel
if !ok { // If channel is closed then increase okay
fmt.Printf("Even channel closed: %v \n", ok)
okay++
} else {
fmt.Printf("Even channel Value received: %v \n", v)
f <- v // Assign the value to fanin channel
}
case v, ok := <-o:
if !ok {
fmt.Printf("Odd channel closed: %v \n", ok)
okay++
} else {
fmt.Printf("Odd channel Value received: %v \n", v)
f <- v
}
}
// Loop exit condition
if okay == 2 {
fmt.Printf("Loop ends: %v \n", okay)
close(f)
return
}
}
}
Cheers
Dev
Leave goLang Learning Blocks ~ Concurrency design pattern using Channels to:
Read more #devdlearningblogs posts
Best Posts From _devdcaptures
We have not curated any of devrajsinghrawat's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From _devdcaptures
- Programmer Analysis on Facebook Libra
- HyperLedger Fabric - Setting and Testing Raft based Orderer
- Facebook Libra
- India’s Own Space Station ! What does it Mean ?
- Taxibots on wheels to save fuel at airport !!
- UPI 2.0
- Sweden is building an electric road that will charge car as you drive
- Dr Subramanian Swamy Speaks To Republic TV After ED Attaches National Herald Properties
- Elon Musk Challenger? Super Sonic Hydrogen IC Engine
- goLang Learning Blocks ~ Concurrency design pattern using Channels