Make-shift shebangs for Go
27 points by runxiyu
27 points by runxiyu
This is cursed. I love it.
I like how exit also ends up passing through the proper return codes if go is not installed, the program exits with an error, etc. From bash(1):
exit [n] Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed.
I've said this on a different but related topic.
I wish go would standardize a comment that implements a suppression for warnings across all programs like fix, vet, gopls, etc. Ideally it would also work for every external linting/tree walking analysis program (errcheck, staticcheck, nilaway, etc.)
The fact that many external tools support something like this indicates the demand signal. The fact that there's no standard and the golang official tools lack such capabilities makes it incredibly frustrating to navigate.
I think your gopls problem is my new favorite addition the list of reasons something like an ignore comment should be standardized.
Shouldn't it be "makeshift"? I'm not an English native speaker but I have the impression that that makes a big difference in the meaning.
(Another small issue is that the location of Go installations could vary a lot between systems, for example, it's prefixed in /usr rather than /usr/local on my system.)
Couldn't this also be done with binfmt-misc (kernel module). It would need some configuration and a wrapper-script though.
echo ':goscript:E::go::/usr/local/bin/go-script:' | sudo tee /proc/sys/fs/binfmt_misc/register
And this wrapper:
#!/bin/sh
set -e
src="$1"
shift
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
go build -o "$tmp/a.out" "$src"
exec "$tmp/a.out" "$@"
This is a cool hack.
https://gist.github.com/posener/73ffd326d88483df6b1cb66e8ed1e0bd (linked from the blog post linked in a sibling comment by @jamesog) Seems to be either the unreferenced source , or this is a happy coincidence of parallel discovery.
I haven't tested this in a file, just in a shell, but here are my observations:
First of all, seems like /*usr/ can break if there's something else in root that ends with usr. Not likely but possible.
Seems like using /*/usr/ works just as well and doesn't suffer from this problem. Maybe the author should try that instead.
Second of all, using globs seems to fail on systems where the root is not listable (e.g. my Android).