Golang主包中的多个文件 [英] Golang multiple files in main package

查看:1388
本文介绍了Golang主包中的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的主包中有一个名为 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()
}


推荐答案

上面的代码实际工作。问题是我需要运行

The code above actually works. The problem was I needed to run

go run *.go

而非

instead of

go run main.go

这篇关于Golang主包中的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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