我想在模块存储库中更新单个依赖项。

我浏览了许多文档和JustForFunc视频,似乎执行此操作的方法是进入.mod文件并将其从" 1.0.0"更新为" 1.2.0"并运行go build。

这可行,但是然后我看到我的整个go.mod文件在很多地方都发生了变化,包括那些不是间接的。 其中一些更改的版本是我项目中其他地方使用的依赖项的主要版本更改。 我明显的担心是,当我希望这些版本保持不变时,这将导致重大变化。

我正在使用模块并在我的GOPATH之外工作。

有没有一种方法可以最小化我的.mod文件更改?

tl; dr将-mod=readonly传递给go命令,以防止其将依赖关系自动更新到最新的次要版本/补丁版本。

根据Go Wiki:

To upgrade or downgrade to a more specific version, 'go get' allows version selection to be overridden by adding an @version suffix or"module query" to the package argument, such as go get github.com/gorilla/mux@v1.6.2, go get foo@e3702bed2, or go get foo@'

从同一个Wiki:

The go tooling provides a fair amount of flexibility to adjust or disable these default behaviors, including via -mod=readonly, -mod=vendor, GOFLAGS, GOPROXY=off, GOPROXY=file:///filesystem/path, go mod vendor, and go mod download.

The details on these options are spread throughout the official documentation. One community attempt at a consolidated overview of knobs related to these behaviors is here, which includes links to the official documentation for more information.