Fetch Values from Goroutines
Channels are the most logical way to get a value from a goroutine. Channels are pipes that connect goroutines that are running at the same time. You can send values from one goroutine to channels and receive them from another goroutine or the asynchronous function.
Example
package main
import "fmt"
func sum(a, b int, res chan int) {
temp := a + b
res <- temp
return
}
func main() {
res := make(chan int, 1)
go sum(2, 3, res)
sumVal := <-res
fmt.Printf("Value: %d\n", sumVal)
close(res)
}

You can also try this code with Online C Compiler
Run Code
Output
Value: 5

You can also try this code with Online C Compiler
Run Code
Play and Pause Execution of Goroutine
We can utilize channels to play and pause goroutine execution. A channel facilitates communication between goroutines by acting as a conduit.
Example
package main
import (
"fmt"
"sync"
"time"
)
var i int
func work() {
time.Sleep(250 * time.Millisecond)
i++
fmt.Println(i)
}
func routine(command <-chan string, wg *sync.WaitGroup) {
defer wg.Done()
var status = "Play"
for {
select {
case cmd := <-command:
fmt.Println(cmd)
switch cmd {
case "Stop":
return
case "Pause":
status = "Pause"
default:
status = "Play"
}
default:
if status == "Play" {
work()
}
}
}
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
command := make(chan string)
go routine(command, &wg)
time.Sleep(1 * time.Second)
command <- "Pause"
time.Sleep(1 * time.Second)
command <- "Play"
time.Sleep(1 * time.Second)
command <- "Stop"
wg.Wait()
}

You can also try this code with Online C Compiler
Run Code
Output
1
2
3
4
Pause
Play
5
6
7
8
Stop

You can also try this code with Online C Compiler
Run Code
Advantages of Goroutines
- Goroutines are cheaper than threads.
- Goroutine is placed in a stack, which can expand and shrink in size depending on the program's requirements. Threads, on the other hand, have a set stack size.
- Goroutines can interact through the channel, which is specifically designed to prevent race situations when Goroutines access shared memory.
- Assume a software has a single thread with several Goroutines associated with it. If any Goroutine blocks the thread owing to resource constraints, the remaining Goroutines will be assigned to a new OS thread. The programmers are unaware of all of this information.
FAQs
-
What is the use of Add, Done, Wait for Waiting for Goroutines to Finish Execution?
Ans: To add a counter to the WaitGroup, use the Add method. To decrement the WaitGroup counter, the Done method of WaitGroup is scheduled using a defer statement. The Wait way of the WaitGroup type waits for all goroutines in the program to complete.
-
Why Define Critical Sections using Mutex?
Ans: A mutex is used to establish a crucial area around code, ensuring that only one goroutine can execute that code portion at a time.
Key Takeaways
In this article, we have extensively discussed Routines and learned the syntax and implementation by example, and also learned play and pause execution.
We hope that this blog enhances your knowledge regarding Routines, and if you would like to learn more, check out all articles on Concurrency in Go, Interview Preparation. Do upvote our blog to help other ninjas grow. Happy Coding!