如何在Golang中使用其他文件中的其他结构方法

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2014年9月18日 11:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/25903509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定