225

I have a single file in the main package called main.go. Because the code isn't reusable I want to separate part of the code in a different file but in the same package.

How do I split the contents of main.go into multiple files without creating a separate package?

I want a directory structure like this:

ls foo

# output:
main.go
bar.go

File bar.go

// file bar.go
package main

import "fmt"

func Bar() {
  fmt.Println("Bar")
}

File main.go

// file main.go
package main

func main() {
  Bar()
}

In main.go i want to use func Bar() from bar.go. But when I run go run main.go it results in:

# command-line-arguments
./main.go:4:2: undefined: Bar

What can be done do run the code without an error?

9 Answers 9

301

Update 26th July 2019 (for go >=1.11)

go run .

Will work on windows as well.

Original answer (for non windows environments)

The code actually works. The problem was that instead of running go run main.go I should run:

go run *.go
6
  • 13
    Unless you have *_test.go in your folder. Then you need to (1) shopt -s ext glob and (2) go run !(*_test).go. Commented Aug 13, 2015 at 7:40
  • and what about those using appengine?
    – dwp4ge
    Commented Nov 5, 2015 at 4:00
  • 3
    I tried go run ./cmd/myCmd/... and it worked for me. Maybe this is the correct way when there are _test.go files?
    – VinGarcia
    Commented Mar 27, 2019 at 17:14
  • go run ../your_folder from inside the working folder works for me
    – Andy
    Commented Jun 4, 2019 at 7:40
  • 3
    Just do go run . Commented Jul 20, 2019 at 4:19
76

Update August 2018, with Go 1.11, a section "Run" states:

The go run command now allows a single import path, a directory name or a pattern matching a single package.
This allows go run pkg or go run dir, most importantly go run .


Original answer Jan. 2015

As mentioned in "How to compile Go program consisting of multiple files?", go run expects a list of files, since it "compiles and runs the main package comprising the named Go source files".
So you certainly can split your main package in several files with go run.

That differs from go build/go install which expect package names (and not go filenames).
A simple go build would produce an executable named after the parent folder.

Note that, as illustrated by this thread, a go run *.go wouldn't work in a Windows CMD session, since the shell doesn't do wildcard expansion.

3
  • 1
    Is there a workaround for running multiple go files with run using the Windows command prompt?
    – Luke
    Commented Jul 18, 2016 at 4:54
  • 1
    Thanks! This helped me a ton. I kept wondering why I couldn't just run the code with just main.go since it was the same package but it kind of makes sense to me as to why they didn't allow for that.
    – Chris
    Commented Oct 10, 2018 at 7:34
  • @Luke just specify both files separated by a space.
    – xuiqzy
    Commented Sep 13, 2020 at 16:39
12

In my opinion, the best answer to this question is hidden in the comments to the top answer.

Just run this:

go run .

This will run all the files in main package, but will not give an error message like

go run: cannot run *_test.go files (main_test.go)

Kudos to @BarthesSimpson

5

As mentioned, you can say go run *.go but for Windows you can just list the script files (since *.go won't work) - go run main.go other.go third.go

1
  • Yes thats correct. In windows i had to run go run .
    – KTM
    Commented Jul 28, 2020 at 7:26
3

The first method to do so will be to run

go run *.go

The another method is to generate an exe file

go build

Then run that .exe file

./filename.exe
2

With current-day Golang, go run . isn't sufficient by itself.

First, you need to initialize the folder with a one-time setup:

go mod init noname

Now you can run your program with:

go run .

Although it doesn't explain the significance of the name chosen for use with go mod init, this method is demonstrated here: Tutorial: Get started with Go. A key point is that "Enable dependency tracking for your code" is optional for the most trivial cases like a typical go run ., but is essentially required for any non-trivial applications or workflows (for example using non-standard imports).

The use of noname in the above example can be replaced by anything that looks like a relative path-name. It basically doesn't matter what you use as the module name if you're building a main module that you don't plan to import elsewhere.

A basic explanation of go mod init was previously asked for here: Can someone please dumb down go mod init for me?


Note: Without go mod init (somename), go run . returns an error like so...

$ go run .
go: go.mod file not found in current directory or any parent directory; see 'go help modules'

There may be alternative solutions: Error message "go: go.mod file not found in current directory or any parent directory; see 'go help modules'"

4
  • Note, though, that go build . uses the module name as the binary name -- so in that case, it matters what you choose. Commented Mar 19, 2023 at 23:50
  • As noted in the output of go mod init (name), you typically follow this with go mod tidy if your module has external dependencies. Commented Jun 11, 2023 at 20:51
  • There seam to be reasons that you want to match the name of your folder with the name of the module. Commented Jun 11, 2023 at 20:51
  • *"install or import elsewhere". Commented Jan 27 at 16:36
1

For Windows install Cygwin and use it instead of command prompt. "go run *.go" will work then.

2
  • I would suggest MinGW for that, but the approach is right if you ask me. I doubt that anyone could be really emotionally attached to cmd.exe Commented Jul 18, 2018 at 15:54
  • I'm using go 1.20, I can "go run ." in Windows and "go run *.go" in Cygwin (which is installed on Windows), but can't "go run *.go" in Windows. Commented Apr 20, 2023 at 12:48
1

Multiple options

  1. go run .
  2. go run *.go
  3. make run using Makefile where, add any of the above command as build target.

for testing

  1. go test ./...
  2. make test using Makefile with go test ./... as build target
0

If you are trying to run multiple files on localhost using gorilla mux in go as per latest version(1.11). Try using any of the following 2 commands.

  1. go install && FolderName -port 8081 .

  2. go build && ./FolderName -port 8081.

Make sure that you are in the source folder ie go/src/FolderName before executing the command in the Terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.