Last Updated: April 07, 2019
·
2.993K
· Tarcísio Xavier Gruppi

#!go run - run go as a script language

The magic

Add //usr/bin/env go run "$0" "$@"; exit to your go code and run it like shellscript, python, ruby...

Step by step

  • Create a file called hello-world.go with this code:
//usr/bin/env go run "$0" "$@"; exit

package main

import "log"

func main() {
  log.Println("Hello World")
}
  • Run chmod u+x hello-world.go
  • Run ./hello-world.go and watch the magic

1 Response
Add your response

That is a great tip Tarcísio. Excellent for who is learning, like me, and has no idea why the src folder structure is required. Thank you.

over 1 year ago ·