0

I'm designing a monolith RESTful API that potentially sometime in the future may turn into microservices. I'm also trying following Uncle Bob's clean code as well. My question lies in structuring the Go code. Many times I see the following file structure for example:

|-- users
| --/  driver
| --/  entity
| --/  usescases
| --/  controller
|-- posts
| --/  driver
| --/  entity
| --/  usescases
| --/  controller

And the respective files under each file. This would allow me to more easily convert to microservices if the need by and uses separation of concerns. However I was looking at an example of implementing this in Go and they structured their file system like the following:

| --drivers
|  --/  users
|  --/  posts
| --entities
|  --/  users
|  --/  posts
| --usescases
|  --/  users
|  --/  posts 
| --controllers
|  --/  users
|  --/  posts 

In this discussion the creator claims that the first file structure is not idiomatic Go. I'm relatively new to Go but not Clean Code and wondering if this is really not good Go code? Should I be using the second file structure, even though it will make converting to microservices rather difficult and it doesn't really follow the separation of concerns principle? Thanks!

8
  • I don't think your file structure should have a significant effect on your architecture. You can separate concerns perfectly well no matter where you files are placed on disk, and you can convert to microservices no matter where your files are placed on disk. Commented Oct 4, 2020 at 18:56
  • @PhilipKendall In theory yes, but because Go heavily emphasizes packages I'm not sure if this correct as each folder is a package. Packages influence the scope of different structs and functions. I'm asking for a Go idiomatic Clean Code architecture, not just a normal Clean architecture.
    – Gabe
    Commented Oct 4, 2020 at 19:13
  • File and folder structure is largely a matter of taste. Architectures are generally unaffected by it. Commented Oct 4, 2020 at 20:36
  • @RobertHarvey Isn't the file structure a big aspect of the architecture itself ? In "The principles of clean architecture", Uncle Bob states "this is a rails app because of the file structure [...] why does the file structure tell me what the framework this application is using, why doesn't it tell me what the application does ?". Commented Oct 4, 2020 at 20:39
  • 1
    @Steve: I think the point is that the folder structure doesn't inform the architecture, except in the sense that (if you choose to do so), you pick a folder structure that tracks with the architectural structure for convenience reasons. Commented Oct 4, 2020 at 21:24

1 Answer 1

2

Idiomatic means "natural and correct". The file structure of an application might look or feel natural, correct, or the opposite. But one cannot state only one file structure for a whole language is idiomatic. Yes, you'll have to adapt your implementations, but that is another problem.

Should I be using the second file structure, even though it will make converting to microservices rather difficult and it doesn't really follow the separation of concerns principle?

The idea of microservices where each project can use its own set of programming language, persistence and cache systems, and the idea to use a file structure that screams "Go" as the main architecture feel illogical to me.

All in all, this question is quite subjective. In the discussion you linked, there are ways to circumvent the importing issues you might run into. So, use the file structure you prefer to work with.

1
  • Quick question on your paragraph: "The idea of microservices... illogical to me". When I say idiomatic I'm not looking for something that screams Go, I'm just looking for a solution that will not go against the flow of Go, which is a highly opinionated language. Thanks for your reply and I do think that it really doesn't matter.
    – Gabe
    Commented Oct 4, 2020 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.