Like all high-brow tools and languages, go
is not easily amenable to be used in ways its authors did not like or foresee.
In this case, go
does not want to have anything to do with with paths not ending in *.go
.
A solution may be to use a temporary file named *.go
:
gorun(){
t=$(mktemp -d) && cat > "$t/in.go" && go run "$t/in.go"
e=$?; rm -fr "$t"; return "$e"
}
gorun <<'EOT'
...
EOT
I cannot use just mktemp /tmp/XXXXXXX.go
above; that will silently create a file named exactly /tmp/XXXXXXX.go
on BSD, where mktemp
only supports trailing Xs
. Also, I cannot assume that /tmp
exists, is writable and is the place where temporary files are created by default.
This point is already moot, but go
also has problems with pipes and other non-regular files, so a process substitution wouldn't have worked anyway:
% ln -s /dev/fd/3 fd3.go
% go run fd3.go 3<<<'package main; func main(){}'
// OK
% go run fd3.go 3< <(echo 'package main; func main(){}')
# command-line-arguments
./fd3.go:1:1: syntax error: package statement must be first
% cat fd3.go 3< <(echo 'package main; func main(){}')
package main; func main(){}
// WTF?
% mkfifo fifo.go
% go run fifo.go &
[1] 8849
% echo 'package main; import "log"; func main(){log.Println("yuck!")}' > fifo.go
// if at first you don't succeed ...
% echo 'package main; import "log"; func main(){log.Println("yuck!")}' > fifo.go
% 2020/02/06 11:21:53 yuck!
[1]+ Done go run fifo.go