-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: cmd/go: allow "go run ." #22726
Comments
FWIW, I'm one of those people that discourages the use of
How does |
go run is already in many ways better than go build/go install for one-off/ad-hoc invocations.
Use case 5 is when there are no other Go files. If there are other Go files, that's use case 1. Updated script.go to main.go to clarify. Here's a concrete example that might help you understand my viewpoint: This program is split across two files, and there's also a test file. There's two choices to help people run this program (note that they may not be experienced Go developers, who fully understand how packages, go build, go install all work). There are a few choices to tell people how to run the program:
As you can see, I chose the third option. It'd be much nicer for this to simply be |
The problem here is when I started a thread on golang-nuts with what I think is similar motivation to this issue: https://groups.google.com/d/msg/golang-nuts/4C_Rd_JJZVU/UdqRePeMBwAJ @cznic raised some concerns about overloading the As you point out, with the build caching changes in Go 1.10, I would guess that |
Related:
I'm pretty sure there's old discussion about whether #! should be ignored in code that played into these too but I can't find the ones that related to go run. (I don't think we should ignore #! lines.) Potential design based on discussion with proposal review:
If you specify a package (even "."), then it's clear how to split the two possible flag lists.
means -flag goes to the go command or to the subprocess (the decision should not depend on exactly which flag it is). For consistency with go build etc we should probably make the default be go flags. That seems less surprising. To specify subprocess flags, fill in [single-package-or-*.go-list] with "." meaning the package in the current directory. Today:
Maybe tomorrow:
This would change @broady's 4 and 5 above to use "." instead of "--", but that's much easier to explain ("." is a valid argument to build, test, etc). |
Actually the default really must be that without a package list the flags are all for the go command. If they were all for the subprocess then there would be ambiguities due to not knowing which of the subprocess flags took arguments. For example if the subprocess took a -x=string flag, then
would look like not having a package, but you'd think an equivalent command would be:
which would incorrectly look like a go flag followed by a package name. Defining that the first flags are always go flags avoids this ambiguity. |
Based on discussion with proposal-review again this week, accepting proposal as detailed in #22726 (comment). I will take a look at this for Go 1.11. |
Back from vacation. I had done a little more thinking before you posted the comments above, and came to a similar (but more strict) conclusion: that "go run" should require a package name (or list of files) to remove all ambiguity. So:
I'm not certain that the gain in precision is worth requiring the argument, and think that the rules you described balance ease of use with precision well enough. |
Imho if we want to simplify this for the newbies then |
@dlsniper please read the most recent comments. The proposal for the |
I like the idea for allowing go run of a directory, broady's proposal for how to handle flags makes sense to me, except the -- is awkward. |
It's unclear to me that |
Hi @broady - do you hope to get this in for Go 1.11? I'm 💯 👍the current proposal.
Just to check one point: Go 1.10's caching changes do not appear to extend to A very nice side effect of doing so would be that we have fast, reproducible "builds" via |
In light of @rsc's recent points, to make the perhaps obvious point: having |
@broady 👍
Sounds good. I suspect @rsc will agree with your suggestion given comments in https://go-review.googlesource.com/c/vgo/+/105855 |
Change https://golang.org/cl/109341 mentions this issue: |
What is the problem to do some auto/run .go file by Go in 2xClick on .go file and set in windows registry the related icon to .go files? |
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 ofgo 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
Program with more than one file, with no tests
go run *.go
go run
Program with more than one file, where test files are also in the same directory.
go run !(*_test).go
orgo run main.go a.go b.go
go run
Program that accepts arguments
go run main.go arg1 arg2
go run -- arg1 arg2
--
separator when filenames are omitted.Program that accepts Go filenames as arguments
go run main.go other.go -- foo.go
go run -- foo.go
Program with build tags
go build -tags -o a && ./a && rm ./a
(go run ignores build tags on the files passed in)go run -tags foo
Also, perhaps a package path should be allowed as an argument, which would better mirror go build:
The text was updated successfully, but these errors were encountered: