英文:
How can I "go run" a project with multiple files in the main package?
问题
我有一个名为main.go
的单个文件,它位于main
包中。由于代码不可重用,我想将代码的一部分分离到同一个包中的不同文件中。
如何将main.go
的内容拆分为多个文件,而不创建单独的包?
我想要以下目录结构:
ls foo
# 输出:
main.go
bar.go
- 文件:
bar.go
package main
import "fmt"
func Bar() {
fmt.Println("Bar")
}
- 文件:
main.go
package main
func main() {
Bar()
}
当我运行go run main.go
时,它给出以下错误:
# command-line-arguments
./main.go:4:2: undefined: Bar
英文:
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
答案1
得分: 286
2019年7月26日更新(适用于go >=1.11)
go run .
在Windows上也可以工作。
原始答案(适用于非Windows环境)
代码实际上是可以工作的。问题在于我应该运行go run main.go
而不是运行:
go run *.go
英文:
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
答案2
得分: 76
2018年8月更新,使用Go 1.11,“运行”部分中提到:
> go run
命令现在允许单个导入路径、目录名或与单个包匹配的模式。
这样可以使用go run pkg
或go run dir
,最重要的是go run .
原始答案,2015年1月
正如在“如何编译由多个文件组成的Go程序?”中提到的,go run
期望一个文件列表,因为它“编译并运行由指定的Go源文件组成的main
包”。
因此,您可以使用go run
将main
包拆分为多个文件。
这与go build/go install
不同,后者期望包名(而不是Go文件名)。
简单的go build
将生成一个以父文件夹命名的可执行文件。
请注意,正如这个线程所示,在Windows CMD会话中,go run *.go
将无法工作,因为shell不会进行通配符扩展。
英文:
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
得分: 12
在我看来,对这个问题的最佳答案隐藏在对顶部答案的评论中。
只需运行以下命令:
go run .
这将运行主包中的所有文件,但不会像下面这样显示错误信息:
go run: cannot run *_test.go files (main_test.go)
向 @BarthesSimpson 致敬。
英文:
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
答案4
得分: 5
如前所述,你可以使用go run *.go
来运行脚本,但在Windows上,你需要列出脚本文件(因为*.go
无法工作),例如go run main.go other.go third.go
。
英文:
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
答案5
得分: 3
第一种方法是运行以下命令:
go run *.go
另一种方法是生成一个可执行文件:
go build
然后运行该.exe文件:
./filename.exe
英文:
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
答案6
得分: 1
对于Windows系统,请安装Cygwin并使用它代替命令提示符。然后,使用命令"go run *.go"即可正常运行。
英文:
For Windows install Cygwin and use it instead of command prompt. "go run *.go" will work then.
答案7
得分: 1
多个选项
go run .
go run *.go
- 使用
Makefile
中的make run
命令,将上述任意命令添加为构建目标。
用于测试
go test ./...
- 使用
Makefile
中的make test
命令,将go test ./...
作为构建目标。
英文:
Multiple options
go run .
go run *.go
make run
usingMakefile
where, add any of the above command as build target.
for testing
go test ./...
make test
usingMakefile
withgo test ./...
as build target
答案8
得分: 1
使用当前版本的Golang,仅使用go run .
是不够的。
首先,你需要进行一次性的文件夹初始化设置:
go mod init noname
现在你可以使用以下命令运行程序:
go run .
虽然它没有解释为什么选择go mod init
的名称的重要性,但这个方法在这里有演示:教程:开始使用Go。一个关键点是,“为你的代码启用依赖跟踪”对于最简单的情况(例如典型的go run .
)是可选的,但对于任何非常简单的应用程序或工作流程(例如使用非标准导入),它基本上是必需的。
上面示例中的noname
可以被任何看起来像相对路径名的东西替换。如果你正在构建一个主模块,不打算在其他地方导入它,那么你使用的模块名基本上无关紧要。
关于go mod init
的基本解释之前在这里提问过:https://stackoverflow.com/q/67606062/86967
注意:如果没有使用go mod init (somename)
,go run .
会返回以下错误...
$ go run .
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
可能有其他解决方案:https://stackoverflow.com/q/66894200
英文:
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: https://stackoverflow.com/q/67606062/86967
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: https://stackoverflow.com/q/66894200
答案9
得分: 0
如果你想在本地主机上使用最新版本(1.11)的gorilla mux在Go中运行多个文件,请尝试使用以下两个命令之一:
- go install && FolderName -port 8081 .
- go build && ./FolderName -port 8081.
在执行命令之前,请确保你在源文件夹中,即go/src/FolderName。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论