News for hackers. Stuff that matters.
Re:Goodbye Python, Hello Go by IshKebab on Friday August 03, 2018 @07:47AM (#17679026)
Writing a program like that would not be too difficult in Go. It's something like this:

    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.

Re:Goodbye Python, Hello Go by boris on Friday August 03, 2018 @08:07AM (#17679144)
Just add this as a second line?

  trap "exit 1" ERR
Re:Goodbye Python, Hello Go by majewsky on Sunday August 05, 2018 @03:57PM (#17693178)
I think you mean:

  set -euo pipefail
Re:Goodbye Python, Hello Go by arendtio on Friday August 03, 2018 @09:30AM (#17679640)
Thanks for giving an equivalent example in Go :-)

I think for this small size of an example program the error checking in Go is pure overhead, but as soon as the program starts to have a few more functions it will be very useful to have more options than just to abort on fail (Bash trap style / set -e). So I don't have any problem with 'polluting' my source with `if err != nil` checks ;-)

What I am more concerned about are the different ways I have to call functions from different libraries like

  resp, err := http.Get(url)
  ...
  defer resp.Body.Close()
  body, err := ioutil.ReadAll(resp.Body)
That is a lot more 'special' than the other function calls and the bash version and probably requires more documentation reading than '$ curl --help'. But hey, thanks to your example I have a starting point and probably are going to experiment with it in the new future.

For all the others who wrote that Bash is the right tool for the job: I read your warnings, opinions and suggestions and will keep them in mind while exploring the possibilities of using Go for script like jobs.

Re:Goodbye Python, Hello Go by h1d on Friday August 03, 2018 @08:34AM (#17679297)
That is so repetitive to handle errors. Doesn't go have a cleaner way of handling errors than making the code look far less readable without unified error handling like exceptions?
Re:Goodbye Python, Hello Go by IshKebab on Saturday August 04, 2018 @05:07PM (#17688434)
It is a little repetitive. The best solution is Rust's Result system. But I'd say Ho's error handling system is second to that.

You might be thinking "but... exceptions!". They are a pretty bad solution. The problems are:

* Error handling is hidden - it's impossible in most implementations to know which exceptions can be thrown by a function * Flow control context is lost. If you wrap lots of lines with try {} you don't know which one caused the error and can't add context information. You end up with shit error messages like "file not found" rather than "error opening config file foo.cfg: file not found". The solution is to wrap every line in a separate try/catch but that is just Go-style error handling but shitter and even more verbose.

Go is a good balance. If you want proper error handling you have to actually write it. Yes it is a little tedious but that's what it takes to do it properly.

If you don't want to do it properly then fine, use Bash and set -e. But accept that it will be shit.