Go包依赖布局

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

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

我认为你可以同时使用这两种方法。包可以依赖于子包,而子包可以依赖于包,只要没有循环依赖。标准库中有一些例子,例如:

  1. image依赖于image/color,而包image/draw依赖于包image
  2. encoding/gob依赖于包encoding
  3. 等等...
英文:

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:

  1. Package image depends on image/color, while package image/draw depends on package image.
  2. Package encoding/gob depends on package encoding
  3. etc...

huangapple
  • 本文由 发表于 2017年7月22日 10:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/45249910.html
匿名

发表评论

匿名网友

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

确定