英文:
How to use other struct methods from other files in Golang
问题
我有一个名为login.go和account.go的文件。
在login.go中:
func (api *ApiResource) test() {
fmt.Println("Works!")
}
在account.go中:
func main() {
Res := new(ApiResource)
Res.test()
}
但是我得到了undefined:test错误。
它们都使用package main,并且位于同一个src/文件夹中。
我需要在这里修复什么?
英文:
I have a file called login.go and account.go
In login.go
func (api *ApiResource) test() {
fmt.Println("Works!")
}
In account.go I have:
func main () {
Res := new(ApiResource)
Res.test()
}
Buit I'm getting undefined:test error.
They both use package main and are on same src/ folder
What do I need to fix here?
答案1
得分: 2
如果你使用了 go run,那么你必须像这样同时传递两个文件:go run login.go account.go。
英文:
If you used go run then you must pass both files to like go run login.go account.go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论