Description
Currently, go run
requires Go files as arguments. It's useful for quickly running Go programs, but has been discouraged by many people because dependent packages are not installed. With the changes to build caching in Go 1.10, I propose we simplify usage of go run
to encourage more use of it.
A rundown of use cases and how this change would improve the user experience:
-
Program with one file (e.g., one-off scripts, go generate scripts)
go run script.go
- After: unchanged
- Improvement: a little more flexibility if it's the only .go file in the directory.
-
Program with more than one file, with no tests
- Before:
go run *.go
- After:
go run
- Improvement: minimal
- Before:
-
Program with more than one file, where test files are also in the same directory.
- Before:
go run !(*_test).go
orgo run main.go a.go b.go
- After:
go run
- Improvement: huge. I had to look up this glob syntax.
- Before:
-
Program that accepts arguments
- Before:
go run main.go arg1 arg2
- After:
go run -- arg1 arg2
- Improvement: same as 1, 2, 3
- NOTE: to prevent collisions of flags between cmd/go and the user's program, we should probably require the
--
separator when filenames are omitted.
- Before:
-
Program that accepts Go filenames as arguments
- Before:
go run main.go other.go -- foo.go
- After:
go run -- foo.go
- Improvement: same as 1, 2, 3
- Before:
-
Program with build tags
- Before:
go build -tags -o a && ./a && rm ./a
(go run ignores build tags on the files passed in) - After:
go run -tags foo
- Improvement: makes what is basically impossible possible. But this isn't a common use case.
- Before:
Also, perhaps a package path should be allowed as an argument, which would better mirror go build:
go run golang.org/x/tools/cmd/godoc -http=:6060