erning / gorun

gorun is a tool enabling one to put a "bang line" in the source code of a Go program to run it, or to run such a source code file explicitly. It was created in an attempt to make experimenting with Go more appealing to people used to Python and similar languages which operate most visibly with source code.
https://wiki.ubuntu.com/gorun
GNU General Public License v3.0
972 stars 70 forks source link

gorun fmt #8

Open ericcurtin opened 6 years ago

ericcurtin commented 6 years ago

A C/C++ programmer primarily, looking to learn something new, golang! One of the things I first thought is what great potential it had as a statically typed scripting language! And I really like how you cache compiled binaries here, pretty cool.

Anyway long story short... I'd like to introduce a "gorun fmt" option to format my gorun scripts... Because if you pass through normal go fmt it doesn't like the shebang of course:

go fmt hello_world.go 
can't load package: package main: 
hello_world.go:1:1: illegal character U+0023 '#

If I open a PR would you except a "gorun fmt" option? Don't want to waste effort if it wouldn't be accepted!

dougnukem commented 5 years ago

@ericcurtin one option is to run it as comments, then you can run go fmt on it fine:

//usr/bin/env gorun "$0" "$@"; exit "$?"

package main

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

For various other solutions and tradeoffs see: https://gist.github.com/posener/73ffd326d88483df6b1cb66e8ed1e0bd

jackielii commented 1 year ago

Somehow this also works and gofmt won't change the code. It adds a space after the comment

// / 2>/dev/null ; gorun "$0" "$@" ; exit $?
powerman commented 2 weeks ago

Somehow this also works and gofmt won't change the code. It adds a space after the comment

Just add an empty line before this gorun line and a package main line. This way this comment won't be considered a "package comment" and gofmt won't change it.