Advertisement
Guest User

Untitled

a guest
May 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.05 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "net/http"
  6.     "sync"
  7. )
  8.  
  9. var (
  10.     URLOK   string = "https://google.com"
  11.     URLSLOW string = "http://slowwly.robertomurray.co.uk/delay/10000/url/" + URLOK
  12. )
  13.  
  14. func goSlow(slowURL string, wg *sync.WaitGroup) {
  15.     fmt.Println("in go slow")
  16.     r, _ := http.Get(slowURL)
  17.     defer r.Body.Close()
  18.     defer fmt.Println("GO SLOW", "DONE")
  19.     defer wg.Done()
  20.  
  21. }
  22.  
  23. func goFast(URL string, wg *sync.WaitGroup) {
  24.     fmt.Println("in go fast")
  25.     r, _ := http.Get(URL)
  26.     defer r.Body.Close()
  27.     defer fmt.Println("GO FAST", "DONE")
  28.     defer wg.Done()
  29. }
  30.  
  31. func justGo(wg *sync.WaitGroup) {
  32.     fmt.Println("in just Go")
  33.     r, _ := http.Get("https://bing.com")
  34.     defer r.Body.Close()
  35.     defer fmt.Println("JUST GO", "DONE")
  36.     defer wg.Done()
  37. }
  38. func main() {
  39.     var wg sync.WaitGroup
  40.     wg.Add(3)
  41.  
  42.     go goSlow(URLSLOW, &wg)
  43.     go justGo(&wg)
  44.     go goFast(URLOK, &wg)
  45.  
  46.     wg.Wait()
  47. }
  48.  
  49.  
  50. //Outputs:
  51.  
  52. in go fast
  53. in go slow
  54. in just Go
  55. GO FAST DONE
  56. JUST GO DONE
  57. GO SLOW DONE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement