github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2013/go-sreops/goroutines-channels.go (about)

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"time"
     8  )
     9  
    10  // START OMIT
    11  func main() {
    12  	textChannel := make(chan string)
    13  	words := []string{"ho!", "hey!"}
    14  	secs := []int{2, 1}
    15  	// Create a goroutine per word
    16  	for i, word := range words {
    17  		go say(word, secs[i], textChannel) // &
    18  	}
    19  	// Wait for response via channel N times
    20  	for _ = range words {
    21  		fmt.Println(<-textChannel)
    22  	}
    23  }
    24  
    25  // say sends word back via channel after sleeping for X secs
    26  func say(word string, secs int, textChannel chan string) {
    27  	time.Sleep(time.Duration(secs) * time.Second)
    28  	textChannel <- word
    29  }
    30  
    31  // STOP OMIT