通过导入父目录,使用 golang 导入子包?
时间:2023-04-26
浏览:130
作者:佚名
知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《通过导入父目录,使用 golang 导入子包?》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!
问题内容
我有这个 main.go 文件:
package main
import "one/entities/bar"
func main(){
}
我有这个目录结构:

当我构建我的项目时:
go install main
我得到这个编译错误:
src/main/main.go:3:8:/home/oleg/codes/oresoftware/oredoc/test/builds/go/src/one/entities/bar 中没有 Go 文件
这个错误是有道理的。有没有办法从one/entities/bar目录中导入所有子包?
像这样的东西:
package main
import bar "one/entities/bar/*"
func main(){
}
(使用某种 * 语法并导入 bar 命名空间中的所有子包)。
最终我正在尝试做这样的事情:
package main
import (
"log"
"one/entities/bar"
)
func main(){
v := bar.Get.Basic.Req.Headers{}
log.Fatal(v)
}
正确答案
根据以下规格,您不能做这样的事情import:
The import names an identifier (PackageName) to be used for access and an
ImportPath that specifies the package to be imported.
ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
ImportSpec = [ "." | PackageName ] ImportPath .
ImportPath = string_lit .
导入语法需要包名或包的路径。所以没有通配符导入。
该声明将阐明原因;
The PackageName is used in qualified identifiers to access exported identifiers of the package within the importing source file.
您必须指定您所引用的元素存在于哪个包中。例如,bar.Get.Basic.Req.Headers。如果没有显式导入,则无法确定这一点。
今天关于《通过导入父目录,使用 golang 导入子包?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于golang的内容请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
