Skip to content

Instantly share code, notes, and snippets.

@DarkGhostHunter
Created October 27, 2020 04:46
Show Gist options
  • Save DarkGhostHunter/85fa8be57f06fd5940c340add8d17816 to your computer and use it in GitHub Desktop.
Save DarkGhostHunter/85fa8be57f06fd5940c340add8d17816 to your computer and use it in GitHub Desktop.
Pinging sites using goroutines
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