func main() {
url := "https://example.com"
mem := "memory.txt"
oldsum, err := ioutil.ReadFile(mem)
if err != nil {
log.Fatal(err)
}
// Download URL
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// Hash it
newsum := sha512.Sum512(body)
if oldsum != newsum {
err = exec.Command("xmpp_notify", url + " changed").Run()
if err != nil {
log.Warn(err)
}
err = ioutil.Write(mem, newsum)
if err != nil {
log.Error(err)
}
}
}
Don't listen to the naysayers. Go is fantastic for scripts like this - it's really good at network stuff, has a ton of built in functions, etc. And you get actual robustness and error checking that isn't a complete joke. Look how many ways your Bash script could fail! That's fine if you're literally watching the output with your eyes. If not it's going to cause issues when `curl` fails for example.The only downside really is that you have to compile the Go code, which is fast but not as fast as just interpreting a Bash script (the first time).
Another possible issue is executable size - if you have a lot of Go scripts they may take up a fair bit of space.
I wish Go had an interpreter / JIT mode for scripts.
Edit: Before someone says "but that's 3 times longer than the bash code!" please write the equivalent Bash code that has proper error checking.