my net house

WAHEGURU….!

Art of Concurrency in Golang Part-1

There are multiple Ways to talk about Concurrency in Golang but I would only Go with Channels, Why?

Because it is Ridiculously Simple and Efficient Solution. To Do more and More Concurrency in GoLang you need two Channels, One would be Sender and other would be Receiver. To Achieve Concurrency all of yo need to do following tings as well:

  1. Create a Function that takes at least Channel as Argument.
  2. Do all the required Processes/Calculations and Send that Value to Channel-Argument.
    That’s IT!  Now run that Function as Go-Routine, Run as Mnay as Go-Rountines you want, Put inside For Loop if you are looking for Parallel-Execution of tings!!

Following Example is Ridiculously Simple we are calling sun() Multiple times and using only one Channel to Receive values from Go-Routines. ad when you get those results you can use any kind operations on those Channels as well.

package main

import “fmt”

func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
c <- sum // send sum to c
}

func main() {
a := []int{7, 2, 8, -9, 4, 0}

c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
x, y := <-c, <-c // receive from c

fmt.Println(x, y, x+y)
}

Note* : – There is no Return Statement in the above Function. 🙂 😉

Let's take to next approach now, What we will see now with Next implementation of Golang thing:

package main

import (
"fmt"
"sort"
)

func sum(a []int, c chan int) {
sum := 0
for _, v := range a {
sum += v
}
c <- sum // send sum to c
}

func largest(a []int, c chan int) {
sort.Ints(a)
fmt.Println(a[len(a)-1])
c <- a[len(a)-1]
}

func main() {
a := []int{7, 2, 8, -9, 4, 0}

c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
go largest(a, c)
x, y, z := <-c, <-c, <-c // receive from c
fmt.Println(x, y, x+y)
fmt.Println("this is largest number", z)

}

Buffered Channels: 

When you want to limit the Receiving Value of Channels. You can Create Bufered Channels.

package main

import "fmt"

func main() {
    c := make(chan int, 2)
    c <- 1
    c <- 2
    fmt.Println(<-c)
    fmt.Println(<-c)
}

When you have Spin one Function/GO-Routine Multiple times by taking care of the Things like Dead-Lock and Getting the Results in the Synchronous Form you can go with kind of Following code.

Note* We are able to give capacity to Channel and sing that Capacity Go Routine can be Run Multiple Times to make sure that all the Channels are Running as per the Requirements.

Use Of “range{}” is important for the Channel.  

package main

import (
“fmt”
)

func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}

func main() {
c := make(chan int, 1)
fmt.Println("this is channel", c)
fmt.Println("this is Capacitty of channel", cap(c))
go fibonacci(cap(c), c)
for i := range c {
fmt.Println(i)
}
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: