What is goroutine in Go programming language?

A goroutine is a function which usually runs concurrently with other functions. If you want to stop goroutine, you pass a signal channel to the goroutine, that signal channel pushes a value into when you want the goroutine to stop.

The goroutine polls that channel regularly as soon as it detects a signal, it quits.

  1. Quit : = make (chan bool)  
  2. go func ( ) {  
  3. for  {  
  4. select {  
  5. case < quit:  
  6. return  
  7. default  
  8. // do other stuff  
  9. }  
  10. }  
  11. }()  
  12. // Do stuff  
  13. // Quit goroutine  
  14. Quit < true