依赖三角形

依赖三角形

问题描述:

Dependencies is an issue for Go but this is something new:

  • Application A directly dependent on libraries B and C
  • Library B directly dependent on library C

If we have something like this in code of the application A:

funcyInstance := &C.FuncyObject{}
B.CleverFunction(funcyInstance)

When in lib B:

func CleverFunction(arg *C.FuncyObject) {}

It raises an error:

cannot use funcyInstance (type "*A/vendor/github.com/C".FuncyObject) as type "*B/vendor/github.com/C".FuncyObject in argument to B.CleverFunction

I'm using Glide as a dependency manager.

I understand that this configuration of dependencies causes existence of several types instead of a single one (for all library) and possibly it is just an anti pattern for Go. Anyway... How to solve the issue?

I'd remove the vendor folder below B, and put C in the root vendor folder (for application A if I am getting your structure correctly).

That way, you only end up with one place for each type.

Not sure why B would have a vendor folder in the first place, since Glide's recommendations are clear on this:

http://glide.readthedocs.io/en/latest/vendor/

Libraries (codebases without a main package) should not store outside packages in a vendor/ folder

and

In applications (codebases with a main package) there should only be one vendor/ directory at the top level