手軽に Go スクリプトを実行する

Linux 限定ですが、binfmt_misc を使えば shell script や Python と同じように実行ビットを立てるだけで実行できるようになります。

  1. 以下のスクリプト/usr/local/bin/gorun とでもして保存

     #!/bin/sh
     exec go run "$@"

  2. Debian/Ubuntu なら update-binfmts があるので以下を実行

    sudo update-binfmts --install go /usr/local/bin/gorun --extension go

  3. Go スクリプトに実行ビットを立てる

    chmod a+x hello.go

  4. hello.go が直接実行できるようになる

     $ ./hello.go
     Hello World!

小ネタで頻出と思いますけど。