Created
October 27, 2020 04:46
-
-
Save DarkGhostHunter/85fa8be57f06fd5940c340add8d17816 to your computer and use it in GitHub Desktop.
Pinging sites using goroutines
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" | |
"os/exec" | |
"strings" | |
"sync" | |
) | |
func isSiteDown(site string, group *sync.WaitGroup) { | |
defer group.Done() | |
cmd := exec.Command("ping", "-n", "1", "-a", site) | |
_, err := cmd.CombinedOutput() | |
if err == nil { | |
fmt.Println(strings.Replace("Site alive: %", "%", site, 1)) | |
} else { | |
fmt.Println(strings.Replace("SITE DOWN! %", "%", site, 1)) | |
} | |
} | |
func main() { | |
var sites = [5]string{ | |
"www.google.com", | |
"www.thisdoesnotexists.com", | |
"www.duckduckgo.com", | |
"www.medium.com", | |
"www.thisalsodoesntexists.com", | |
} | |
var waitGroup sync.WaitGroup | |
for _, site := range sites { | |
waitGroup.Add(1) | |
go isSiteDown(site, &waitGroup) | |
} | |
waitGroup.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment