英文:
Go main package in two files
问题
在$GOPATH/src/testapp
目录下,我有两个文件:
main.go
otherfile.go
main.go文件内容如下:
package main
import "fmt"
func main() {
fmt.Println(SomeFunc())
}
otherfile.go文件内容如下:
package main
func SomeFunc() string {
return "a thing"
}
当我尝试使用go run main.go
运行时,出现了错误:
./main.go:6: undefined: SomeFunc
我原以为我可以创建另一个带有package main
的文件,并且main.go
中的代码可以访问它的声明。为什么这样不起作用?我应该如何做类似的事情?
英文:
In $GOPATH/src/testapp
I have two files
main.go
otherfile.go
main.go has
package main
import "fmt"
func main() {
fmt.Println(SomeFunc())
}
otherfile.go has
package main
func SomeFunc() string {
return "a thing"
}
When I try to run with go run main.go
, I get an error
./main.go:6: undefined: SomeFunc
I thought I could create another file with package main
and code in main.go
would have access to its declarations. Why doesn't this work? How would I do something like this?
答案1
得分: -1
如果我运行go run *.go
,它可以工作。
如果我使用go build
然后运行./testapp
也可以工作。
希望这个问题能帮助将来的某个人。
这个页面上的一些评论对我有帮助:https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/555
英文:
If I run go run *.go
it works.
It also works if I go build
and then ./testapp
.
Hopefully this question helps someone in the future.
Some comments on this page helped me: https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/555
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论