Home > Article > Backend Development > How to Resolve Go Module Dependency Conflicts with Top-Level and Sub-module Imports?
Resolving Go Module Dependency Conflicts with Top-Level and Sub-module Imports
This issue arises when a top-level module and one of its sub-modules are imported separately as different versions, leading to conflicting dependencies. Consider a scenario where your project has two dependencies, github.com/foo/bar and github.com/raz/mataz. Upon executing go mod download, two different versions of github.com/shared/dependency are downloaded due to the presence of sub-modules.
Inspecting the downloaded modules reveals multiple downloaded versions:
This mismatch results in an ambiguous import error when attempting to import github.com/shared/dependency/api. The go tooling cannot determine which version to use due to the conflicting versions present in both directories.
Solution: Upgrading Dependency Versions
The root cause of this issue is often related to pre-go-modules versioning within the conflicting dependencies. Adding the following line to your go.mod file forces the conflicting dependencies to use go-module-enabled versions:
replace ( github.com/shared/dependency => github.com/shared/dependency v1.2.0 )
This effectively forces references to github.com/shared/dependency to use versions compatible with go modules, resolving the conflicting dependency issue. It is important to note that this solution works because all references to the shared dependency are being directed to go-module-enabled versions.
The above is the detailed content of How to Resolve Go Module Dependency Conflicts with Top-Level and Sub-module Imports?. For more information, please follow other related articles on the PHP Chinese website!