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.
You can run all
.go
files, excluding tests, using this bash construction:just use this
it will work assuming u don't have any test files
Here is my solution:
I use it with an alias to make it easy to run command line apps
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, firstgo 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 withgo generate
a solution can be :The best way to do it is to run it like this:
It skips all your test files which is exactly what you need to avoid the error.
The other suggestion:
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.