我该如何“跑步"?一个在主程序包中有多个文件的项目? [英] How can I "go run" a project with multiple files in the main package?
本文介绍了我该如何“跑步"?一个在主程序包中有多个文件的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的主程序包中目前只有一个文件,称为main.go
.如何将main.go
的内容拆分为多个文件,而又不创建单独的程序包,因为代码不可重用.
I currently have a single file in my main package called main.go
. How do I split the contents of main.go
into multiple files without creating a separate package because the code isn't reusable.
我想要这样的目录结构:
I want a directory structure like this:
$ 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
推荐答案
2019年7月26日更新(适用于go> = 1.11)
go run .
原始答案
上面的代码实际上有效.问题是我需要跑步
Original answer
The code above actually works. The problem was I needed to run
go run *.go
代替
go run main.go
这篇关于我该如何“跑步"?一个在主程序包中有多个文件的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文