大型项目的包层次结构

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

Package hierarchy of large scale projects

问题

我想参与一个大型项目的开发。为了符合常见的编程范式,我想将我的项目划分为多个包。一个示例如下:

project/
    domain/
    utils/
    system/

Go的包管理系统似乎不适合这种包的划分方式。每个包都被假定为独立的包。但是在处理大型项目时,可能需要使用相互依赖的包。有几个原因可以构建这样的层次结构:

  • 松耦合的代码
  • 代码基的逻辑分离
  • 在单个版本控制系统存储库中管理整个项目

那么,在Go中是否可以实现这一点,还是我应该适应将所有内容都放在GOPATH下的方式?在这种情况下,我必须为每个包创建一个存储库,并且可能会有一些包可以从不相关的包中导入,但对它们来说没有意义。

英文:

I want to work on a large scale project. For the sake of common programming paradigms, I want to divide my project into packages. An example is as follows:

project/
    domain/
    utils/
    system/

The Go packaging system seems to be not appropriate for this kind of packaging. Each package is assumed to be standalone packages. But when dealing with a large scale project, one may have to use packages somehow dependent to each other. There are several reasons to build a hierarchy like this:

  • Loosely coupled code
  • Logical separation of code base
  • Managing whole project in a single VCS repository

So, is this possible in Go or should I just adapt myself to package everything under GOPATH? In this case, I have to create a repository for each package and there will be packages that importable from irrelevant packages but meaningless mechanic for them.

答案1

得分: 7

每个Go中的包(除了main)都像一个库一样运行。

你应该像创建一个库一样组织你的包。这看起来非常简单和有效的方法-它遵循Go的主要原则。一个库应该了解自己的一切。

但是在处理大型项目时,可能需要使用相互依赖的包。

这看起来是一个问题的焦点。为什么你想要有两个相互依赖的包(ab)-这违反了Go的一个原则(循环依赖)。如果你无论如何考虑这种用法,那么你应该合并这些包。

如果你仍然不确定如何组织包-那么看一下Go标准库。一个很好的例子可能是http包

在我们的项目中,我们希望将应用程序类型(模型)分离到单独的包中,这样我们可以在项目的其他应用程序中使用它们。你需要考虑你的项目的哪个部分你想要用作应用程序,哪个部分用作库/驱动程序。

松耦合的代码

实际上,在同一个包(目录)中的文件数量没有限制。*[更新]*包是同一个目录中的一组文件。包/目录可以嵌套。

在单个VSC存储库中管理整个项目

没有任何东西会阻止你将项目中的所有库放在一个存储库中。Go标准库(有几十个)就在一个存储库中。

[更新]此外,你需要理解包和go get之间的区别以及如何解析/获取它们。go get是一个工具,它解释import路径并处理具有支持的前缀的路径(例如:github、bitbucket...-你可以在go get文档中了解它们)。所以如果一个包以github/user/name开头,它会使用HTTPS将整个存储库克隆到本地。

所以例如,我在同一个存储库中有两个包:

  • "github.com/scale-it/go-web"
  • "github.com/scale-it/go-web/handlers"

go get会理解它们并只克隆一次(在*$GOPATH/src*目录下)。

英文:

Each package (besides main) in Go behaves like a library.

You should compose your packages as you would produce a library. It looks to be very simple and valid approach - it follows main Go principles. A library should know everything about itself.

> But when dealing with a large scale project, one may have to use packages somehow dependent to each other.

This looks like a spot of problems. Why you will like to have two packages (a and b) which depend on themselves (a requires b and b requires a) - it's against one of the Go principles (circular dependency). If you anyhow think about this usage, then you should merge those packages.

If you are still unsure how to compose packages - then look at Go standard library. Good example might by http package.

In our project we want to separate application types (model) into separate package, so we can use them in other applications of a project. You need to think which part of you project you want to use as an application, and which as a library/driver.

> Loosely coupled code

Practically, there is no limit in files number in the same package (directory).<br />
[update] Package is a bunch of files within the same directory. Packages / directories can be nested.

> Managing whole project in a single VSC repository

There is nothing which may stop you to keep all libraries from the project in a single repository. Go std packages (there are dozens of them) are in a single repository.

[update] Also you need to understand difference between packages and how go get resolves / fetch them. go get is a tool which interprets import paths and handles ones with supported prefix (eg: github, bitbucket .... - you can read about them in go get docs). So if a packages starts with github/user/name it clones whole repository locally using HTTPS.

So for example I have two packages in the same repository:

  • "github.com/scale-it/go-web"
  • "github.com/scale-it/go-web/handlers"

And go get understand them and clone only once (under $GOPATH/src directory)

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

发表评论

匿名网友

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

确定