Upgrade Your Go project to next major version [Part-I]
Seamlessly Upgrade your project with these steps
Overview
As your Go project evolves, there may come a time when a major version upgrade is necessary to incorporate new features, refactor code, or address architectural improvements.
However, upgrading from one major version to another can be a challenging task, if one doesn’t know the right way to deal with it. In this article, I will provide a comprehensive guide on how to successfully upgrade your Go project from version X to X+1. I will cover essential steps, considerations, and best practices to ensure a smooth transition while minimising disruptions.
Pre-requisites
- Project uses Golang
- go mod is used
Steps to be followed for go module version update on the occasion of a major release:
1) Update go.mod file
In the new version’s go.mod file, append new major version number to the module path, as in the following example:
- Existing version: github.com/arzzon/golang-project-sample
- New version: github.com/arzzon/golang-project-sample/v2
Or if already some major versions have been release then
- Existing version: github.com/arzzon/golang-project-sample/v2
- New version: github.com/arzzon/golang-project-sample/v3
2) Update import paths
In source code, update every imported package path where you import a package from the module, appending the major version number to the module path portion.
- Old import statement: import “github.com/arzzon/golang-project-sample/utils”
- New import statement: import “github.com/arzzon/golang-project-sample/v2/utils”
An IDE will make your life easy while making these changes.
If you are using Goland IDE then refer the following steps
Open go.mod file → select the module name → Left click → Refactor → Rename → Update the module name → Click Refactor
NOTE: Since IDEs keep changing their options, these steps may not be applicable to your IDE, just figure it out.
3) Ensure all dependencies are updated
Run the following command to download new dependencies(if we have added any new dependencies)
go mod tidy
4) Run the following commands to check everything went well
go vet <path to your main.go file>
You may run you Unit tests to ensure nothing is broken.
5) Commit your changes
git add .
git commit -m “<commit message>”
Congrats!!! Now you have upgraded your Go project to a new version!!