简体   繁体   中英

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

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

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

go run .

Original answer

The code above actually works. The problem was I needed to run

go run *.go

instead of

go run main.go

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 .

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

如前所述,您可以说go run *.go但对于 Windows,您可以只列出脚本文件(因为 * go run main.go other.go third.go不起作用)- go run main.go other.go third.go

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM