如何通过命令行运行当前目录中的所有.go文件(多文件包) [英] How to run all .go files within current directory through the command line (multi file package)

查看:644
本文介绍了如何通过命令行运行当前目录中的所有.go文件(多文件包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Go的新来者。我非常喜欢这种语言,但是我很快意识到,由于程序大小增加,我需要开始分割文件。

I'm a newcomer to Go. I extremely like the language, but I quickly realised that I needed to start dividing my files due to an increase in program size.

运行main.go (其中main.go已成为我的main()函数的文件)

go run main.go (with main.go having been the file with my main() function)

无法正常工作,我遇到了一段时间的障碍,因为我不知道如何使程序正常工作。

didn't work and I hit a barrier for a while, because I had no clue how to get my program working.

一些快速搜索使我想到了

Some quick searching lead me to the answer of

go run main.go的答案other.go ..

在其中键入我包主文件中的所有文件包括,我可以让程序运行。但是,每次这样做都非常麻烦且令人沮丧。

where by typing all the files that my package main consists of, I could get the programming running. However, this is utterly cumbersome and frustrating to do each time.

我写了以下自我回答的问题,以防止像我这样的人可能再次遇到这个障碍。

I write the following self-answered question in order to prevent others like myself who may again hit this barrier.

推荐答案

内特·芬奇注释:


运行是……实际上仅是为了

Go run is ... really only meant to be used on very small programs, which generally only need a single file.

即使在unix上,也可以运行 * .go 通常是不正确的。在任何具有单元测试的项目中(每个项目都应具有单元测试),这将产生错误:

Even on unix, go run *.go is often not correct. In any project with unit tests (and every project should have unit tests), this will give the error:

go run: cannot run *_test.go files (something_test.go)

它也会忽略构建限制,因此 _windows.go 文件将在Unix上编译(或尝试编译),这不是您想要的。

It will also ignore build restrictions, so _windows.go files will be compiled (or attempted to be compiled) on Unix, which is not what you want.

已经有一个关于使开始运行像其他 go 命令一样工作的一些讨论为此进行删除( 5164 )。目前正在考虑使用Go 1.4。同时,在所有平台上推荐的解决方案是:

There has been a bit of discussion of making go run work like the rest of the go commands, and there's an open CL for it (5164). It's currently under consideration for Go 1.4. In the meantime, the recommended solution on all platforms is:

go build && ./<executable>

这篇关于如何通过命令行运行当前目录中的所有.go文件(多文件包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆