216

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
package main

import "fmt"

func Bar() {
  fmt.Println("Bar")
}
  • File: main.go
package main

func main() {
  Bar()
}

When I run go run main.go, it gives me:

# command-line-arguments
./main.go:4:2: undefined: Bar
Teocci
  • 7,189
  • 1
  • 50
  • 48
Neil
  • 8,925
  • 10
  • 44
  • 49

9 Answers9

286

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
Teocci
  • 7,189
  • 1
  • 50
  • 48
Neil
  • 8,925
  • 10
  • 44
  • 49
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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Is there a workaround for running multiple go files with `run` using the Windows command prompt? – Luke Jul 18 '16 at 04: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 Oct 10 '18 at 07:34
  • @Luke just specify both files separated by a space. – xuiqzy Sep 13 '20 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

chim
  • 8,407
  • 3
  • 52
  • 60
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

Karl Penzhorn
  • 375
  • 1
  • 3
  • 12
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
Rohan
  • 171
  • 6
1

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

Alex Dvoretskiy
  • 317
  • 2
  • 9
  • 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` – Aleksandr Kravets Jul 18 '18 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. – user1283182 Apr 20 '23 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
ralonr
  • 180
  • 1
  • 1
  • 9
1

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'"

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
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.

Bharath Kuppala
  • 176
  • 1
  • 3
  • 14