Created
May 19, 2022 14:37
-
-
Save aligoren/1f05354f5ee458e4bde2d459c4f9a1dd to your computer and use it in GitHub Desktop.
fetch_sites_non_concurrent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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