Go as a scripting language

Go as a scripting language

Since the last couple of years I've been growing so used to Go that I use it more and more for almost everything. It's such a simple language (with batteries included) to use that I often find myself using it even for the most simple of hacks. I am still a Pythonista by heart, and it's difficult to beat Python's ability to compile near pseudo code. But surprisingly the lines of code actually written for the same task is often quite equal (and some times surprisingly even smaller with Go).

Anyway, the point is that I stumbled upon this great tool that allows using Go as a scripting language and adding your favorite #! line on top of your .go script file. Just like you usually do with Bash, Python, Perl, etc.

The magic is done by gorun.

It allows to write the following script:

#!/usr/bin/env gorun

package main

func main() {
    println("Hello world!")
}

And run it with usual chmod +x hello.go and ./hello.go. Without gorun it would never pass the compiler. A great hack and specially for the cases you just want to run your source code and maintain the readability of it. Gorun even maintains the compiled version, so unless you made any changes to the code it will just keep running the same compiled binary in the background.