86

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
dk123
  • 18,684
  • 20
  • 70
  • 77

12 Answers12

108

Finally we can now use:

go run .

thanks to: https://github.com/golang/go/issues/22726#issuecomment-345841019

Macilias
  • 3,279
  • 2
  • 31
  • 43
  • But how do I do this when my working directory is not and must not be `.`? – Zyl Oct 31 '19 at 14:36
  • 1
    . is just a placeholder for current directory. You can replace is probably by any other path. – Macilias Nov 01 '19 at 08:46
  • This happens when the "package main" exist in more than 1 file, then you can't just simply run e.g. "go run main.go" or "go run app.go", you will need to run "go run ." === "go run file1.go file2.go file3.go..." === "go run *.go" Thanks for this answer. Great enlightening moment! – shellyyg Aug 25 '22 at 13:17
76

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>
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
26

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.

Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
dk123
  • 18,684
  • 20
  • 70
  • 77
26

You can run all .go files, excluding tests, using this bash construction:

go run $(ls -1 *.go | grep -v _test.go)
Ruben S
  • 931
  • 1
  • 10
  • 11
  • 5
    This should be the accepted answer. It solves the problem for *nix developers – MusikPolice Oct 13 '17 at 14:28
  • 1
    I 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. – cody Aug 30 '18 at 14:04
19

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.

r.sendecky
  • 9,933
  • 9
  • 34
  • 62
8

If i understand your question right, you need import other code as libraries.

Little example

./testing/main.go:

package main

import "fmt"
import L "testing/lib"

func main() {
    fmt.Println("Hello from main()")
    L.Somefunc()
}

./testing/lib/somelib.go:

package lib

import "fmt"

func Somefunc() {
    fmt.Println("Hello from Somefunc()")
    return
}

To launch - go run main.go

Noisee
  • 306
  • 2
  • 7
  • 1
    This 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 like `sc := new(lib.ServiceControl)` – AntonB Oct 23 '15 at 19:20
4

just use this

go run *.go 

it will work assuming u don't have any test files

3

If you are run in terminal of Vscode on Windows AND without knowledge about workspace, just do like this:

go run $(ls *.go)
Point
  • 41
  • 4
2

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
Clément Prévost
  • 8,000
  • 2
  • 36
  • 51
1

On Windows I usually just add a little test.bat to all my project directories:

go build
.\{project_name}.exe
go clean

Works well enough. Replace {project_name} with the name of your executable, of course. And once the executable finishes, the batch script moves on to the clean up.

0

For peoples attempting to use go run combined with go generate a solution can be :

//go:generate sh -c "go run path/*.go"
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
-1

For window the following works: Open cmd and go to the path where your folder exists. Then type the following command and press Enter.

go build

after this one executable will be created. Then in the command prompt call the executable. If your executable name is Project.exe then type the following and press Enter:

Project.exe
M. Paul
  • 361
  • 5
  • 18