How to run all .go files within current directory through the command line (multi file package)
Solution 1
Finally we can now use:
go run .
thanks to: https://github.com/golang/go/issues/22726#issuecomment-345841019
Solution 2
As Nate Finch notes:
Go run is ... really only meant to be used on very small programs, which generally only need a single file.
Even on unix, go run *.go
is often not correct. In any project with unit tests (and every project should have unit tests), this will give the error:
go run: cannot run *_test.go files (something_test.go)
It will also ignore build restrictions, so _windows.go
files will be compiled (or attempted to be compiled) on Unix, which is not what you want.
There has been a bit of discussion of making go run
work like the rest of the go
commands, and there's an open CL for it (5164). It's currently under consideration for Go 1.4. In the meantime, the recommended solution on all platforms is:
go build && ./<executable>
Solution 3
Unix related systems
go run *.go
will be sufficient in most cases.
Continue to the below method if this causes errors.
Windows systems (and in other cases where go run *.go
doesn't work)
Token expansion doesn't work in the windows command line and hence the above will not work and display an error. go run *.go
may also not work in OSs in some cases due to current compiler limitations.
In these cases, use
go build && foo.exe
where foo.exe
is the name of the .exe file produced.
If perhaps you have no idea what the name of your executable is, first
go build
and check the name of the .exe file produced. Afterwards, use the method that includes the file name.
These 2 methods will build and run all the .go files within your current directory with minimum fuss.
Solution 4
You can run all .go
files, excluding tests, using this bash construction:
go run $(ls -1 *.go | grep -v _test.go)
Solution 5
The best way to do it is to run it like this:
go run !(*_test).go
It skips all your test files which is exactly what you need to avoid the error.
The other suggestion:
go build && ./<executable>
is a bit annoying. You have to delete the executable all the time to avoid being marked by git. You can put it in gitignore, of course, but I am lazy and this is an extra step.
dk123
Updated on April 27, 2021Comments
-
dk123 about 3 years
I'm a newcomer to Go. I extremely like the language, but I quickly realised that I needed to start dividing my files due to an increase in program size.
go run main.go
(with main.go having been the file with my main() function)didn't work and I hit a barrier for a while, because I had no clue how to get my program working.
Some quick searching lead me to the answer of
go run main.go other.go ..
where by typing all the files that my
package main
consists of, I could get the programming running. However, this is utterly cumbersome and frustrating to do each time.I write the following self-answered question in order to prevent others like myself who may again hit this barrier.
-
AntonB over 8 yearsThis is the right answer for developers on windows. In my case I had to use fully qualified path
import "github.com/abacaj/hello/lib"
and then use it likesc := new(lib.ServiceControl)
-
Yi Jiang over 8 yearshow to run *.go under the folder in subfolder?
-
MusikPolice over 6 yearsThis should be the accepted answer. It solves the problem for *nix developers
-
Johnny Wong over 6 years@user1872384 you should run
shopt -s extglob
first to enable the!(...)
wildcard syntax -
Macilias over 6 yearsThats definitely what I was looking for! Thx!
-
OWADVL about 6 yearsin case you use Windows PowerShell it's: go build; .\foo.exe (instead of && you have ; (point and comma))
-
Nato Boram about 6 yearsWill not work on Windows.
CreateFile *.go: The filename, directory name, or volume label syntax is incorrect.
-
cody almost 6 yearsI like this, but the command doesn't play nicely when it's set as an alias. r.sendecky's solution with
shopt
and the!(...)
works really well. -
Macilias over 5 yearsbut now, we can just use go run . :)
-
Zyl over 4 yearsBut how do I do this when my working directory is not and must not be
.
? -
Macilias over 4 years. is just a placeholder for current directory. You can replace is probably by any other path.
-
Christophe Vidal over 4 yearsWorking well, but just not in bash files for some reasons, I suppose there are some unsupported characters.
-
Polv almost 4 yearsIs there a cross platform way, that
filepath.Abs(filepath.Dir(os.Args[0]))
is not temp directory? -
Sajidur Rahman almost 3 yearsWill not work if there are any test files. Better use
go run .
-
Omar Omeiri almost 3 yearsYes it works on windows. Just use git bash