Go mod下本地目录结构
Go mod下如何引用本项目的包呢?
大家只要记住这个公式即可:
import的路径 = go.mod下的module name + 包相对于go.mod的相对目录
举例:
我们的课程示例代码中:https://git.imooc.com/coding-180/coding-180/src/master/go.mod
module imooc.com/ccmouse/learngo
tree包在:https://git.imooc.com/coding-180/coding-180/src/master/lang/tree
相对于go.mod的相对路径为:lang/tree
所以引用tree包时:
import “imooc.com/ccmouse/learngo/lang/tree”
在项目内的任一地方,都是用这个路径进行import。
例:
https://git.imooc.com/coding-180/coding-180/src/master/lang/tree/treeentry/entry.go
https://git.imooc.com/coding-180/coding-180/src/master/lang/tree/treeentry_embedded/entry.go
- 更新依赖 go get [包地址@v],go mod tidy
- 将旧项目迁到go mod:go mod init,gobuild ./…