go work notes (multi-module workspace)

1. go work notes (multi-module workspace)

1.1. What is go work?

go 1.18 introduces functional generics (Generics), as well as multi-module workspaces (Workspaces) and fuzzing (Fuzzing).

The Go multi-module workspace makes it easier for developers to work on multiple modules at the same time, such as:

  • Convenient for dependent code debugging (break points, code modifications) and troubleshooting dependent code bugs
  • Facilitates parallel development and debugging of multiple warehouses/modules at the same time

1.2. go work supports commands

  • Normally, it is recommended not to submit go.workfiles to git as it is mainly used for local code development;
  • $GOPATHIt is recommended to execute under the path : to generate go.workfiles
  • go work initInitialize workspace file, used to generate go.workworkspace file

Initialize and write a new go.workto the current path, you can specify the code module that needs to be added.
Example: go work init ./helloAdd the local warehouse hello to the workspace
hello warehouse must be a go mod dependency management warehouse ( ./hello/go.modthe file must exist)

  • go work use adds new modules to the workspace

Command example:

go work use ./example 添加一个模块到工作区
go work use ./example ./example1 添加多个模块到工作区
go work use -r ./example 递归 ./example 目录到当前工作区
删除命令使用 go work edit -dropuse=./example 功能
  • go work editfor editing go.workfiles

You can use editthe command to edit the file and manually edit go.workthe file. The effect is the same, example:

go work edit -fmt go.work 重新格式化 go.work 文件
go work edit -replace=github.com/link1st/example=./example go.work 替换代码模块
go work edit -dropreplace=github.com/link1st/example 删除替换代码模块
go work edit -use=./example go.work 添加新的模块到工作区
go work edit -dropuse=./example go.work 从工作区中删除模块
  • go work syncSynchronize a workspace's build list to the workspace's modules

  • go env GOWORK

Check the environment variables and check the current workspace file path to check whether the workspace file is set correctly. If go.workthe path is not found, you can use GOWORKspecify

> go env GOWORK
$GOPATH/src/link1st/link1st/workspaces/go.work

1.3. go.work file structure

  • The file structure is similar to the go.mod file structure. It supports Go version number, specified workspace and warehouse that needs to be replaced. File structure example:
go 1.18

use (
    ./hello
    ./example
)

replace (
    github.com/link1st/example => ./example1
)

1.4. use specifies the module directory to use

  • You can use go work use hello to add modules, or you can manually modify the go.work workspace to add new modules. The module path is added to the workspace. When compiling, the local code in use will be automatically used for code compilation, and the replaces function similar.
# 单模块结构
use ./hello

# 多模块结构
use (
    ./hello
    ./example
)

1.5. replaces replaces the dependent warehouse address

  • The replaces command is the same as the go.mod command and is used to replace the warehouse address that the project depends on. It should be noted that replaces and use cannot specify the same local path at the same time.

同时指定报错信息:
go: workspace module http://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.

  • Error example

Specify the same local path in use and replace at the same time

go 1.18

use (
    ./hello
    ./example
)

replace (
    github.com/link1st/example => ./example
)

1.6. The priority of go.work file is higher than that defined in go.mod

  • When using go.work and go.mod replace functions at the same time, specify different code warehouse paths respectively. The priority of go.work is higher than that defined in go.mod.

Replace the definition in go.mod with the local warehouse example

replace (
    github.com/link1st/example => ./example1
)

Replace the definition in go.work with the local warehouse example1

replace (
    github.com/link1st/example => ./example1
)
  • When building the code, the code of the example1 warehouse specified by go.work is used, and go.work has a higher priority.

1.7. How to use

  • In Go 1.18, go run and go build will use the workspace function by default. GOWORK can also specify the location of the go.work file.
export GOWORK="~/go/src/test/go.18/workspace/go.work"

1.8. How to disable a workspace

  • Go global variable GOWORK is set to off to disable the workspace function.

export GOWORK=off

1.9. Development process demonstration

  • Demonstrates how to use the multi-module workspace feature. In today's era when microservices are prevalent, one person will maintain multiple code warehouses. In many cases, multiple warehouses are developed simultaneously.

  • Assume that we are now developing the hello warehouse. The function implemented is to reverse the input string and output it. The string reversal function relies on the public warehouse implementation of http://github.com/link1st/example (hereinafter collectively referred to as example)

  • Create a new hello project

mkdir hello
cd hello
# 代码仓库启动 go mod 依赖管理, 生成 go.mod 文件
go mod init github.com/link1st/link1st/workspaces/hello
# 下载依赖包
go get github.com/link1st/example
# 编写 main 文件
vim main.go
  • main.go code
// Package main main 文件, go 多模块工作区演示代码
// 实现将输入的字符串反转输出并输出
package main

import (
    "flag"
    "fmt"

    "github.com/link1st/example/stringutil"
)

var (
    str = ""
)

func init() {
    
    
    flag.StringVar(&str, "str", str, "输入字符")
    flag.Parse()
}

func main() {
    
    
    if str == "" {
    
    
        fmt.Println("示例: go run main.go -str hello")
        fmt.Println("str 参数必填")
        flag.Usage()
        return
    }

    // 调用公共仓库, 进行字符串反转
    str = stringutil.Reversal(str)
    // 输出反转后的字符串
    fmt.Println(str)
    return
}
  • Run the code go run main.go -str “hello world” or go run github.com/link1st/link1st/workspaces/hello -str “hello world”. You can see that the reversed string of hello world is output.
> go run main.go -str "hello world"
dlrow olleh
  • At this point, the initial function has been completed, but subsequent changes in requirements require not only outputting the reversed string, but also capitalizing the string. We then need to add and develop the function of capitalizing the string in the example warehouse.
# 回到工作根目录, 将 common 代码下载到本地进行添加新的功能
# 下载依赖的 example 包
git clone [email protected]:link1st/example.git
# 在 example 包中添加 字符串大学的功能
  • vim example/stringutil/to_upper.go code is as follows
// Package stringutil stringutil
package stringutil

import (
    "unicode"
)

// ToUpper 将字符串进行大写
func ToUpper(s string) string {
    
    
    r := []rune(s)
    for i := range r {
    
    
        r[i] = unicode.ToUpper(r[i])
    }
    return string(r)
}
  • Since the code is still being debugged locally and has not been submitted to the git repository, you need to use the Go multi-module workspace function at this time.

  • Enter the project root directory and initialize the module we are developing now

# 初始化 go.work 文件
go work init  ./hello ./example
# 查看 go.work 文件内容
cat go.work
  • The file structure is as follows
go 1.18

use (
    ./example
    ./hello
)
  • Back to the hello project, vim main.go adds the function of capitalizing strings.
func main() {
    
    
    ...

    // 调用公共仓库, 进行字符串反转
    str = stringutil.Reversal(str)
    // 增加字符大写的功能
    str = stringutil.ToUpper(str)
    // 输出反转后的字符串
    fmt.Println(str)

    ...
}
  • run code

You can see that the reversed and uppercase string is output. The uppercase function is only local and not submitted to git. In this way, we can develop two modules in parallel at the same time.

go run main.go -str "hello world"
DLROW OLLEH
  • At this point, the demo code has been completed

1.10. Summary

  • Using the function of Go's multi-module workspace allows us to easily switch work between multiple modules and is more adaptable to the development of modern microservice architecture.

Guess you like

Origin blog.csdn.net/wan212000/article/details/132414236