英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论