github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/concurrency/support/google.go (about)

     1  
     2  // +build OMIT
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"math/rand"
     9  	"time"
    10  )
    11  
    12  type Result string
    13  
    14  // START1 OMIT
    15  func Google(query string) (results []Result) {
    16  	results = append(results, Web(query))
    17  	results = append(results, Image(query))
    18  	results = append(results, Video(query))
    19  	return
    20  }
    21  // STOP1 OMIT
    22  
    23  // START2 OMIT
    24  var (
    25  	Web = fakeSearch("web")
    26  	Image = fakeSearch("image")
    27  	Video = fakeSearch("video")
    28  )
    29  
    30  type Search func(query string) Result // HL
    31  
    32  func fakeSearch(kind string) Search {
    33          return func(query string) Result {
    34  	          time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
    35  	          return Result(fmt.Sprintf("%s result for %q\n", kind, query))
    36          }
    37  }
    38  // STOP2 OMIT
    39  
    40  func main() {
    41  	rand.Seed(time.Now().UnixNano())
    42  	start := time.Now()
    43  	results := Google("golang") // HL
    44  	elapsed := time.Since(start)
    45  	fmt.Println(results)
    46  	fmt.Println(elapsed)
    47  }