Updated April 14, 2023
Introduction to Golang Concurrency
In Go language concurrency means running multiple operations at the same time, as we have already discussed goroutine on the go language which blocks all other once call and once completed then another call will be entertained, so with the help of the concurrency we will be able to perform multiple operations at the same time, we can not use concurrency directly we can use it with help of the sleep method over the goroutines, so there can be multiple goroutines inside one main goroutine and if the main goroutine will be halted or terminated then the other all goroutine will be terminated.
Syntax of Golang Concurrency
Below is a simple syntax for the concurrency of the go language, We can explain the below go, here in the below first we have written a function with the name name-of-function and this function contains several expressions. These expressions can be any calculation and some other tasks. Now suppose we wanted to run expression 1, expression 2, and expression 3 at the same time we want to execute the expression, or very simply we want to concurrently execute them. So we can use the sleep and this will allow us to control execute them at the same time.
Please see the below syntax for a better understanding.
func name-of-function(){
// expression 1
// expression 1
// expression 1
}
go name-of-function()
How Does Concurrency Work in Go language?
Before going to discuss the working of the concurrency in go language let us understand the basic concept and uses of the concurrency in the Go language. You have seen YouTube video, if you will see there are always two things that will be happening at one time one is the playing of the video and another is the buffering of the video. This means even you pause the video buffering happens, and after that even inter gone which means in offline mode also you will be able to play the video. This all happening because of the concurrency, which is allowing the application to run at one time, and at the same time, it allows us to buffer the video for capturing the video for offline mode. All these things are happening just because of the concurrency. So basically in concurrency first one particular task will run and in the meanwhile, sleep function will be called which will give space to run another task. We have already discussed the goroutine, basically, it is a call between two goroutines, go language there will be multi goroutine and there will be one main goroutine, every goroutine will fall under the main goroutine and if the main goroutine will terminate then other all go routine all be terminated. We can discuss point to point to the working of the concurrency of the.
- Concurrency allows you to use goroutine which is blockers, you know once a go routine gets called it will block for execution of others.
- Hence with the help of sleep, we will be able to call concurrent goroutine.
- Every goroutine belongs to the main goroutine in go language and if the main goroutine will be terminated then all other goroutines will also terminate.
Examples to Implement Golang Concurrency
In the below we have given three examples, these examples explain the way of working of the concurrency. If we wanted to execute this code then we can take the example and pass them on any file and run the command go run filename.go and we can see the output.
Example #1
Below is a simple example where we are calling a function show and this call continuously prints the data, but here we are not printing data in the concurrency, which means we are printing one by one.
Code:
package main
import "fmt"
func show(name string) {
for i := 0; i< 6; i++ {
fmt.Println(name)
}
}
func main() {
go show("Ajay")
show("Ranjan")
}
Output:
Example #2
In the below example we have a function called the show and that function is defined with the help of the go keyword and then this show function called, inside the function definition we have written the logic to print the param passed to the function show and each time when it get called it will be print the param. Now the question is we want to execute then concurrently which means together execution of these two go show(“Ajay”) and show(“Ranjan”). So we have written a sleep function inside the show definition, it will allow two simultaneous calls to the function for executions. See the example output first it is printing Ajay and then it printing Ranjan, this way it will keep printing both the names are getting a chance to print.
Code:
package main
import (
"fmt"
"time"
)
func show(name string) {
for i := 0; i< 6; i++ {
time.Sleep(1 * time.Second)
fmt.Println(name)
}
}
func main() {
go show("Ajay")
show("Ranjan")
}
Output:
Example #3
Below is another example for printing them out in a concurrent way, we have defined a normal function inside the main, and inside the function we have taken some printing statements at the same time we are also printing some data inside the main function. One we are printing on the above of the function and another we are printing after the function. So after the function, we have used the sleep to allow concurrent printing of all along with the printing of the go function.
Code:
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Hello , friends how are you")
go func() {
fmt.Println("How are you doing ")
}()
time.Sleep(2 * time.Second)
fmt.Println("We have done with the discussion")
}
Output:
Conclusion
From this tutorial we learned the basic concept of concurrency in the Go language, we learned the syntax and the working way of the concurrency in the Go language. We also focus on some of the examples with their explanation which revealed the working of the concurrency in go language.
Recommended Article
This is a guide to Golang Concurrency. Here we discuss the introduction, syntax and working of Golang Concurrency along with examples and code implementation. You can also go through our other suggested articles to learn more –