Avatar ·

在其他文件中声明的函数不可见 — Go中的“undefined”错误

大家好!我刚开始学习Go,遇到了一个非常奇怪的问题。我在同一个 `main` 文件夹中有两个文件: **main.go:** ```go package main func main() { hello() } ``` **helper.go:** ```go package main func hello() { println("Hello from helper!") } ``` 我用命令 `go run main.go` 运行程序,却得到错误: ``` # command-line-arguments ./main.go:4:2: undefined: hello ``` 为什么会这样?两个函数都在同一个 `main` 包中,而且在其他语言(比如Python或Java)中,同一文件夹中的文件通常会自动“看到”彼此。我试过重命名文件、检查语法——都没有效果。请告诉我,我哪里做错了?在Go中如何正确组织代码,使不同文件中的函数可以互相访问? <<>>

为什么会出现“undefined”错误

问题在于,命令 go run main.go 只运行了一个文件—— ma