Last active
March 3, 2022 20:44
-
-
Save gsingharoy/be453b54e7d61522d3b8610ec2b30a04 to your computer and use it in GitHub Desktop.
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" | |
"net/http" | |
) | |
func main() { | |
// A slice of sample websites | |
urls := []string{ | |
"https://www.easyjet.com/", | |
"https://www.skyscanner.de/", | |
"https://www.ryanair.com", | |
"https://wizzair.com/", | |
"https://www.swiss.com/", | |
} | |
c := make(chan urlStatus) | |
for _, url := range urls { | |
go checkUrl(url, c) | |
} | |
result := make([]urlStatus, len(urls)) | |
for i, _ := range result { | |
result[i] = <-c | |
if result[i].status { | |
fmt.Println(result[i].url, "is up.") | |
} else { | |
fmt.Println(result[i].url, "is down !!") | |
} | |
} | |
} | |
//checks and prints a message if a website is up or down | |
func checkUrl(url string, c chan urlStatus) { | |
_, err := http.Get(url) | |
if err != nil { | |
// The website is down | |
c <- urlStatus{url, false} | |
} else { | |
// The website is up | |
c <- urlStatus{url, true} | |
} | |
} | |
type urlStatus struct { | |
url string | |
status bool | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment