How to use golang workspaces(go.work)? And What problem does it solve?
Need Go > 1.18
Take my own projects as example:
go-mygit is a command line tool using
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:
go-mygitand split them later.
- Keep updating them separately, keep doing
go get -ufor every single change.
Both methods work, but very cumbersome.
This new Go feature changed all of that. With workspaces setup,
go build and other Go tools will use
go-helper source tree when dealing with
With directory layout:
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-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!!!