Bryce
Bryce

Reputation: 3384

how to upgrade go mod to v2 or higher version?

My go package version is v1.0.7 and now I want to upgrade it to v2.0.0. I create a new tag with it bug when I use go get CODEPATH it still use version v1.0.7. The go.mod should like require CODEPATH v2.0.0+incompatible but I want to know what command will do this?

The document Modules says that add /v2 to module path but didn't tell how to upgrade client's go.mod.

Upvotes: 8

Views: 3502

Answers (2)

Bryce
Bryce

Reputation: 3384

I tried myself and it worked.

  1. Add /v2 to your go.mod's module line module github.com/mnhkahn/aaa/v2;
  2. If you import a sub-package of the module, import like this import "github.com/mnhkahn/aaa/v2/config";
  3. Create a tag with name v2.0.0;
  4. go get github.com/mnhkahn/aaa/v2;
  5. go mod tidy;

Upvotes: 6

thepudds
thepudds

Reputation: 5314

The answer from Bryce looks good if you are doing this manually.

If you are interested in an automated approach (for example, perhaps you have many files you would need to visit), a good automated solution is https://github.com/marwan-at-work/mod, which can automatically add, remove, or change the required /vN in your *.go code and your go.mod. See this answer for more details.

Upvotes: 3

Related Questions