如何在 golang 中对包进行版本控制?

how to version packages in golang?

我已经阅读了一大堆关于导入 3rd 方 go 包的文章和 SO 问题,这些看起来都很简单,但我不明白的是 none 我已经阅读过任何参考版本控制。在 Dartlang 中有一个 pubspec 文件,它定义了你的包,包括它的版本和它的依赖项,包括它们所需的版本。如果我执行 go get github.com/gorilla/sessions 并编写我的应用程序,那么 6 个月后我必须清除我的目录并重新获取所有内容,此时该包已经更新并破坏了与我使用旧版本的代码的向后兼容性版本?

正式版,来自GO FAQ:

If you're using an externally supplied package and worry that it might change in unexpected ways, the simplest solution is to copy it to your local repository. (This is the approach Google takes internally.) Store the copy under a new import path that identifies it as a local copy.

该方法有很多替代方法,主要基于声明您正在使用的那些项目的确切版本。

参见例如“Dead Simple Dependencies in Go -- Keep it simple and keep your sanity." (based on emil2k/vend)

Go 依赖管理的主要不同选项列于:

"Go Package Management -- A summary of dependency management in Go"
(及其关联 GOPM mailing list

2015 年 7 月更新:

2017 年第 4 季度更新:如前所述 , go dep is the official tool for pinning version of dependencies (even though that pinning approach is not without criticism: see "The cargo cult of versioning").
它应该在 Go 1.10 开发开始时合并到工具链中,according to its roadmap.

2018 年第 2 季度更新:go dep 已被 go mod (modules) in Go 1.11, 取代。

我使用 dep 作为 golang 项目的依赖管理工具。请使用以下 link dep tool 获取更多信息。

dep is a dependency management tool for Go. It requires Go 1.9 or newer to compile.

dep was the "official experiment." The Go toolchain, as of 1.11, has (experimentally) adopted an approach that sharply diverges from dep. As a result, we are continuing development of dep, but gearing work primarily towards the development of an alternative prototype for versioning behavior in the toolchain.

当前状态:2019 年 1 月

dep is safe for production use.