Guest User

Untitled

a guest
Mar 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. )
  9.  
  10. func main() {
  11. nums := []int{2, 3, 4, 1, 5}
  12. channel := make(chan string, len(nums))
  13. for i, num := range nums {
  14. go getUser(strconv.Itoa(num), len(nums), i, channel)
  15. }
  16. // Location 1
  17. // close(channel)
  18. for data := range channel {
  19. fmt.Println(data)
  20. }
  21.  
  22. }
  23.  
  24. func getUser(userID string, length int, current int, channel chan string) {
  25. resp, err := http.Get("https://jsonplaceholder.typicode.com/users/" + userID)
  26. if err != nil {
  27. // handle error
  28. }
  29. defer resp.Body.Close()
  30. body, err := ioutil.ReadAll(resp.Body)
  31. data := string(body)
  32. channel <- data
  33. // Location 2
  34. // if current == length-1 {
  35. // close(channel)
  36. // }
  37. }
Add Comment
Please, Sign In to add comment