This commit is contained in:
oneao committed 2026-03-18 17:14:34 +08:00
1 parent 52cef511d6
commit 68f08d921a
184 files changed
+19499 -1

No files matched your search

@@ -0,0 +1,36 @@
## golang 同一个包中函数互相调用报错 undefined
demo 文件夹下有两个文件,分别为 hello.go 和 main.go ,结构如下:
```
demo/
├── hello.go
└── main.go
```
文件内容如下:
```go title="hello.go"
package main
import "fmt"
func hello() {
fmt.Println("hello, world")
}
```
```go title="main.go"
package main
func main() {
hello()
}
```
允许代码后报错:./main.go:4:2: undefined: hello
**原因**:Go 中 main 包默认不会加载其他文件, 而其他包都是默认加载的。
**解决方法**:
1. 执行 `go run main.go hello.go`