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.

Test the code

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

#devdlearningblogs #golanglearningblocks #golang

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by DevD from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 98914.40
ETH 3374.27
USDT 1.00
SBD 3.08