如何从父目录导入模块?

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

How to import from parent directory

问题

我有以下的Golang目录结构:

hello
├── content
|  ├── types.go
|  ├── log
|       ├── logging.go
|── main.go
|── go.mod

在types.go中:

package content;

func GetTypes() (string) {
//...................
}

在logging.go中,想要从父目录中使用GetTypes():

package log;
import (
   "hello/content"
)

GetTypes()

但是得到以下错误:

undeclared name: GetTypes

如何让子目录的文件从父目录中导入?

英文:

I have the following golang directory structure:

hello
├── content
|  ├── types.go
|  ├── log
|       ├── logging.go
|── main.go
|── go.mod

in types.go:

package content;

func GetTypes() (string) {
//...................
}

in logging.go want to use GetTypes() from parent directory:

package log;
import (
   "hello/content"
)

GetTypes()

but get the following:

undeclared name: GetTypes

How the sub directory files can import from parent directory?

答案1

得分: 1

如果你想使用来自不同包(父包、子包或任何第三方包)的任何函数或变量,你必须导入该包。

最后,使用packagename.FuncName()来使用该函数。

package log;
import (
   "hello/content"
)

...
   content.GetTypes()
英文:

If you want to use any function or variables from a different package (either parent or child or any third party package), you must import that package.

And finally use packagename.FuncName() to use that function.

package log;
import (
   "hello/content"
)

...
   content.GetTypes()

huangapple
  • 本文由 发表于 2021年12月23日 18:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/70460623.html
匿名

发表评论

匿名网友

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

确定