相同依赖的两个版本 - 较低版本被忽略

Two version of same dependency - lower version getting ignored

我有一个项目,其中两个依赖项使用同一库的不同版本。例如,我的项目有依赖项 A 和依赖项 BAB,都使用了一个共同的library/dependencyX,但是版本不同。 Av1 版本的 XBv2 版本的 X。所以现在当我在我的项目中添加 AB 作为依赖项时,我的项目的 go.sum 中有两个版本的 X

我期待,AB 将在 运行 时引用相应的版本。但事实并非如此。不知何故,当我 运行 测试我的项目时,A 使用 Xv2,理想情况下它应该使用 v1(因为在 go.mod A,明确 specified/added v1)。所以它打破了执行,因为 v1Xv2 有很多差异。

所以在我的项目中,如何明确指定使用 Xv1 by A 并使用 v2 by B? go modules中有这样的规定吗?

您的 B 包必须导入带有 /v2 后缀的 X

Go Wiki: Modules: Semantic Import versioning:

Recall semver requires a major version change when a v1 or higher package makes a backwards incompatible change. The result of following both the import compatibility rule and semver is called Semantic Import Versioning, where the major version is included in the import path — this ensures the import path changes any time the major version increments due to a break in compatibility.

As a result of Semantic Import Versioning, code opting in to Go modules must comply with these rules:

  • If the module is version v2 or higher, the major version of the module must be included as a /vN at the end of the module paths used in go.mod files (e.g., module github.com/my/mod/v2, require github.com/my/mod/v2 v2.0.0) and in the package import path (e.g., import "github.com/my/mod/v2/mypkg").

导入路径中的这个版本后缀将使它们成为 2 个“不同”的包。如果 AB 将使用相同的 X 主要版本,则不会有 2 个版本,将选择较高版本(“最小版本选择”算法)。有关详细信息,请参阅 Version Selection