katzien / go-structure-examples

Examples for my talk on structuring go apps
MIT License
2.35k stars 287 forks source link

Modular example suffers from cyclic dependencies. #9

Closed ghost closed 5 years ago

ghost commented 5 years ago

The 'storage' package imports both 'beers' and 'reviews' packages. The handler.go file in the 'beers' and 'reviews' packages imports the 'storage' package.

I see that it's stated that all other examples will compile, but then I see no point in presenting a 'modular' example that will not work.

godwhoa commented 5 years ago

For what it's worth, the talk does mention circular dependencies and not compiling with the modular structure: https://youtu.be/B5oQnECDJ8g?t=830

jmafc commented 5 years ago

I've looked at both the layered and modular examples, which both suffer from cyclic dependency problems, as mentioned in the README. FWIW, I think the problem is with segregating the storage module, which creates an artificial distinction. The "model" parts are not just the structure of entities manipulated by your application, but also the code does the manipulation.

In a typical application, the "storage" would be implemented via some database package, relational or otherwise, and IMHO that code would reside in the model package in the layered case and split between the beers and reviews in the modular example. I have applied this to the layered example and it built and ran fine. It's a little trickier, but not impossible in the modular case.