takeaway
-
With the release of Go 1.18 on March 15, 2020, in addition to performance improvements, the new version also introduces a number of new features, including the long-awaited go feature Generics, Multi-module Workspaces and Fuzzing were also introduced.
-
There are already many tutorials on generics online, but here I present a useful feature, the use of a multi-module workspace, and tutorials.
-
The Go multi-module workspace makes it easier for developers to work on multiple modules at once, such as:
It is convenient to debug dependent codes (break points, modify codes) and check dependent code bugs. It is convenient to develop and debug multiple warehouses/modules at the same time
directory
- Multi-module workspace
- Development process Demonstration
- conclusion
- reference
Multi-module workspace
instructions
- Go uses a multi-module workspace, making it easier for developers to work on multiple modules at the same time. Available only before Go 1.17
go.mod replace
If you happen to be developing multiple modules at the same time, it can be a pain to use. Every time you want to commit code, you have to delete itgo.modIn thereplace
To make a stable release of the module. - Using this work becomes simple and manageable when using the Go 1.18 multi-module Workspace feature. Here’s how to use this feature.
- Go Multi-module workspace documentation, code examples to get addresses Github.com/link1st/lin…
Conditions of use
- First we need a Go 1.18 or later GO installation
#Viewing the GO Version
> go versionGo version go1.18 Darwin/amd64Copy the code
go work
Support command
-
In general, it is recommended not to commit the go.work file to Git, as it is primarily used for native code development.
-
You are advised to run the command in the $GOPATH directory to generate the go.work file
-
Go work init Initializes the workspace file to generate the go.work workspace file
Initialize and write a new go.work to the current path. You can specify examples of code modules to add: Go work init./hello Adds local repository Hello to workspace. The repository must be the repository for go Mod dependency management (the./hello/go.mod file must exist).
go work use
Add a new module to the workspace
Command example: /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 Delete the directory to the current workspace using the go work edit -dropuse=./example function
go work edit
To editgo.workfile
An example of how a go.work file can be edited using the edit command and edited manually is the same: Go work edit – FMT. Work to reformat the go. Go work file work edit – replace=github.com/link1st/example=./example go. Go work to replace the code module Work edit – dropreplace=github.com/link1st/example delete replace code module go work edit – use =. / example. Work a new module is added to the workspace go work edit -dropuse=./example go.work Deletes modules from the workspace
-
Go Work Sync synchronizes the workspace build list to the workspace module
-
go env GOWORK
Check the environment variables and the current workspace file path to check whether the workspace file is set correctly. If the go.work path cannot be found, use GOWORK to specify it
> go env GOWORK
$GOPATH/src/link1st/link1st/workspaces/go.work
Copy the code
go.workFile structure
- The file structure is similar to the go.mod file structure, supporting go version numbers, specifying workspaces, and repositories to replace
- Example file structure:
1.18 use the go (. / hello. / example) replace (github.com/link1st/example = >. / example1)Copy the code
use
Specifies the module directory to use
- You can use
go work use hello
Add a module or modify it manuallygo.workAdd new modules to workspace - Added a module path to the workspace, which is used automatically at compile timeuseNative code for code compilation, and
replaces
Functions are similar.
Use (./hello./example)Copy the code
replaces
Replace the dependent warehouse address
replaces
Command andgo.modThe same directive replaces the repository address that is dependent on in the project- One thing to note
replaces
和use
You cannot specify the same local path at the same time
Error message: go: workspace module github.com/link1st/example is replaced at all versions in the go.work file. To fix, remove the replacement from the go.work file or specify the version at which to replace the module.
- The wrong sample
Specify the same local path for both use and replace
1.18 use the go (. / hello. / example) replace (github.com/link1st/example = >. / example)Copy the code
go.workThe file priority is higher thango.modDefined in the
- At the same timego.work 和 go.mod
replace
Function to specify a different repository path,go.workPriority overgo.modDefined in the
Replace go.mod with the local repository example
replace (
github.com/link1st/example => ./example1
)
Copy the code
Replace with local repository example1 defined in go.work
replace (
github.com/link1st/example => ./example1
)
Copy the code
- The code for example1 repository specified by go.work is used when the code is built, and go.work has a higher priority
How to use
- In the 1.18
go run
和go build
The workspace function is used by default GOWORK
You can also specify configurationsgo.workFile location
export GOWORK="~/go/src/test/go.18/workspace/go.work"
Copy the code
How to disable workspaces
- Go global variable
GOWORK
Set up theoff
You can disable the workspace feature
export GOWORK=off
Development process Demonstration
-
Demonstrates how to use the multi-module workspace feature. In today’s microservices era, one person can maintain multiple repositories, many of which are developed simultaneously
-
Suppose we now hello warehouse development, the realization of function is, realizing the input string reversal and output, string reversal function depends on the github.com/link1st/example (hereinafter generally referred to as the example) public warehouse
-
New Hello project
mkdir hello
cd hello
#The repository starts the Go Mod dependency management and generates the go.mod file
go mod init github.com/link1st/link1st/workspaces/hello
#Downloading dependency packages
go get github.com/link1st/example
#Write a main file
vim main.go
Copy the code
- The main. The go code
// Package main main file, go multi-module workspace demo code
// The implementation reverses the input string and prints it
package main
import (
"flag"
"fmt"
"github.com/link1st/example/stringutil"
)
var (
str = ""
)
func init(a) {
flag.StringVar(&str, "str", str, "Input character")
flag.Parse()
}
func main(a) {
if str == "" {
fmt.Println("Example: go run main.go -str hello")
fmt.Println("STR parameter mandatory")
flag.Usage()
return
}
// Call the public repository for string inversion
str = stringutil.Reversal(str)
// Outputs the inverted string
fmt.Println(str)
return
}
Copy the code
- Run the code
go run main.go -str "hello world"
或go run github.com/link1st/link1st/workspaces/hello -str "hello world"
You can see the outputhello worldInvert the string
> go run main.go -str "hello world"
dlrow olleh
Copy the code
- At this point, the initial functionality is complete, but subsequent requirements change to not only output the inverted string, but also capitalize the string
- We need to go to the Example repository and add the ability to capitalize strings
# return to work root, Common code to add new functionality downloaded to a local # download rely on the example of package git clone [email protected]: link1st/example. The git # in example string is added to the package, the function of the universityCopy the code
- The vim example/stringutil/to_upper. Go code is as follows
// Package stringutil stringutil
package stringutil
import (
"unicode"
)
// ToUpper capitalizes the string
func ToUpper(s string) string {
r := []rune(s)
for i := range r {
r[i] = unicode.ToUpper(r[i])
}
return string(r)
}
Copy the code
-
Because the code is still being debugged locally and not committed to the Git repository, this is where the Go multi-module workspace comes in.
-
Go to the project root directory and initialize the module we are currently developing
/ hello. /example # check the go.work fileCopy the code
- The file structure is as follows
Go 1.18 use (./example./hello)Copy the code
- Go back tohelloThe project,
vim main.go
Add the ability to capitalize strings.
func main(a){...// Call the public repository for string inversion
str = stringutil.Reversal(str)
// Add character uppercase function
str = stringutil.ToUpper(str)
// Outputs the inverted string
fmt.Println(str)
...
}
Copy the code
- Run the code
You can see that the output is reversed and capitalized. The capitalized function is local and not committed to Git, so we can develop on both modules in parallel
go run main.go -str "hello world"
DLROW OLLEH
Copy the code
- At this point, the demo code is complete
conclusion
- Using the Go multi-module workspace feature allows us to easily switch work between modules, making it more adaptable to modern microservices architecture development.
reference
Go 1.18 new feature Multi-module Workspace tutorial
Go 1.18 is released!
Tutorial: Getting started with multi-module workspaces
Go – 1.18 – the features