Introduction
Go 1.18 added the workspace mode to Go, allowing you to work on multiple modules simultaneously without having to edit the go.mod file.
Module is a folder having go.mod
file
Earlier if you are working with multiple modules the flow would look like below
- Make the change in module1.
- Update the go.mod file of module2 with
replace
directive for your local, unpublished changes. - Make the changes in module2.
- Before pushing to remote remember to remove the
replace
from thego.mod
.
What workspace allows you to do is allow you to access the changes from module1 in module2 without updating the go.mod
files.
Thus allowing developers to fully focus on writing code, only when you are going to complete your local development flow and are going to push to remote, we need to update the version tag of module1 in module2’s go.mod file.
How to Setup Go Workspace
Setting it up is even simpler.
- Create a folder that will contain all your modules that you want to be part of a workspace, we will name it
workspace-1
- Add your modules to this folder. Your folder structure will look something like this.
- Now, assuming you are using Go version > 1.18 (i recommend the latest stable version)
- Execute
go work init
in theworkspace-1
directory. - This will create a
go.work
file for you, which will have the go version it is using.
- In the
go.work
file add the followingHere I am using go 1.21.6 and I am giving a relative path to the go.mod files in all my modules that I want in this workspace.go 1.21.6 use ( ./mod1 ./mod2 ./mod3 )
- The End.
Now you can make changes in mod1 and those changes will be reflected in other modules part of this workspace, in this case mod2 and mod3.
As you progress and add new modules, go will also create a go.work.sum
file similar to go.sum
it will be auto-generated, so we don’t have to touch it.
Conclusion
I am using workspaces extensively, allowing me to focus on writing code without modifying other unwanted files. It also allows me to use a single instance of an IDE where I can get a complete picture of all the changes I have made in different modules. Thus reducing resource consumption, switching multiple windows, and ease of tracking changes all across the project.
Hope you start using this tool to increase your productivity.
For a detailed article, do visit the official blog here
If you like my article or want to share some insight, feel free to reach out to me.