组织代码的惯用方式

huangapple go评论85阅读模式
英文:

idiomatic way of organizing my code

问题

我感觉这个问题有点模糊,涉及到待关闭的问题、模糊的问题、意见等等,但还是来回答一下吧。

如何组织我的Go代码库?

我有一个非平凡的应用程序(Unix守护进程),它与其他进程通信,读写数据库,与Web服务器通信,并具有内部状态。在C++中,我会编写一堆类(可能在主要子组件的子目录中)。假设这个项目名为projd。

然后在更高的层次上,我有与项目相关的实用CLI函数。projcli1、projcli2...我假设与一个项目相关的所有代码都放在一个GOPATH下。

如果我在几个不相关的项目上工作,我应该使用一个GOPATH还是每个项目一个GOPATH呢?

我试图找到一些示例代码库,但唯一的大型代码库是packages存储库,那只是一堆库,不是完全相同的东西。

如果这些问题太模糊了,那么这个问题如何?同一个目录中的所有Go文件是否必须属于同一个包?

另一个简单具体的问题。我找到了一个很好的对象池实现。它的包名是"pool",很好。我应该把它的单个文件放在哪里?放在一个名为"pool"的子目录中,我想要一个名为"utils"(或类似的)的目录,似乎我不能这样做。我的意思是,我不能同时拥有utils/pool和utils/db(比如)而不需要一个由单个文件组成的目录树。

英文:

I feel the pending 'close question, too vague, opinions,...' but here goes.

How do I organize my go codebase?

I have a non trivial app (unix daemon) that talks to other processes, read/writes a database, talks to a web server, has internal state. In c++ i would write a bunch of classes (probably in subdirectories for the main subcomponents). Lets say that this is projd

Then at a higher level I have utility cli functions associated with the project. projcli1, projcli2.... I assume all the code associated with one project goes under one GOPATH

What If i was working on several unrelated projects. Do I still use one GOPATH or do I have one per project.

I tried to find sample code bases but the only large set is the packages repository, and that's a bunch of libraries - which is not really the same thing.

If those questions are too vague how about this. Is it correct that all the go files in the same directory have to be for the same package?

Another simple specific question. I found a nice implementation of an object pool. Its package says "pool" - fine. Where do I put its single file. In a subdir called 'pool', I would like to have a dir called utils (or something like that) seems like I cannot do it. I mean I cant have utils/pool and util/db (say) without having a tree of one file dirs

答案1

得分: 6

你的问题相当模糊,但其中确实有几个可以回答的问题。

首先,如果你还没有阅读过http://golang.org/doc/code.html,请先阅读。

你只需要一个GOPATH;你可以有多个并在它们之间切换,但我想不出任何使用多个GOPATH的情况。

在同一个目录中的所有go文件必须属于同一个包,这是编译器强制执行的。

在Go中组织一个大型项目时,关键是选择你的包并决定哪些代码放在每个包中。以下是一些建议:

  • 包之间不能循环依赖-如果你的理论布局会导致两个包互相依赖,那是行不通的。
  • 子目录是完全独立的包-它们与父目录中的包没有特殊关系。
  • 每个包应该是独立有用的-如果你必须导入包b才能使用包a中的内容,那么你的布局就有问题了。
  • 不要害怕相对较大的包-一开始在单个目录中有很多go文件可能会感到奇怪(在其他语言中你可能会使用子目录),但实际上并不会引起太多问题。

对于你所说的这种规模的大型项目,我发现它们通常会分为三到四个包:一个或两个实现独立的核心组件,理论上可以在其他项目中重用;一个将特定应用功能实现为库的包;以及一个实现实际程序(守护进程、用户界面等)的包。当然,这完全取决于具体的程序。

英文:

Your question is fairly vague, but you do ask a few answerable questions in it.

First, read http://golang.org/doc/code.html if you haven't already

You should only ever need one GOPATH; you can have several and switch between them, but I can't think of any use case for it.

All go files in a single directory must be for the same package, that is enforced by the compiler.

When organizing a large project in Go, it's all about choosing your packages and deciding what code goes in each package. A few useful tips:

  • package dependencies cannot be cyclic - if your theoretical layout would have two packages depending on each other, that will not work
  • subdirectories are entirely distinct packages - they have no special relationship to the package in their parent directory
  • each package should be independently useful - if you have to import package b in order to make use of the stuff in package a, your layout is confused
  • don't be scared of relatively large packages - it's weird at first to have lots and lots of go files in a single directory (where in other languages you'd be using subdirectories) but it doesn't actually cause too many problems

For large projects of the size you seem to be talking about, I find they usually end up in three or four packages: one or two implementing stand-alone core components that could theoretically be reused in other projects, one implementing the specific application functionality as a library, and one implementing the actual program (the daemon, or the user interface, or what-have-you). It always depends on the program of course.

huangapple
  • 本文由 发表于 2014年5月24日 05:21:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/23838491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定