如何通过命令行运行当前目录中的所有.go文件(多文件包)

huangapple go评论131阅读模式
英文:

How to run all .go files within current directory through the command line (multi file package)

问题

我是Go的新手。我非常喜欢这门语言,但很快意识到由于程序规模增加,我需要开始将文件分开。

go run main.go(其中main.go是包含main()函数的文件)

这个命令不起作用,我陷入了一段时间的困境,因为我不知道如何让我的程序工作起来。

通过快速搜索,我找到了答案:

go run main.go other.go ..

通过输入package main所包含的所有文件,我可以让程序运行起来。然而,每次都这样做非常麻烦和令人沮丧。

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

英文:

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.

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.

答案1

得分: 108

最终我们现在可以使用:

  1. go run .

感谢:
https://github.com/golang/go/issues/22726#issuecomment-345841019

英文:

Finally we can now use:

  1. go run .

thanks to:
https://github.com/golang/go/issues/22726#issuecomment-345841019

答案2

得分: 76

根据Nate Finch的观点:

go run...实际上只适用于非常小的程序,通常只需要一个文件。

即使在Unix上,go run *.go通常也是不正确的。在任何带有单元测试的项目中(每个项目都应该有单元测试),这将导致错误:

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

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

关于使go run像其他go命令一样工作,已经有一些讨论,并且有一个开放的CL(5164)正在考虑中,预计会在Go 1.4中实现。与此同时,在所有平台上推荐的解决方案是:

  1. go build && ./<executable>
英文:

As Nate Finch notes:

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

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:

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

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.

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:

  1. go build &amp;&amp; ./&lt;executable&gt;

答案3

得分: 26

Unix相关系统

在大多数情况下,go run *.go就足够了。

如果出现错误,请继续下面的方法。

Windows系统(以及其他情况下go run *.go不起作用的情况)

在Windows命令行中,令牌扩展不起作用,因此上述方法将无法工作并显示错误。在某些情况下,由于当前编译器的限制,go run *.go也可能无法在操作系统中工作。

在这些情况下,使用以下命令:

go build && foo.exe

其中foo.exe是生成的.exe文件的名称。
如果你不知道可执行文件的名称,首先运行

go build,然后检查生成的.exe文件的名称。然后,使用包含文件名的方法。

这两种方法将在当前目录中构建和运行所有的.go文件,操作简便。

英文:

Unix related systems

go run *.go will be sufficient in most cases.

Continue to the below method if this causes errors.

Windows systems (and in other cases where go run *.go doesn't work)

Token expansion doesn't work in the windows command line and hence the above will not work and display an error. go run *.go may also not work in OSs in some cases due to current compiler limitations.

In these cases, use

go build &amp;&amp; foo.exe

where foo.exe is the name of the .exe file produced.
If perhaps you have no idea what the name of your executable is, first

go build and check the name of the .exe file produced. Afterwards, use the method that includes the file name.

These 2 methods will build and run all the .go files within your current directory with minimum fuss.

答案4

得分: 26

你可以使用以下bash命令运行所有的.go文件(不包括测试文件):

  1. go run $(ls -1 *.go | grep -v _test.go)
英文:

You can run all .go files, excluding tests, using this bash construction:

  1. go run $(ls -1 *.go | grep -v _test.go)

答案5

得分: 19

最好的方法是这样运行:

  1. go run !(*_test).go

这样可以跳过所有的测试文件,这正是你需要避免错误的地方。

另一个建议是:

  1. go build && ./<可执行文件>

这有点麻烦。你必须一直删除可执行文件,以避免被git标记。当然,你可以将其放入gitignore中,但我比较懒,这是一个额外的步骤。

英文:

The best way to do it is to run it like this:

  1. go run !(*_test).go

It skips all your test files which is exactly what you need to avoid the error.

The other suggestion:

  1. go build &amp;&amp; ./&lt;executable&gt;

is a bit annoying. You have to delete the executable all the time to avoid being marked by git. You can put it in gitignore, of course, but I am lazy and this is an extra step.

答案6

得分: 8

如果我理解你的问题正确,你需要将其他代码作为库导入。

小例子

./testing/main.go:

  1. package main
  2. import "fmt"
  3. import L "testing/lib"
  4. func main() {
  5. fmt.Println("Hello from main()")
  6. L.Somefunc()
  7. }

./testing/lib/somelib.go:

  1. package lib
  2. import "fmt"
  3. func Somefunc() {
  4. fmt.Println("Hello from Somefunc()")
  5. return
  6. }

要运行 - go run main.go

英文:

If i understand your question right, you need import other code as libraries.

Little example

./testing/main.go:

  1. package main
  2. import &quot;fmt&quot;
  3. import L &quot;testing/lib&quot;
  4. func main() {
  5. fmt.Println(&quot;Hello from main()&quot;)
  6. L.Somefunc()
  7. }

./testing/lib/somelib.go:

  1. package lib
  2. import &quot;fmt&quot;
  3. func Somefunc() {
  4. fmt.Println(&quot;Hello from Somefunc()&quot;)
  5. return
  6. }

To launch - go run main.go

答案7

得分: 4

只需使用以下命令:

  1. go run *.go

假设您没有任何测试文件,它将正常工作。

英文:

just use this

  1. go run *.go

it will work assuming u don't have any test files

答案8

得分: 3

如果你在Windows上的Vscode终端中运行,并且对工作区没有了解,只需按照以下步骤操作:

  1. go run $(ls *.go)
英文:

If you are run in terminal of Vscode on Windows AND without knowledge about workspace, just do like this:

  1. go run $(ls *.go)

答案9

得分: 2

这是我的解决方案:

  1. 运行以下命令:
  2. go run $(find . -name "*.go" -and -not -name "*_test.go" -maxdepth 1)

我使用一个别名来方便地运行命令行应用程序:

  1. 别名 gorun='go run $(find . -name "*.go" -and -not -name "*_test.go" -maxdepth 1)'
  2. $ gorun 参数1 参数2
英文:

Here is my solution:

  1. go run $(find . -name &quot;*.go&quot; -and -not -name &quot;*_test.go&quot; -maxdepth 1)

I use it with an alias to make it easy to run command line apps

  1. alias gorun=&#39;go run $(find . -name &quot;*.go&quot; -and -not -name &quot;*_test.go&quot; -maxdepth 1)&#39;
  2. $ gorun param1 param2

答案10

得分: 1

在Windows上,我通常只需在所有项目目录中添加一个名为test.bat的小脚本:

  1. go build
  2. .\{project_name}.exe
  3. go clean

这样就足够好用了。当然,将{project_name}替换为你的可执行文件的名称。一旦可执行文件完成,批处理脚本就会继续进行清理操作。

英文:

On Windows I usually just add a little test.bat to all my project directories:

  1. go build
  2. .\{project_name}.exe
  3. go clean

Works well enough. Replace {project_name} with the name of your executable, of course. And once the executable finishes, the batch script moves on to the clean up.

答案11

得分: 0

对于尝试使用go run结合go generate的人来说,一个解决方案可以是:

  1. //go:generate sh -c "go run path/*.go"
英文:

For peoples attempting to use go run combined with go generate a solution can be :

  1. //go:generate sh -c &quot;go run path/*.go&quot;

答案12

得分: -1

对于Windows系统,以下操作适用:
打开命令提示符(cmd),并进入包含你的文件夹的路径。然后输入以下命令并按回车键。

  1. go build

执行完毕后,将生成一个可执行文件。然后在命令提示符中调用该可执行文件。
如果你的可执行文件名为Project.exe,则输入以下命令并按回车键:

  1. Project.exe
英文:

For window the following works:
Open cmd and go to the path where your folder exists. Then type the following command and press Enter.

  1. go build

after this one executable will be created. Then in the command prompt call the executable.
If your executable name is Project.exe then type the following and press Enter:

  1. Project.exe

huangapple
  • 本文由 发表于 2014年5月16日 20:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/23695448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定