Guest User

Untitled

a guest
Mar 10th, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7.  
  8. "github.com/remeh/sizedwaitgroup"
  9. )
  10.  
  11. func main() {
  12. fmt.Println("Input # of times to run")
  13. var number int
  14. fmt.Scan(&number)
  15. fmt.Println("Input # of threads")
  16. var threads int
  17. fmt.Scan(&threads)
  18. swg := sizedwaitgroup.New(threads)
  19.  
  20. for i := 0; i < number; i++ {
  21. swg.Add()
  22. go func(i int) {
  23. defer swg.Done()
  24. var data Autogenerated
  25. resp, err := http.Get("http://api.open-notify.org/iss-now.json")
  26. if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { // Parse []byte to go struct pointer
  27. fmt.Println("Can not unmarshal JSON")
  28. }
  29. if err != nil {
  30. fmt.Println(err)
  31. }
  32. resp.Body.Close()
  33. fmt.Println("\n Longitude: ", data.IssPosition.Longitude, " Latitude:", data.IssPosition.Latitude, "Request # ", i)
  34.  
  35. }(i)
  36. }
  37. swg.Wait()
  38.  
  39. }
  40.  
  41. type Autogenerated struct {
  42. Message string `json:"message"`
  43. IssPosition struct {
  44. Longitude string `json:"longitude"`
  45. Latitude string `json:"latitude"`
  46. } `json:"iss_position"`
  47. Timestamp int `json:"timestamp"`
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment