From 93395e1aae29c9c6afba67a05895cf7db9fae918 Mon Sep 17 00:00:00 2001 From: asahi <496063163@qq.com> Date: Mon, 6 Jan 2025 00:40:12 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=85=E8=AF=BBgolang=20multi=20modules?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Golang/Golang Document.md | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/Golang/Golang Document.md b/Golang/Golang Document.md index a890adf..5538304 100644 --- a/Golang/Golang Document.md +++ b/Golang/Golang Document.md @@ -11,4 +11,39 @@ 在实际开发中,module name通常是source code被保存的repository location,例如,`uuid`module的module name为`github.com/google/uuid`。 #### go mod tidy -`go mod tidy`命令会根据import添加缺失的module并且移除未使用的module。 \ No newline at end of file +`go mod tidy`命令会根据import添加缺失的module并且移除未使用的module。 + +## multi-module workspace +示例目录结构如下所示: +- workspace +- workspace/hello +- workspace/example/hello + +### go work init +在本示例中,为了创建多module的workspace,可以执行`go work init ./hello`,其会创建`go.work`文件,并将`./hello`目录下的module包含到`go.work`文件中。 + +`go.work`内容如下: +``` +go 1.18 + +use ./hello +``` + + +### go work use +通过`go work use ./example/hello`命令,会将`./example/hello`中的module加入到`go.work`文件中。 + +`go.work`内容如下: +``` +go 1.18 + +use ( + ./hello + ./example/hello +) +``` + +`go work use [-r] [dir]`命令行为如下: +- 如果指定目录存在,会为`dir`向`go.work`文件中添加一条use指令 +- 如果指定目录不存在,会删除`go.work`文件中关于目录的use指令 +