Dunaevlad
@Dunaevlad

Как исправить ошибку panic: runtime error: invalid memory address or nil pointer dereference?

func MakeRequest(url string, ch chan<- string) {
	start := time.Now()
	resp, err := http.Get(url)
	if err != nil {
		ch <- fmt.Sprintf(err.Error())
	}
	secs := time.Since(start).Seconds()
	body := resp.StatusCode
	defer resp.Body.Close()
	ch <- fmt.Sprintf("%.2f elapsed with response status code: %d %s", secs, body, url)
}
func main() {
	start := time.Now()
	ch := make(chan string)
	urls := []string{
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
		"https://www.vox.com/",
		"https://www.nbcnews.com/",
	}
	for _, url := range urls {
		go MakeRequest(url, ch)
	}
	for range urls {
		fmt.Println(<-ch)
	}

	fmt.Printf("Was made %v requests\n", len(urls))
	fmt.Printf("%.2fs elapsed\n", time.Since(start).Seconds())
}
  • Вопрос задан
  • 106 просмотров
Решения вопроса 1
@deliro
Ты сначала смотришь ошибку, а потом забиваешь на неё, как будто ошибки не было и парсишь resp
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы