简体   繁体   English

如何在主 package 中“运行”一个包含多个文件的项目?

[英]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 .我在main package 中有一个名为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.因为代码不可重用,所以我想将部分代码放在不同的文件中,但在同一个 package 中。

How do I split the contents of main.go into multiple files without creating a separate package?如何在不创建单独的 package 的情况下将main.go的内容拆分为多个文件?

I want a directory structure like this:我想要这样的目录结构:

ls foo

# output:
main.go
bar.go
  • File: bar.go文件: bar.go
package main

import "fmt"

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

func main() {
  Bar()
}

When I run go run main.go , it gives me:当我运行go run main.go时,它给了我:

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

Update 26th July 2019 (for go >=1.11) 2019 年 7 月 26 日更新(适用于 >=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: 2018 年 8 月更新,在Go 1.11 中“运行”部分指出:

The go run command now allows a single import path, a directory name or a pattern matching a single package. go run命令现在允许使用单个导入路径、目录名称或匹配单个包的模式。
This allows go run pkg or go run dir , most importantly go run .这允许go run pkggo run dir ,最重要的是go run .


Original answer Jan. 2015原始答案 2015 年 1 月

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".正如“如何编译由多个文件组成的 Go 程序? ”中所述,go run需要一个文件列表,因为它“编译并运行包含命名的 Go 源文件的main包”。
So you certainly can split your main package in several files with go run .因此,您当然可以使用go runmain包拆分为多个文件。

That differs from go build/go install which expect package names (and not go filenames).这与go build/go install不同,后者需要包名(而不是 go 文件名)。
A simple go build would produce an executable named after the parent folder.一个简单的go build会生成一个以父文件夹命名的可执行文件。

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 .请注意,如 该线程所示go run *.go在 Windows CMD 会话中 不起作用,因为 外壳不进行通配符扩展

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感谢@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另一种方法是生成一个exe文件

go build

Then run that .exe file然后运行那个.exe文件

./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. make run using Makefile其中,添加上述任何命令作为构建目标。

for testing用于检测

  1. go test./...
  2. make test using Makefile with go test./... as build target使用Makefile和 go make test go test./...作为构建目标进行测试

For Windows install Cygwin and use it instead of command prompt.对于 Windows,安装 Cygwin 并使用它代替命令提示符。 "go run *.go" will work then. “go run *.go”将起作用。

If you are trying to run multiple files on localhost using gorilla mux in go as per latest version(1.11).如果您尝试按照最新版本 (1.11) 在 go 中使用 gorilla mux 在本地主机上运行多个文件。 Try using any of the following 2 commands.尝试使用以下 2 个命令中的任何一个。

  1. go install && FolderName -port 8081 .去安装 && FolderName -port 8081 。

  2. go build && ./FolderName -port 8081.去构建 && ./FolderName -port 8081。

Make sure that you are in the source folder ie go/src/FolderName before executing the command in the Terminal.在终端中执行命令之前,请确保您位于源文件夹中,即 go/src/FolderName。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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