Multiple Goroutines

Prerequisite: Multiple Goroutines

A Goroutine is a function or method which executes independently and simultaneously in connection with any other Goroutines present in your program. Or in other words, every concurrently executing activity in Go language is known as a Goroutines. In Go language, you are allowed to create multiple goroutines in a single program. You can create a goroutine simply by using go keyword as a prefixing to the function or method call as shown in the below syntax:

func name(){

// statements
}

// using go keyword as the 
// prefix of your function call
go name()

Now we discuss how to create and work on multiple goroutines with the help of an example:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate Multiple Goroutines
package main
  
import (
    "fmt"
    "time"
)
  
// For goroutine 1
func Aname() {
  
    arr1 := [4]string{"Rohit", "Suman", "Aman", "Ria"}
  
    for t1 := 0; t1 <= 3; t1++ {
      
        time.Sleep(150 * time.Millisecond)
        fmt.Printf("%s\n", arr1[t1])
    }
}
  
// For goroutine 2
func Aid() {
  
    arr2 := [4]int{300, 301, 302, 303}
      
    for t2 := 0; t2 <= 3; t2++ {
      
        time.Sleep(500 * time.Millisecond)
        fmt.Printf("%d\n", arr2[t2])
    }
}
  
// Main function
func main() {
  
    fmt.Println("!...Main Go-routine Start...!")
  
    // calling Goroutine 1
    go Aname()
  
    // calling Goroutine 2
    go Aid()
  
    time.Sleep(3500 * time.Millisecond)
    fmt.Println("\n!...Main Go-routine End...!")
}

chevron_right


Output:

!...Main Go-routine Start...!
Rohit
Suman
Aman
300
Ria
301
302
303

!...Main Go-routine End...!

Creation: In the above example, we have two goroutines other than main goroutine, i.e, Aname, and Aid. Here, Aname prints the name of the authors and Aid prints the id of the authors.

Working: Here, we have two goroutines, i.e, Aname, and Aid and one main goroutine. When we run this program first the main goroutine strat and print “!...Main Go-routine Start...!“, here the main goroutine is like a parent and other goroutines are its children so first main goroutine run after that those other goroutines start and if the main goroutine terminates, then other goroutines are also terminated. So, after the main goroutine, Aname and Aid goroutines start their working concurrently. The Aname goroutine starts it working from 150ms and Aid start its working from 500ms as shown in the below image:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.