DEV Community

Bo
Bo

Posted on

Golang: version your module

#go

Quote the "Learning Golang" book:

Go supports two ways for creating the different import paths:

  • Create a subdirectory within your module named vN, where N is the major ver‐ sion of your module. For example, if you are creating version 2 of your module, call this directory v2. Copy your code into this subdirectory, including the README and LICENSE files.
  • Create a branch in your version control system. You can either put the old code on the branch or the new code. Name the branch vN if you are putting the new code on the branch, or vN-1 if you are putting the old code there. For example, if you are creating version 2 of your module and want to put version 1 code on the branch, name the branch v1.

Say you have a repo at gitlab.com/yourname/gomath, now you want to create a version 2, if you choose the 2nd method from above, then you need to do:
1, At your current repo, create a branch named v1 then commit it:

$ git checkout -b v1
$ git push --set-upstream origin v1
Enter fullscreen mode Exit fullscreen mode

2, Switch back to your main branch, make the v2 change to your package

$ git checkout main
# make some v2 change
Enter fullscreen mode Exit fullscreen mode

3, Change the "module" path, append v2 to it in the go.mod file:

# before
module gitlab.com/yourname/gomath
# change it to
module gitlab.com/yourname/gomath/v2
Enter fullscreen mode Exit fullscreen mode

4, Commit the change and push it.

Now you are able to use the new version:

$ go get gitlab.com/yourname/gomath/v2
Enter fullscreen mode Exit fullscreen mode

Discussion (0)