Skip to content

Instantly share code, notes, and snippets.

@aligoren
Created May 19, 2022 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aligoren/1f05354f5ee458e4bde2d459c4f9a1dd to your computer and use it in GitHub Desktop.
Save aligoren/1f05354f5ee458e4bde2d459c4f9a1dd to your computer and use it in GitHub Desktop.
fetch_sites_non_concurrent
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"io"
"net/http"
"time"
)
func fetchUrls2(url string) (string, error) {
httpResponse, err := http.Get(url)
if err != nil {
return "", err
}
if httpResponse.StatusCode != http.StatusOK {
return "", nil
}
defer httpResponse.Body.Close()
title := getTitle2(httpResponse.Body)
return title, nil
}
func getTitle2(htmlResponse io.ReadCloser) string {
doc, err := goquery.NewDocumentFromReader(htmlResponse)
if err != nil {
return "Title cannot be fetched"
}
title := doc.Find("title").Text()
return title
}
func main() {
urls := []string{"https://github.com", "https://www.enuygun.com", "https://aligoren.com", "https://coderwall.com",
"https://eksisozluk.com", "https://medium.com", "https://stackoverflow.com", "https://www.youtube.com"}
responses := make([]string, 0)
start := time.Now()
for _, url := range urls {
response, _ := fetchUrls2(url)
if len(response) > 0 {
responses = append(responses, response)
}
}
elapsedTime := time.Since(start)
fmt.Printf("%#v\n", responses)
fmt.Printf("Elapsed time: %s", elapsedTime.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment