Continuing with our lockdown learning of Go language. Here’s a video where we discuss Exchanging data using Channels in Golang
package main
import "fmt"
func main() {
channel := make(chan int)
//Let's square a number in a goroutine
//Pass the number to the goroutine using channel
go squareNum(channel)
channel <- 5
x := <- channel
fmt.Println("Squared", x)
//Let's increment the squared number in a goroutine
go incrementNum(channel)
channel <- x
fmt.Println("Incremented", <- channel)
}
func incrementNum(channel chan int) {
num := <- channel
num += 1
channel <- num
}
func squareNum(channel chan int) {
num := <- channel
num *= num
channel <- num
}
You can watch other videos in Go lang in the links below
Continuing with our lockdown learning of Go language. Here’s a video where we discussChannels in Golang
package main
import "fmt"
func main() {
//Channels are pipelines used to communicate with goroutines
//channel is used to pass and receive integer data
var channel chan int
fmt.Println(channel)
//Initialized using make
channel = make(chan int)
fmt.Println(channel)
//You can send data to a channel using <-
channel <- 10
//You can receive data from a channel using <-
data := <- channel
fmt.Println(data)
//Send, receive operations are blocking
//So you need to have a goroutine receiving and sending data
//Let's discuss that in the next video
}
You can watch other videos in Go lang in the links below
Continuing with our lockdown learning of Go language. Here’s a video where we Play more with WaitGroup in Golang
package main
import (
"fmt"
"sync"
)
func main() {
wg := sync.WaitGroup{} //a structure
fmt.Println(wg)
wg.Add(1)
//go doSomething(wg) //Passing a copy of waitgroup. So no modifications to the original
go doSomething(&wg)
//wg.Add(1) //Wait for 1 go routine to finish
//wg.Add(2)//Wait for 2 go routines to finish. But we have only one GoRoutine. hmm
//This delta: <number> is equal to the number of times Done() gets called from goroutines
//go func() {
// fmt.Println("...Inside goroutine 1")
// wg.Done()
// wg.Done() //Silly isn't it?
// wg.Done()//panic: sync: negative WaitGroup counter
//
//}()
wg.Wait()
fmt.Println("****END")
}
func doSomething(wg *sync.WaitGroup) {
fmt.Println("...DoSomething")
wg.Done()
}
You can watch other videos in Go lang in the links below
Continuing with our lockdown learning of Go language. Here’s a video on WaitGroup in goroutines in Golang
A goroutine may have to wait for another goroutine to complete. Using time.Sleep() is a very crude way to wait. Here’s where WaitGroup comes to picture. This is similar to CountDownLatch in Java
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
func main() {
//We need to ask main goroutine to wait for fixIssues() goroutine to complete its task
wg := sync.WaitGroup{}
wg.Add(1) //WaitGroup is like a countdown system. It will wait till the count is 1
go fixIssues(&wg)
//time.Sleep(time.Second * 5)//Not intuitive and correct too
//Main go routine is waiting now
//Blocking statement
wg.Wait()
fmt.Println("***Enough! I am done waiting")
}
func fixIssues(wg *sync.WaitGroup) {
defer wg.Done() //When the goroutine is completed, it sends a signal ie., it has completed for the lock to release
i := 0
rand.Seed(time.Now().UnixNano())
iterations := rand.Intn(20)
fmt.Println("Time required:", iterations, "seconds")
for i < iterations {
fmt.Println("Fixing Issues ...")
time.Sleep(time.Second * 1)
i++
}
}
You can watch other videos in Go lang in the links below