Go Workspaces
How to use golang workspaces(go.work)? And What problem does it solve?
Need Go > 1.18
The Issue
Take my own projects as example:
go-mygit is a command line tool using go-gitapi and go-helper as libraries. When I was developing go-mygit, I was constantly changing go-gitapi and expanding go-helper. In the past, I had two choices:
- Move
go-gitapiandgo-helperintogo-mygitand split them later. - Keep updating them separately, keep doing
gittag,pushandgo get -ufor every single change.
Both methods work, but very cumbersome.
How To
This new Go feature changed all of that. With workspaces setup, go build and other Go tools will use go-gitapi and go-helper source tree when dealing with go-mygit.
With directory layout:
| |
Do following:
| |
This creates a new fille, go.work. To add current directory:
| |
Then add library:
| |
From now on, doing go build inside go-mygit folder will compile with code from ../go-gitapi and ../go-helper, not package cache.
We no longer have to tag/push libraries before testing with the main project. And more importantly, VS Code Go extension is doing syntax checking accordingly!!!