I'm creating a REST API, and I've separated each resource into it's own package. I've got a User
and a Group
package. There is a many to many relationship between the two resources. So I need User
imported into the Group
and Group
imported to the User
. Since both structs need them as fields.
My question is, should I create another package and import both and then import that package into each of these packages?
Or does it make more sense to combine these two packages into one in go lang?
Put them in the same package, but leave them in separate files.
What I have done in the same situation is:
I put the structures alone into a separate package models
, which contains the structures for both as userModel.go
and groupModel.go
and have kept the functions related to user
and group
in their own package.
Both user
and group
packages include the model
package.
However, this is a very opinionated answer. As Joshua's post says
, we can also put everything together. The deciding factor should be, whether user
and group
functionalities would be used in other packages outside them or not. If so, then to keep a model package would have its benefits.