Last active
March 11, 2022 18:15
-
-
Save charlires/3ed9212352e6c850edee10360b6444c7 to your computer and use it in GitHub Desktop.
Go Async
This file contains hidden or 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/", | |
} | |
for _, url := range urls { | |
checkUrl(url) | |
} | |
} | |
//checks and prints a message if a website is up or down | |
func checkUrl(url string) { | |
_, err := http.Get(url) | |
if err != nil { | |
fmt.Println(url, "is down !!!") | |
return | |
} | |
fmt.Println(url, "is up and running.") | |
} |
This file contains hidden or 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/", | |
} | |
for _, url := range urls { | |
go checkUrl(url) | |
} | |
} | |
//checks and prints a message if a website is up or down | |
func checkUrl(url string) { | |
_, err := http.Get(url) | |
if err != nil { | |
fmt.Println(url, "is down !!!") | |
return | |
} | |
fmt.Println(url, "is up and running.") | |
} |
This file contains hidden or 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" | |
"time" | |
) | |
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/", | |
} | |
for _, url := range urls { | |
// Call the function check | |
go checkUrl(url) | |
} | |
time.Sleep(5 * time.Second) | |
} | |
//checks and prints a message if a website is up or down | |
func checkUrl(url string) { | |
_, err := http.Get(url) | |
if err != nil { | |
fmt.Println(url, "is down !!!") | |
return | |
} | |
fmt.Println(url, "is up and running.") | |
} |
This file contains hidden or 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" | |
"sync" | |
) | |
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/", | |
} | |
var wg sync.WaitGroup | |
for _, u := range urls { | |
// Increment the wait group counter | |
wg.Add(1) | |
go func(url string) { | |
// Decrement the counter when the go routine completes | |
defer wg.Done() | |
// Call the function check | |
checkUrl(url) | |
}(u) | |
} | |
// Wait for all the checkWebsite calls to finish | |
wg.Wait() | |
} | |
//checks and prints a message if a website is up or down | |
func checkUrl(url string) { | |
_, err := http.Get(url) | |
if err != nil { | |
fmt.Println(url, "is down !!!") | |
return | |
} | |
fmt.Println(url, "is up and running.") | |
} |
This file contains hidden or 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