Naming is a very important thing, but everyone is different.

Why is replace used

In the beginning, I wrote A project A with A repository named project-alpha, but the package in go.mod is set to github.com/k8scat/alpha. When I want to reference the code of project A in another project B, generally speaking, Using github.com/k8scat/project-alpha, because go get will use git, but due to the warehouse is not the same as the name of the package and code, need to use the replace conversion:

Package github.com/k8scat/beta require github.com/k8scat/alpha v0.0.0 replace github.com/k8scat/alpha Github.com/k8scat/project-alpha v1.0.0Copy the code

Use go mod Edit to set replace:

Go mod edit - the replace github.com/k8scat/alpha=github.com/k8scat/[email protected]Copy the code

Note: this command only sets replace; require requires another setting, and the dependency version of require is ignored; replace’s dependency version is used.

Other use scenarios for Replace

Fix golang.org dependency that cannot be downloaded

Go mod edit-replace golang.org/x/crypto=github.com/golang/[email protected]Copy the code

Note: The target version must conform to the SemVer specification, not master and latest (go get can be used).

The resources

  • Go get the source code
  • Use replace in go modules
  • Semantic Versioning