Golang Program to Create Two Goroutines


When working with go language there may be instances where you need to create two goroutines for parallel processing, asynchronous operations and more. In this go language article we will explore how to create two goroutines using anonymous functions, named functions as well as using function calls.In golanguage a goroutine is an independent concurrent function, it enables concurrent programming by allowing the functions to run concurrently.

Syntax

time.Sleep(duration)
  • It is a built-in go language function used to pause the execution of a program.

  • It take only one argument “duration”.

  • duration: it is the length of time you want to sleep.

Algorithm

  • Launch the main goroutine.

  • Create a new goroutine within the primary goroutine by calling an anonymous function

  • Loop from i = 1 to i = 4 in the first goroutine. "Goroutine 1:" is printed, followed by the current value of i. 500 milliseconds of sleep.

  • Create another goroutine within the primary goroutine by calling another anonymous function.

  • Loop from i = 1 to i = 4 in the second goroutine. "Goroutine 2:" is printed, followed by the current value of i. 500 milliseconds of sleep.

  • The main goroutine sleeps for 2 seconds after generating the goroutines to allow the child goroutines to execute.

Example 1

In this example, we are going to create two goroutines using anonymous functions, this method is recommended for simple go routine setups. In the code given below we are going to creates two goroutines and print the message using a loop.

package main

import (
   "fmt"
   "time"
)

func main() {
   go func() {
      for i := 1; i <= 4; i++ {
	     fmt.Println("Goroutine 1:", i)
		 time.Sleep(time.Millisecond * 500)
	  }
   }()

   go func() {
      for i := 1; i <= 4; i++ {
	     fmt.Println("Goroutine 2:", i)
         time.Sleep(time.Millisecond * 500)
      }
   }()

   time.Sleep(time.Second * 2)
}

Output

Goroutine 2: 1 
Goroutine 1: 1 
Goroutine 1: 2 
Goroutine 2: 2 
Goroutine 1: 3 
Goroutine 2: 3 
Goroutine 2: 4 
Goroutine 1: 4 

Example 2

In this example, we are going to create two separate named functions and each function is going to be executed as a go routine. In the code given below two goroutines run concurrently and print a message along with the number.

package main

import (
   "fmt"
   "time"
)

func printMessage(message string) {
   for i := 1; i <= 6; i++ {
      fmt.Println(message, i)
      time.Sleep(time.Millisecond * 500)
	}
}

func main() {
   go printMessage("Goroutine 1:")
   go printMessage("Goroutine 2:")

   time.Sleep(time.Second * 2)
}

Output

Goroutine 1: 1 
Goroutine 2: 1 
Goroutine 1: 2 
Goroutine 2: 2 
Goroutine 2: 3 
Goroutine 1: 3 
Goroutine 1: 4 
Goroutine 2: 4 
Goroutine 2: 5 

Example 3

In this example we are not defining functions directly, rather invoking existing functions by passing their name as argument.In the code given below we are going to print numbers for a specific count using two goroutines that run concurrently.

package main

import (
   "fmt"
   "time"
)

func printMessage(message string, count int) {
   for i := 1; i <= count; i++ {
      fmt.Println(message, i)
      time.Sleep(time.Millisecond * 500)
	}
}

func main() {
   go printMessage("Goroutine 1:", 5)
   go printMessage("Goroutine 2:", 5)

   time.Sleep(time.Second * 3)
}

Output

Goroutine 2: 1 
Goroutine 1: 1 
Goroutine 1: 2 
Goroutine 2: 2 
Goroutine 2: 3 
Goroutine 1: 3 
Goroutine 1: 4 
Goroutine 2: 4 
Goroutine 2: 5 
Goroutine 1: 5 

Conclusion

In this article we have discussed how we can create two goroutines in one language. We have looked at three different approaches to establishing two goroutines that are anonymous functions, named functions, and using function calls. Goroutine enables concurrent programming by allowing the functions to run concurrently.

Updated on: 13-Jul-2023

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements