Assuming that you have a Go program that has three duties to perform;
- Accepts input from users 2.Verify the status of network connectivity
- Every 5 minutes, update the stock exchange price Isn't it a cool program? Hold on. Wait..
What if the customer had to pee or make himself a coffee? What's happening? Our program continue to wait until an input is entered by the user. This is where the actual issue comes in. The program becomes idle and other tasks that need to be accomplished begin to wait until power is transferred to them from the input task. This would not only decrease our program's efficacy, but also increase the time for the program to be executed.
What if we can run several task at once?, wouldnt that save us a lot of time and allow each tasks run independent of other tasks?. How do we achieve this. Go splits a big program into several chunck of tasks called gorountines. lets get this example solved. we have a program that has two functions: a and b
func a(){
for i:=0;i<=50;i++{
fmt.Println("a")
}
}
func b(){
for i:=0;i<=50;i++{
fmt.Println("b")
}
}
func main(){
a()
b()
}
Each function loops 50 times to print out 'a' and 'b' respectively. it is expected that func a runs first then func b follows and the program exits
backendpapa@esereomo-Backendpapa:~/Go projects/goroutine$ go run main.go
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
what if we wanted each of functions to print a and b for every 1 or 2 seconds, what happens.
func a(){
for i:=0;i<=50;i++{
time.Sleep(time.Second*1)
fmt.Println("a")
}
}
func b(){
for i:=0;i<=50;i++{
time.Sleep(time.Second*2)
fmt.Println("b")
}
}
func main(){
a()
b()
}
When we run this program we get this outputs:
backendpapa@esereomo-Backendpapa:~/Go projects/goroutine$ go run main.go
aaaa
backendpapa@esereomo-Backendpapa:~/Go projects/goroutine$ go run main.go
aaaaaaaaaaaaaaaaaaaaaaaa
backendpapa@esereomo-Backendpapa:~/Go projects/goroutine$ go run main.go
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb
backendpapa@esereomo-Backendpapa:~/Go projects/goroutine$ go run main.go
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
backendpapa@esereomo-Backendpapa:~/Go projects/goroutine$ go run main.go
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Ah, oops! It took our program 150 seconds to complete the execution, which is not too good. This is where Goroutines are required to reduce the execution time, we have to run these two programs simultaneously and also allow other functions to run independently. we initialize go routines using the 'go' statements. it will be used on the called functions in Go's main function
func a(){
for i:=0;i<=50;i++{
time.Sleep(time.Second*1)
fmt.Println("a")
}
}
func b(){
for i:=0;i<=50;i++{
time.Sleep(time.Second*2)
fmt.Println("b")
}
}
func main(){
go a()
go b()
time.Sleep(time.Second*100)
}
Our tasks are now executed independently of each functions. This does not only save time but also increases efficiency For the maintime we ensure that our main function is still running for 100seconds to prevent it from sleeping.
This is our outputs. Go routines switches tasks between func a and func b
backendpapa@esereomo-Backendpapa:~/Go projects/goroutine$ go run main.go
abaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabbbbbbbbbbbbbbbbbbbbbbbb
Go routine uses the concurrency concept which we will talk about in the next series. I will be expecting your feedbacks in the comment below. Have a great day dev!