Channels
package main import ( "fmt" "time" ) /* * This function prints messages in a loop with a short delay between, then * sends a message on the channel to indicate completion. */ func printer(count int, c chan string) { fmt.Println("Printer start...") for i := 1; i <= count; i++ { fmt.Println("Printer", i) time.Sleep(300*time.Millisecond) } fmt.Println("Printer end...") c <- "All finished!" } /* * The main mostly starts the printer function, then waits for the message * from the printer. */ func main() { var c chan string = make(chan string) fmt.Println("Main start...") go printer(8, c) msg := <- c fmt.Println("Printer says", msg) fmt.Println("Main end...") }