英文:
Golang including local files
问题
我有以下两个文件:
a.go:
package main
func foo() {
return 42
}
b.go:
package main
func main() {
println(foo())
}
根据我所了解的,同一个包(和同一个目录)中的两个文件应该能够使用彼此的函数。然而,当我运行go run b.go
时,我得到以下错误:
# command-line-arguments
./b.go:4: undefined: foo
怎么回事?我需要在b.go中以某种方式导入a.go吗?
英文:
I have the following two files:
a.go:
package main
func foo() {
return 42
}
b.go:
package main
func main() {
println(foo())
}
From what I've learned, it seems as if two files in the same package (and the same directory) should be able to use each other's functions. Yet, when I run go run b.go
, I get the following:
# command-line-arguments
./b.go:4: undefined: foo
What's up? Do I have to somehow import a.go in my b.go?
答案1
得分: 2
你需要使用两个文件运行,命令是 go run a.go b.go
。
英文:
You need to run using the two files go run a.go b.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论