While working on my Go project today, I realised I had ran into a small problem. I had a package
that had a struct
that held a pointer to a struct from another package
. However, that package
also made use of a struct from the other package. In C and C++, this wouldn't pose an issue, as I'd use a header guard. However, in Go, such a program won't compile due to infinite import
recursion.
This made me wonder, are there too many packages in my project? Should I favour bigger packages? I've always been told that each package should be specifically focused on one thing.
Right now, my structure is like this.
game/ <-- imports engine, needs to access the resource manager a lot
video/ <-- rendering goes here
renderer.go
shader.go
scene.go
...
engine/ <-- imports video, file, etc
root.go <-- contains a Root struct, handles initialisation etc
resource.go
...
file/
dds.go
config.go
resource_list.go
script.go
...
main.go
...
That said, here are my questions:
video
and engine
packages? Redesign your program so that video
no longer depends on engine
?main
package? I personally tend to get out of it as quickly as possible, maybe because I'm too used to OOP.As these questions are rather broad and project-specific, I can only hope this is of any help:
video
and engine
can rely. Depending on the project though, this might be a difficult thing to do, and they might need to be merged.main
package to get things started: listening to ports, calling other functions, making sure database connections are closed properly, etc. Example (not sure if helpful? ) :
I once had a package that was just about configurations (config
), which referred to the rest of the packages (for some reason). The other packages however, depended on those configurations inside config
. This is where the main
package can come in handy: linking the config
package with the other packages, by means of parameters.
The comment from VonC is also very useful and elaborate. It will probably help you more than this.