background
Using multiple modules in go can be a real chore. Especially when one of your modules depends on another, you need to edit both modules at the same time!
You edit the parent module, but then you need to push it to the repo. Then run update in the dependency module to download the new version. Finally use 2 lines to fix what you need. It's a pain to say the least.
Prior to Go 1.18, it was recommended to use the replace directive in the dependent module to deal with this issue.
This method is effective, but it also has its own problems, such as manual editing, ensuring that when you submit the code, you do not commit the replacement, etc.
Finally, starting with Go 1.18, a new approach to dealing with multiple modules simultaneously has been introduced, which eliminates these problems: .
Multi-Module, Single Workspace
/@michael_epps/multi-module-single-workspace-3493528555ad
Example: Unreleased module
When developing a local Go project, multiple libraries (project library, tool library, third-party library) may be developed locally at the same time.
The following code:
package main import ( "/eddycjy/pkgutil" ) func main() { () }
We see that the only dependence of this code on the outside is the module whose module path is "/eddycjy/pkgutil", but the latter is a module that is still being developed locally and has not been published on it.
If you run go run or go mod tidy at this time, it will fail.
A similar error is reported:
fatal: repository '/eddycjy/pkgutil/' not found
This problem is reported because the /eddycjy/pkgutil library is not available in GitHub, so it is naturally impossible to pull.
Therefore, many students will question soulfully: Do Go dependencies must be uploaded to GitHub, and are strongly bound?
Solution: Before Go1.18, we would replace or upload it directly to Github, and we would naturally be able to get dependencies by the Go toolchain.
Use the replace indicator to point this version to the development directory of the local module.
Go1.18 new feature: Multi-Module (Multi-Module) workspace mode
Go 1.18 was officially released on March 15, 2022. In addition to improving performance, the new version has also introduced many new features, including the long-awaited functional generics (Generics), as well as the multi-module workspaces and fuzzing.
It makes up for some shortcomings in the current go module construction mode and can be regarded as the last piece of the go module construction mode.
The Go multi-module workspace enables developers to more easily handle the work of multiple modules at the same time, such as:
- Convenient to debug dependency code (breaking points, modifying code), and troubleshooting dependency code bugs
- Convenient to carry out parallel development and debugging of multiple warehouses/modules at the same time
go uses a multi-module workspace, which makes it easier for developers to handle the development of multiple modules at the same time. Before Go 1.17, it could only be implemented using the replace directive. If you happen to be developing multiple modules at the same time, it can be painful to use it. Every time you want to submit code, you have to delete the replace in order to make the module stable release version.
Go1.18 Workspace Mode
Under multiple rounds of feedback from the community, Michael Matloob put forward the proposal "Proposal: Multi-Module Workspaces in cmd/go[1]" and conducted a lot of discussion and implementation, and it was officially implemented in Go1.18.
A core concept of the new proposal is to add the concept of go work workspace, which targets Go Module's dependency management model.
This proposal introduces a file to enable Go workspace mode.Some local paths are set through the directory indicator, and the go module under these paths forms a workspace (workspace)The Go command can operate the go module under these paths, and it will alsoPriority to using go module in the workspace。
It can set a series of local paths of dependent modules in the local project file, and then form the modules under the path to a workspace of the current Go project, that is,N Go Modules make up 1 Go Work, and the workspace has the highest reading priority.
Summary: When you have many modules locally and these modules are interdependent, we can establish a Go workspace outside these modules, and it becomes very convenient to develop and debug these modules based on this Go workspace.
Initialize a new workspace
Just execute go work init to initialize a new workspace, followed by the parameters are the specific submodule mod to be generated.
The command is as follows:
go work init ./mod ./tools
The project directory is as follows:
awesomeProject ├── mod │ ├── // Submodule│ └── ├── // Workspace└── tools ├── └── // Submodules
go work supports commands
- Normally, it is recommended not to submit files to git, as it is mainly used for local code development.
- Recommended to execute under: $GOPATH path to generate a file
- go work init Initializes the workspace file to generate the workspace file
Initialize and write a new one to the current path, you can specify the code module you need to add.
Example: go work init ./hello Add local repository hello to the workspace
The hello repository must be a go mod repository (the ./hello/ file must exist)
go work use Add new module to the workspace
use Specify the module directory to use
Command Example:
go work use ./example Add a module to the workspace
Command Example:
go work use ./example Add a module to the workspace go work use ./example ./example1 Add multiple modules to the workspace go work use -r ./example recursion ./example Directory to the current workspace Use the delete command go work edit -dropuse=./example Function
You can use go work use hello to add modules, or you can manually modify the workspace to add new modules
Module paths are added to the workspace. When compiling, the local code in use will be automatically compiled, which is similar to the replacement function.
# Single module structureuse ./hello # Multi-module structureuse ( ./hello ./example )
go work edit for editing files
go work edit
Used to edit files
You can use the edit command to edit and manually edit the file effect is the same
Example:
go work edit -fmt Reformatting document go work edit -replace=/link1st/example=./example Replace code module go work edit -dropreplace=/link1st/example 删除Replace code module go work edit -use=./example Add new modules to the workspace go work edit -dropuse=./example Delete modules from workspace
go work sync modules for synchronizing the build list of the workspace to the workspace
go env GOWORK
View environment variables and view the current workspace file path
You can check whether the workspace file is set correctly. If the path cannot be found, you can use GOWORK to specify it.
File structure
The file structure is similar to that of the file structure, and supports Go version number, specified workspace, and repositories that need to be replaced.
File structure example:
go 1.18 use ( ./hello ./example ) replace ( /link1st/example => ./example1 )
replaces Replaces Resident dependent repository address
The replaces command is the same as the directive and is used to replace the repository address that is dependent in the project.
It should be noted that replaces and use cannot specify the same local path at the same time.
Error Example
Specify the same local path in both use and replace
go 1.18 use ( ./hello ./example ) replace ( /link1st/example => ./example )
The file priority is higher than the definition in
When using the function and replace, specify different code repository paths respectively, with the priority higher than that defined in
How to disable the workspace
Go global variable GOWORK setting off can disable the workspace function
export GOWORK=off
This is the article about the multi-module multi-Module workspace mode, which is the new feature of Go1.18. For more related content on Go multi-module workspace mode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!