In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "Go1.18 multi-module Multi-Module workspace model is what", detailed content, clear steps, details handled properly, I hope that this "Go1.18 multi-module Multi-Module workspace model is what" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge bar.
Background
Using multiple modules in go can be a real chore. Especially if 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 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 that you use the replace instruction in the dependency module to deal with this problem.
This method is effective, but it also has its own problems, such as the need to manually edit the go.mod, make sure you do not commit the replace when you submit the code, and so on.
Finally, starting with Go 1.18, a new method of dealing with multiple modules at the same time is introduced, which eliminates these problems: go.work.
Multi-Module, Single Workspace
Example: unreleased module
When doing local Go project development, multiple libraries (project libraries, tool libraries, third-party libraries) may be developed locally.
The code is as follows:
Package main import ("github.com/eddycjy/pkgutil") func main () {pkgutil.PrintFish ()}
We see that the only external dependency of this code is module with a module path of "github.com/eddycjy/pkgutil", but the latter is a module that is still under development locally and has not yet been released to http://github.com.
If you run go run or go mod tidy at this time, neither will work, and it will fail.
Similar errors are reported as follows:
Fatal: repository 'https://github.com/eddycjy/pkgutil/' not found
The reason for this problem is that the library github.com/eddycjy/pkgutil is not available in GitHub, so it cannot be pulled.
As a result, many students will question the soul: do Go dependencies have to be uploaded to GitHub, strong binding?
Solution: before Go1.18, we would use replace, or upload directly to Github, and naturally be able to pull dependencies by the Go tool chain.
Use the replace indicator to point the version to the local module development directory.
New feature of Go1.18: multi-module (Multi-Module) workspace mode
With the official release of go 1.18 on March 15, 2022, the new version introduces a number of new features in addition to performance improvements, including go's long-awaited functional generics (Generics), as well as multi-module workspaces (Workspaces) and fuzzy testing (Fuzzing).
It makes up for some shortcomings of the current go module construction mode, and can be called the last piece of the puzzle of the go module construction pattern.
The Go multi-module workspace makes it easier for developers to work on multiple modules at the same time, such as:
Convenient for debugging dependent code (breakpoints, modifying code), troubleshooting dependent code bug
Facilitate 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 deal with multiple modules at the same time. Before Go 1.17, you could only use the go.mod replace instruction, which can be painful if you happen to be developing multiple modules at the same time. Every time you want to submit code, you have to delete the replace in go.mod to make the module a stable release.
Go1.18 workspace mode
Under many rounds of feedback from the community, Michael Matloob put forward the proposal "Proposal: Multi-Module Workspaces in cmd/go [1]" for a lot of discussion and implementation, and officially landed in Go1.18.
A core concept of the new proposal is the addition of the concept of a go work workspace, which is aimed at Go Module's dependency management model.
This proposal introduces a go.work file to turn on Go workspace mode. Go.work sets some local paths through the directory indicator, and the go module under these paths forms a workspace, and the Go command can manipulate the go module under these paths and give priority to the go module in the workspace.
It can be in the go.work file of the local project, by setting a series of dependent module local paths, and then the modules under the path into a current Go project workspace, that is, N Go Module constitute a Go Work, the workspace read priority is the highest.
Summary: when you have a lot of module locally, and these module are interdependent, then we can set up a Go workspace outside these module, based on this Go workspace, it becomes very convenient to develop and debug these module.
Initialize a new workspace
You can initialize a new workspace simply by executing go work init, followed by an argument to 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 │ ├── go.mod / / Sub-module │ └── main.go ├── go.work / / Workspace └── tools ├── fish.go └── go.mod / / Sub-module go work support commands
In general, it is recommended that you do not submit a go.work file to git because it is mainly used for native code development.
It is recommended to execute under: $GOPATH path to generate go.work file
Go work init initializes the workspace file, which is used to generate go.work workspace file
Initialize and write a new go.work to the current path. You can specify the code modules to be added.
Example: go work init. / hello adds the local warehouse hello to the workspace
The hello warehouse must be a go mod dependent management warehouse (. / hello/go.mod file must exist)
Go work use adds a new module to the workspace
Use specifies the module directory to be used
Example of the command:
Go work use. / example add a module to the workspace
Example of the command:
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 recursive. / example directory to the current workspace delete command using the go work edit-dropuse=./example function
You can use go work use hello to add modules, or you can manually modify the go.work workspace to add new modules
A module path has been added to the workspace, and the code will be compiled automatically with native code in use, similar to replaces.
# single module structure use. / hello# multi-module structure use (. / hello. / example) go work edit is used to edit go.work files
Go work edit is used to edit go.work files
You can use the edit command to edit a go.work file and manually edit it with the same effect.
Example:
Go work edit-fmt go.work reformat go.work file go work edit-replace=github.com/link1st/example=./example go.work replacement code module go work edit-dropreplace=github.com/link1st/example delete replacement code module go work edit-use=./example go.work add a new module to the workspace go work edit-dropuse=./example go.work delete module go work sync synchronize the build list of the workspace to the module of 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 go.work path cannot be found, you can specify it using GOWORK.
Go.work file structure
The file structure is similar to the go.mod file structure, supporting Go version numbers, designated workspaces, and repositories to be replaced
Example of file structure:
Go 1.18use (. / hello. / example) replace (github.com/link1st/example = >. / example1)
Replaces replaces dependent warehouse address
The replaces command is the same as the go.mod directive to replace the warehouse address that is dependent on in the project
Note that replaces and use cannot specify the same local path at the same time
Error exampl
Specify the same local path in both use and replace
Go 1.18use (. / hello. / example) replace (github.com/link1st/example = >. / example) go.work files have higher priority than those defined in go.mod
Specify different code repository paths when using go.work and go.mod replace functions at the same time, and go.work priority is higher than that defined in go.mod.
How to disable a workspace
The Go global variable GOWORK setting off can disable the workspace feature
Export GOWORK=off read here, this "Go1.18 multi-module Multi-Module workspace model is what" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.