How can I "go run" a project with multiple files in the main package?
Asked Answered
M

9

223

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?

Milline answered 22/1, 2015 at 4:51 Comment(0)
M
298

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
Milline answered 22/1, 2015 at 4:59 Comment(6)
Unless you have *_test.go in your folder. Then you need to (1) shopt -s ext glob and (2) go run !(*_test).go.Hirsutism
and what about those using appengine?Dwarf
I tried go run ./cmd/myCmd/... and it worked for me. Maybe this is the correct way when there are _test.go files?Orthocephalic
go run ../your_folder from inside the working folder works for meNaif
Just do go run .Nardone
You might need to add the ./ prefix. For example go run ./cmdChromatophore
O
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.

Oppenheimer answered 22/1, 2015 at 6:44 Comment(3)
Is there a workaround for running multiple go files with run using the Windows command prompt?Macleod
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.Knell
@Macleod just specify both files separated by a space.Vermination
F
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

Fatal answered 26/7, 2019 at 10:38 Comment(0)
U
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

Umbilication answered 15/8, 2018 at 9:21 Comment(1)
Yes thats correct. In windows i had to run go run .Smitt
M
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
Mulderig answered 24/8, 2020 at 14:46 Comment(0)
P
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'"

Propst answered 2/3, 2023 at 3:28 Comment(4)
Note, though, that go build . uses the module name as the binary name -- so in that case, it matters what you choose.Propst
As noted in the output of go mod init (name), you typically follow this with go mod tidy if your module has external dependencies.Propst
There seam to be reasons that you want to match the name of your folder with the name of the module.Propst
*"install or import elsewhere".Propst
G
1

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

Goatsbeard answered 29/11, 2017 at 23:14 Comment(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.exeDeceased
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.Dorotea
P
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
Photomicroscope answered 25/7, 2022 at 2:46 Comment(0)
P
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.

Place answered 8/10, 2018 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.