英文:
Go Package dependency layout
问题
在Go语言中,最佳实践是让包依赖于其子包,还是让子包依赖于包?显然,两者都不行,因为那样会引入循环依赖。
例如:
└── a
├── a.go
├── b
   └── b.go
在这种情况下,a应该依赖于b,还是b应该依赖于a?
英文:
In Go, is it best practice to have packages depend on their sub-packages or the other way around? Obviously you can't have both because that would introduce a circular dependency.
For example
└── a
├── a.go
├── b
   └── b.go
Should a depend on b, or should b depend on a?
答案1
得分: 4
我认为你可以同时使用这两种方法。包可以依赖于子包,而子包可以依赖于父包,只要没有循环依赖。标准库中有一些例子,例如:
- 包
image依赖于image/color,而包image/draw依赖于包image。 - 包
encoding/gob依赖于包encoding。 - 等等...
英文:
I think you can use both approach. Package may depend on sub-packages, and a sub-package may depend on the parent package, as long as there is no circular dependency. There are examples in the standardlib, e.g:
- Package
imagedepends onimage/color, while packageimage/drawdepends on packageimage. - Package
encoding/gobdepends on packageencoding - etc...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论