back

by gurjeet·6y ago·view on hn ↗
> difficulty of creating a language that is simultaneously a good programming language and a good scripting language

I bridged that gap recently with GoLang. Below [1] is an example of how I now use GoLang instead of Bash for my scripting needs. The development cycle is essentially just 2 steps, like scripting, 1) Edit file(s) 2) Run the script (./my.go).

This gives the same feeling of writing scripts, but with all the guarantees of GoLang and benefits of an IDE (VSCode). The trick is to have the first line of the file be a script (for the process loader to execute) as well as a comment (so that GoLang infrastructure ignores that line.

[1]: https://gist.github.com/gurjeet/3121b2e373e61873c5a6e96d72b6... (Gist version at the time of submission: v3)

1 comments
That is one interesting shebang. Nicely done. Can you explain the error handling stuff?
There are functions in log.go that emit message to stdout (thinking about it, I need to send warnings and above to stderr).

The function err(), in addition to sending a message to stdout, also causes an exit of the process. But it does so only if ExitOnError is turned on; which is on by default, and can be turned off by setting environment variable EXIT_ON_ERROR=true.

I have converted that Gist into a standalone Git repo, and hope to update it to add more improvements (like the stderr change, above) to showcase how to leverage GoLang for scripting.

[1]: https://git.sr.ht/~gurjeet/GoLang_Scripting_Example