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 r
just use this
go run *.go
it will work assuming u don't have any test files
Here is my solution:
go run $(find . -name "*.go" -and -not -name "*_test.go" -maxdepth 1)
I use it with an alias to make it easy to run command line apps
alias gorun='go run $(find . -name "*.go" -and -not -name "*_test.go" -maxdepth 1)'
$ gorun param1 param2
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>
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.
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.
For peoples attempting to use go run
combined with go generate
a solution can be :
//go:generate sh -c "go run path/*.go"