github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume1/section4/goroutinesdemowithchannel/goroutinesdemowithchannel.go (about)

     1  // An example of using a channel to wait for a goroutine to complete.
     2  package main
     3  
     4  import (
     5  	"fmt"
     6  )
     7  
     8  var done chan bool = make(chan bool)
     9  
    10  func printGreetings(source string) {
    11  
    12  	for i := 0; i < 9; i++ {
    13  		fmt.Println("Hello Gopher!", i, source)
    14  	}
    15  
    16  	if source == "goroutine" {
    17  		done <- true
    18  	}
    19  
    20  }
    21  
    22  func main() {
    23  
    24  	go printGreetings("goroutine")
    25  	printGreetings("main function")
    26  
    27  	<-done
    28  }