I made one of these too! I decided not to use // because I use gofmt auto formatting in my editor and it puts a space between the // and the usr. This one isn't changed by gofmt:
/*usr/bin/env go run "$0" "$@"; exit;*/
- go fmt leaves it alone
- it preserves the exit code in case env or go breaks
- it "figures out" where go is, no need to hard code a path that breaks on yet another machine (where /usr/bin/env is "standard")
(
The first example:
- I'm not a big fan of the subtle extra work that the question mark in the original path imposes on the system. env is (almost) always going to be as /usr/bin/env and rarely would something else that matches exist. ls -d /?sr to see what outputs on your system. That being said, the extra work isn't very meaningful in this case...
- The trailing $? seems unnecessary as the final exit will convey the return code from env go regardless of if the $? is present or not
)
I wonder if bash has a way to override the default function calling mechanism. Since functions in bashrc would probably take priority, maybe something like this could be possible: (psuedo code)
```
execve(path, args) {
if path.endsWith('.go') {
go run path args
} else {
super($path, *args)
}
}
```
Then when you run `./script.go`, the function gets called and passes it so `go run`, while everything else goes through the existing execve function. That would be interesting, because then you could do this with absolutely anything.
It works, but the best in me I cannot explain fully first 3 symbols.
/*?sr/bin/env finds /usr by expanding *? to a first matching directory. But why not just /*usr/ instead?