package main import ( "encoding/json" "fmt" "log" "net/http" "time" ) type Comic struct { Num int `json:"num"` Link string `json:"link"` Img string `json:"img"` Title string `json:"title"` } const baseXkcdURL = "https://xkcd.com/%d/info.0.json" func GetComic(comicID int) (comic Comic, err error) { url := fmt.Sprintf(baseXkcdURL, comicID) response, err := http.Get(url) if err != nil { log.Println(err) } err = json.NewDecoder(response.Body).Decode(&comic) if err != nil { log.Println(err) } return comic, nil } func main() { start := time.Now() defer func() { fmt.Println("Execution Time: ", time.Since(start).Seconds()) }() comicsNeeded := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ch := make(chan Comic) for _, id := range comicsNeeded { go func(id int) { comic, err := GetComic(id) if err != nil { log.Println("error getting comic", err) } ch <- comic }(id) } var comics []Comic for i := 0; i < len(comicsNeeded); i++{ comm := <-ch comics = append(comics, comm) fmt.Println("Fetched comic", comicsNeeded[i], "of title:", comics[i].Title) } }
Here's an updated code with channels also making it faster and ran for 1 sec
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's an updated code with channels also making it faster and ran for 1 sec