go modules 多个主要方法
go modules multiple main methods
我有一个包含多个主要方法的项目。
当 运行 go build program1/main1.go
具有与 program2/main2.go
不同的一组依赖项时,我的第一个 go build
似乎更改了我的 go.mod
文件并删除了它的依赖项 认为不需要。然而 main2
需要这些依赖项。
我试过使用 go build ...
但这也创建了一组不同的依赖项。具体来说,似乎所有 //indirect
依赖项都被删除并导致 program2 失败。
有没有办法在不更新go.mod
文件的情况下运行go build
或go run
?使用 go build -mod=readonly program1/main1.go
它告诉我它失败了,因为需要更新依赖项..
我相信您正在寻找 子模块。参见 this walktrhough。
TLDR:您需要在每个工具的 cmd
目录中有一个单独的 go.mod
,并且您可以使用 replace
指令将这些工具的依赖项指向您的本地模块。
This Go Issue 和其他从它链接的人建议弄清楚 "the one right way" 来做到这一点仍然是 WIP,尽管我认为你的用例很简单。
使用子模块是嵌套多个 Go 模块项目的一种方式,您可以编辑。
但是 Go 1.18 可能包含 Go 工作区 的概念,这意味着您不再需要子模块:一个 Go 项目可以包含多个您可以编辑的模块。
见golang/go
issue 45713: "proposal: cmd/go
: add a workspace mode " and its design document。
Background
Users often want to make changes across multiple modules: for instance, to introduce a new interface in a package in one module along with a usage of that interface in another module.
Normally, the go
command recognizes a single "main
" module the user can edit.
Other modules are read-only and are loaded from the module cache.
The go mod replace
directive is the exception: it allows users to replace the resolved version of a module with a working version on disk.
But working with the replace
directive can often be awkward: each module developer might have working versions at different location on disk, so having the directive in a file that needs to be distributed with the module isn't a good fit for all use cases.
Proposal
This proposal describes a new workspace mode in the go
command for editing multiple modules.
The presence of a go.work
file in the working directory or a containing directory will put the go
command into workspace mode.
The go.work
file specifies a set of local modules that comprise a workspace. When invoked in workspace mode, the go
command will always select these modules and a consistent set of dependencies.
Main modules: The module the user is working in.
Before this proposal, this is the single module containing the directory where the go
command is invoked. This module is used as the starting point when running MVS.
This proposal proposes allowing multiple main modules.
参见 CL 334934(CL = 更改列表)
[dev.cmdgo
] cmd/go
: add the workspace mode
This change adds the outline of the implementation of the workspace mode.
The go
command will now locate go.work
files, and read them to determine
which modules are in the workspace.
It will then put those modules in the root of the workspace when building the build list.
It supports building, running, testing, and listing in workspaces.
您可以使用 go mod initwork
启动多模块项目
同样,这不是在 Go 1.18(2022 年第一季度)之前,并且可能会在 Go 1.19(2022 年第三季度)中选择加入。
我有一个包含多个主要方法的项目。
当 运行 go build program1/main1.go
具有与 program2/main2.go
不同的一组依赖项时,我的第一个 go build
似乎更改了我的 go.mod
文件并删除了它的依赖项 认为不需要。然而 main2
需要这些依赖项。
我试过使用 go build ...
但这也创建了一组不同的依赖项。具体来说,似乎所有 //indirect
依赖项都被删除并导致 program2 失败。
有没有办法在不更新go.mod
文件的情况下运行go build
或go run
?使用 go build -mod=readonly program1/main1.go
它告诉我它失败了,因为需要更新依赖项..
我相信您正在寻找 子模块。参见 this walktrhough。
TLDR:您需要在每个工具的 cmd
目录中有一个单独的 go.mod
,并且您可以使用 replace
指令将这些工具的依赖项指向您的本地模块。
This Go Issue 和其他从它链接的人建议弄清楚 "the one right way" 来做到这一点仍然是 WIP,尽管我认为你的用例很简单。
使用子模块是嵌套多个 Go 模块项目的一种方式,您可以编辑。
但是 Go 1.18 可能包含 Go 工作区 的概念,这意味着您不再需要子模块:一个 Go 项目可以包含多个您可以编辑的模块。
见golang/go
issue 45713: "proposal: cmd/go
: add a workspace mode " and its design document。
Background
Users often want to make changes across multiple modules: for instance, to introduce a new interface in a package in one module along with a usage of that interface in another module.
Normally, the
go
command recognizes a single "main
" module the user can edit.
Other modules are read-only and are loaded from the module cache.The
go mod replace
directive is the exception: it allows users to replace the resolved version of a module with a working version on disk.
But working with thereplace
directive can often be awkward: each module developer might have working versions at different location on disk, so having the directive in a file that needs to be distributed with the module isn't a good fit for all use cases.Proposal
This proposal describes a new workspace mode in the
go
command for editing multiple modules.The presence of a
go.work
file in the working directory or a containing directory will put thego
command into workspace mode.
Thego.work
file specifies a set of local modules that comprise a workspace. When invoked in workspace mode, thego
command will always select these modules and a consistent set of dependencies.Main modules: The module the user is working in.
Before this proposal, this is the single module containing the directory where thego
command is invoked. This module is used as the starting point when running MVS.
This proposal proposes allowing multiple main modules.
参见 CL 334934(CL = 更改列表)
[
dev.cmdgo
]cmd/go
: add the workspace modeThis change adds the outline of the implementation of the workspace mode.
The
go
command will now locatego.work
files, and read them to determine which modules are in the workspace.
It will then put those modules in the root of the workspace when building the build list.
It supports building, running, testing, and listing in workspaces.
您可以使用 go mod initwork
同样,这不是在 Go 1.18(2022 年第一季度)之前,并且可能会在 Go 1.19(2022 年第三季度)中选择加入。