英文:
Go: how many packages per project
问题
在处理我的Go项目时,我意识到遇到了一个小问题。我有一个package
,其中包含一个指向另一个package
中的结构体的指针。然而,该package
还使用了另一个package
中的结构体。在C和C++中,这不会成为问题,因为我会使用头文件保护。然而,在Go中,这样的程序由于无限的import
递归而无法编译。
这让我想知道,我的项目中是否有太多的package
?我应该更倾向于更大的package
吗?我一直被告知每个package
应该专注于一件事。
现在,我的结构如下所示。
game/ <-- 导入engine,需要经常访问资源管理器
video/ <-- 渲染相关内容
renderer.go
shader.go
scene.go
...
engine/ <-- 导入video、file等
root.go <-- 包含Root结构体,处理初始化等
resource.go
...
file/
dds.go
config.go
resource_list.go
script.go
...
main.go
...
说到这里,我有以下几个问题:
- 你会如何解决这个问题? 你会合并
video
和engine
包吗?重新设计程序,使video
不再依赖于engine
吗? - 你如何决定何时创建一个新的
package
? 是基于功能还是按类别分组? - 你对
main
包的使用有多少? 我个人倾向于尽快离开它,也许是因为我太习惯面向对象编程了。
英文:
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:
- How would you solve this? Would you combine the
video
andengine
packages? Redesign your program so thatvideo
no longer depends onengine
? - How do you decide when it's appropriate to make a new package? Do you base it on functionality? Do you group it by category?
- How much use do you make of the
main
package? I personally tend to get out of it as quickly as possible, maybe because I'm too used to OOP.
答案1
得分: 1
由于这些问题相当广泛且与项目相关,我只能希望这些回答对你有所帮助:
- 就个人而言,我会尝试创建一个第三个包,让
video
和engine
都依赖于它。不过,这可能是一个困难的事情,可能需要将它们合并。 - 一个新的包通常是关于一个特定任务的。根据项目的不同,这可能涉及数据库输入/输出,包括所有模型 - 但也可能是一个仅仅从磁盘读取配置文件的包。
- 由于我主要构建Web应用程序,我使用
main
包来启动项目:监听端口,调用其他函数,确保数据库连接正确关闭等。
举个例子(不确定是否有帮助?):
我曾经有一个仅关于配置的包(config
),它引用了其他包(出于某种原因)。然而,其他包依赖于config
中的这些配置。这就是main
包的用武之地:通过参数将config
包与其他包链接起来。
VonC的评论也非常有用和详细。它可能比这个回答对你更有帮助。
英文:
As these questions are rather broad and project-specific, I can only hope this is of any help:
- Personally, I would try to create a third package on which both
video
andengine
can rely. Depending on the project though, this might be a difficult thing to do, and they might need to be merged. - A new package is usually about one specific task. Depending on the project, this might be database I/O, including all models as well - but it might also be a package that's just about reading configuration files from the disk.
- As I've built mostly web-apps, I use the
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论