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

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

问题描述

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



go run main.go (main.go是main()函数的文件)



没有任何作用,并且我遇到了一段时间的障碍,因为我不知道如何让程序正常工作。



一些快速搜索引导我回答

go run main.go other.go ..



通过输入我的 package main 包括,我可以让程序运行。然而,这是完全麻烦和令人沮丧的做每一次。



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

解决方案

由于 Nate Finch 注意:


跑步是......真的只是为了用于非常小的程序,通常只需要一个文件。


即使在unix上, go run * .go 通常不正确。在任何使用单元测试的项目中(每个项目都应该有单元测试),这会产生错误:

  go run:can not运行* _test.go文件(something_test.go)

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



已有讨论使得运行 go 命令的其余部分一样工作,并且有一个打开CL( 5164 )。目前正在考虑Go 1.4。与此同时,所有平台上推荐的解决方案是:

  go build&& ./<executable> 


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.

解决方案

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:

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:

go build && ./<executable>

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

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