Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Go has explicitly rejected adding shebang support, mandating this hack, due to being considered "abuse of resources"[0]. Rather `gorun`, which is also called a mistake by Pike, is recommended instead. And can alter this method so not to need to hardcode a path.

    /// 2>/dev/null ; gorun "$0" "$@" ; exit $?
>Good-old posix magic. If you ask an LLM, it'll say it's due to Shebangs.

Well, ChatGPT gives same explanation as article, unsurprising considering this mechanic has been repeated many times.

>none other fits as well as Go

Nim, Zig, D, all have `-run` argument and can be used in similar way. Swift, OCaml, Haskell can directly execute a file, no need to provide an argument.

[0]: https://groups.google.com/d/msg/golang-nuts/iGHWoUQFHjg/_dbL...





There is also yaegi, a Go interpreter, which might be a better choice for small scripts than 'go run'.

https://github.com/traefik/yaegi


Also, for Go oneliners: https://github.com/dolmen-go/goeval

(Disclaimer: author here)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: