GO-0014

原文

Tutorial: Getting started with multi-module workspaces

Table of Contents

This tutorial introduces the basics of multi-module workspaces in Go. With multi-module workspaces, you can tell the Go command that you’re writing code in multiple modules at the same time and easily build and run code in those modules.

In this tutorial, you’ll create two modules in a shared multi-module workspace, make changes across those modules, and see the results of those changes in a build.

Note: For other tutorials, see Tutorials.

Prerequisites

  • An installation of Go 1.18 or later.
  • A tool to edit your code. Any text editor you have will work fine.
  • A command terminal. Go works well using any terminal on Linux and Mac, and on PowerShell or cmd in Windows.

This tutorial requires go1.18 or later. Make sure you’ve installed Go at Go 1.18 or later using the links at go.dev/dl.

Create a module for your code

To begin, create a module for the code you’ll write.

  1. Open a command prompt and change to your home directory.

    On Linux or Mac:

    $ cd
    

    On Windows:

    C:\> cd %HOMEPATH%
    

    The rest of the tutorial will show a $ as the prompt. The commands you use will work on Windows too.

  2. From the command prompt, create a directory for your code called workspace.

    $ mkdir workspace
    $ cd workspace
    
  3. Initialize the module

    Our example will create a new module hello that will depend on the golang.org/x/example module.

    Create the hello module:

    $ mkdir hello
    $ cd hello
    $ go mod init example.com/hello
    go: creating new go.mod: module example.com/hello
    

    Add a dependency on the golang.org/x/example/hello/reverse package by using go get.

    $ go get golang.org/x/example/hello/reverse
    

    Create hello.go in the hello directory with the following contents:

    package main
    
    import (
        "fmt"
    
        "golang.org/x/example/hello/reverse"
    )
    
    func main() {
        fmt.Println(reverse.String("Hello"))
    }
    

    Now, run the hello program:

    $ go run .
    olleH
    

Create the workspace

In this step, we’ll create a go.work file to specify a workspace with the module.

Initialize the workspace

In the workspace directory, run:

$ go work init ./hello

The go work init command tells go to create a go.work file for a workspace containing the modules in the ./hello directory.

The go command produces a go.work file that looks like this:

go 1.18

use ./hello

The go.work file has similar syntax to go.mod.

The go directive tells Go which version of Go the file should be interpreted with. It’s similar to the go directive in the go.mod file.

The use directive tells Go that the module in the hello directory should be main modules when doing a build.

So in any subdirectory of workspace the module will be active.

Run the program in the workspace directory

In the workspace directory, run:

$ go run ./hello
olleH

The Go command includes all the modules in the workspace as main modules. This allows us to refer to a package in the module, even outside the module. Running the go run command outside the module or the workspace would result in an error because the go command wouldn’t know which modules to use.

Next, we’ll add a local copy of the golang.org/x/example/hello module to the workspace. That module is stored in a subdirectory of the go.googlesource.com/example Git repository. We’ll then add a new function to the reverse package that we can use instead of String.

Download and modify the golang.org/x/example/hello module

In this step, we’ll download a copy of the Git repo containing the golang.org/x/example/hello module, add it to the workspace, and then add a new function to it that we will use from the hello program.

  1. Clone the repository

    From the workspace directory, run the git command to clone the repository:

    $ git clone https://go.googlesource.com/example
    Cloning into 'example'...
    remote: Total 165 (delta 27), reused 165 (delta 27)
    Receiving objects: 100% (165/165), 434.18 KiB | 1022.00 KiB/s, done.
    Resolving deltas: 100% (27/27), done.
    
  2. Add the module to the workspace

    The Git repo was just checked out into ./example. The source code for the golang.org/x/example/hello module is in ./example/hello. Add it to the workspace:

    $ go work use ./example/hello
    

    The go work use command adds a new module to the go.work file. It will now look like this:

    go 1.18
    
    use (
        ./hello
        ./example/hello
    )
    

    The workspace now includes both the example.com/hello module and the golang.org/x/example/hello module, which provides the golang.org/x/example/hello/reverse package.

    This will allow us to use the new code we will write in our copy of the reverse package instead of the version of the package in the module cache that we downloaded with the go get command.

  3. Add the new function.

    We’ll add a new function to reverse a number to the golang.org/x/example/hello/reverse package.

    Create a new file named int.go in the workspace/example/hello/reverse directory containing the following contents:

    package reverse
    
    import "strconv"
    
    // Int returns the decimal reversal of the integer i.
    func Int(i int) int {
        i, _ = strconv.Atoi(String(strconv.Itoa(i)))
        return i
    }
    
  4. Modify the hello program to use the function.

    Modify the contents of workspace/hello/hello.go to contain the following contents:

    package main
    
    import (
        "fmt"
    
        "golang.org/x/example/hello/reverse"
    )
    
    func main() {
        fmt.Println(reverse.String("Hello"), reverse.Int(24601))
    }
    

Run the code in the workspace

From the workspace directory, run

$ go run ./hello
olleH 10642

The Go command finds the example.com/hello module specified in the command line in the hello directory specified by the go.work file, and similarly resolves the golang.org/x/example/hello/reverse import using the go.work file.

go.work can be used instead of adding replace directives to work across multiple modules.

Since the two modules are in the same workspace it’s easy to make a change in one module and use it in another.

Future step

Now, to properly release these modules we’d need to make a release of the golang.org/x/example/hello module, for example at v0.1.0. This is usually done by tagging a commit on the module’s version control repository. See the module release workflow documentation for more details. Once the release is done, we can increase the requirement on the golang.org/x/example/hello module in hello/go.mod:

cd hello
go get golang.org/x/example/hello@v0.1.0

That way, the go command can properly resolve the modules outside the workspace.

Learn more about workspaces

The go command has a couple of subcommands for working with workspaces in addition to go work init which we saw earlier in the tutorial:

  • go work use [-r] [dir] adds a use directive to the go.work file for dir, if it exists, and removes the use directory if the argument directory doesn’t exist. The -r flag examines subdirectories of dir recursively.
  • go work edit edits the go.work file similarly to go mod edit
  • go work sync syncs dependencies from the workspace’s build list into each of the workspace modules.

See Workspaces in the Go Modules Reference for more detail on workspaces and go.work files.

以下是对您提供的英文技术文档《Tutorial: Getting started with multi-module workspaces》的完整处理,严格遵循您提出的三项要求:

  • 专业翻译(准确、流畅、符合中文技术文档规范)
  • 词汇表梳理(仅收录此前未出现的新词,共新增 13 个)
序号 单词 词性 音标 拼读 释义
1 Workspace 名词 /ˈwɜːrkspeɪs/ Work·space 沃-克-斯-佩-斯 工作区。Go 1.18 引入的概念,指通过 go.work 文件管理的、包含多个本地模块的开发环境,支持跨模块联合构建。
2 Simultaneously 副词 /ˌsaɪmlˈteɪniəsli/ Si·mul·ta·ne·ous·ly 赛-姆-特-奈-尼-厄-斯-利 同时地。指在同一时间段内并发进行多个操作或任务。
3 Shared 形容词 /ʃerd/ Shared 谢-德 共享的。指被多个实体共同使用或访问的资源、目录或环境。
4 Clone 动词 / 名词 /kloʊn/ Clone 克-隆 克隆。在版本控制中指完整复制远程仓库到本地的操作。
5 Recursively 副词 /rɪˈkɜːrsɪvli/ Re·cur·sive·ly 瑞-克-欧-斯-伊-夫-利 递归地。指函数或操作在其定义或执行过程中重复调用自身或深入子结构的方式。
6 Sync 动词 / 名词 /sɪŋk/ Sync 辛-克 同步。指使多个系统、文件或依赖项的状态保持一致的操作。
7 Activate 动词 /ˈæktɪveɪt/ Ac·ti·vate 阿-克-提-维-特 激活。指使某个模块或环境在当前上下文中生效。
8 Cache 名词 / 动词 /kæʃ/ Cache 凯-什 缓存。指 Go 工具链本地存储的已下载模块副本,用于加速后续构建。
9 Tell 动词 /tel/ Tell 特-欧 告诉,告知。在编程或工具链上下文中,指通过配置、命令或代码向系统明确传递指令、意图或元数据。
10 Rest 名词 /rest/ Rest 瑞-斯特 其余部分,剩余内容。在技术文档中常用于指代“当前未展示但后续将出现的文本、命令或步骤”。
11 Initialize 动词 /ɪˈnɪʃəlaɪz/ In·i·tial·ize 伊-尼-夏-莱-兹 初始化。指在程序、模块或系统启动时,为其设置初始状态、配置或资源;在 Go 中特指通过命令(如 go mod initgo work init)创建配置文件并设定基础元数据。
12 Reverse 动词 / 名词 / 形容词 /rɪˈvɜːrs/ Re·verse 瑞-沃-斯 作动词时,意为“反转,颠倒”,指将序列、字符串或数字的顺序完全倒置;作名词时,可指“反面”或“逆序结果”;作形容词时,意为“反向的”。在 Go 编程上下文中,特指对字符串、字节切片或整数进行顺序颠倒的操作。
13 Refer 动词 /rɪˈfɜːr/ Re·fer 瑞-弗-欧 引用,提及。在编程语境中,特指通过导入路径、变量名或包名在代码中使用另一个实体(如函数、类型或模块);也用于描述工具(如 Go 命令)如何定位和关联代码单元。
14 Examines 动词(第三人称单数形式) /ɪɡˈzæmɪnz/ Ex·am·ines 伊-格-扎-米-尼-兹 检查,审查。在编程工具上下文中,指命令或程序对文件、目录或代码结构进行系统性扫描与分析,以提取信息或验证状态。
posted @ 2026-01-27 07:43  红尘过客2022  阅读(40)  评论(0)    收藏  举报