How can I import a workspace as a dependency in another go repository?

huangapple go评论78阅读模式
英文:

How can I import a workspace as a dependency in another go repository?

问题

我有一个使用多个工作区的Go项目,其结构如下:

apps/go.mod
go.work

该项目包括一个名为apps的子模块,其go.work文件内容如下:

go 1.180
use (
./apps
)

我有另一个独立的Go项目(单独的代码库),想要将这个apps子模块作为库导入,下面是go.mod文件的内容:

module xxxx

go 1.18

require (
	github.com/zhaoyi0113/test-go-module v0.0.3 // indirect
)

然后我有以下代码:

package main

import (
	"github.com/zhaoyi0113/test-go-module/apps/logger"
)

func main() {
	logger.Test()
}

编译时出现以下错误:

main.go:7:2: no required module provides package github.com/zhaoyi0113/test-go-module/apps/logger; to add it:
        go get github.com/zhaoyi0113/test-go-module/apps/logger

如何从多个工作区项目中导入子模块呢?

英文:

I have a go multiple workspace project whose structure is:

apps/go.mod
go.work

The project includes a submodule apps and its go.work file has:

go 1.180
use (
./apps
)

I have another go project (separate repository) wants to import this app submodule as a library, below is go.mod:

module xxxx

go 1.18

require (
	github.com/zhaoyi0113/test-go-module v0.0.3 // indirect
)

then I have below code:

package main

import (
	"github.com/zhaoyi0113/test-go-module/apps/logger"
)

func main() {
	logger.Test()
}

It fails to compile with the error:

main.go:7:2: no required module provides package github.com/zhaoyi0113/test-go-module/apps/logger; to add it:
        go get github.com/zhaoyi0113/test-go-module/apps/logger

How can I import the submodule from the multiple workspaces project?

答案1

得分: 1

你不能导入一个工作区。

以下是当前的情况:

github.com/zhaoyi0113/test-go-module/apps 上有一个模块,它将其模块路径声明为 github.com/zhaoyi0113/test-go-module(请参见此处)。由于实际路径与声明的路径不匹配(请注意其中一个路径中缺少 /apps),因此此模块无法使用。

xxxx 模块需要 github.com/zhaoyi0113/test-go-module,但在该路径下找不到模块。如前段所述,它位于 apps 子目录中。

根据您想要放置模块的位置,有两种可能的修复方法。

如果您想将导入的 go.mod 保留在当前目录,请更新模块路径以包括 apps

导入模块的 go.mod:

module github.com/zhaoyi0113/test-go-module/apps
go 1.19

导入模块的 go.mod:

module xxxx
go 1.18
require (
    github.com/zhaoyi0113/test-go-module/apps v0.0.3 // indirect
)

第二种常规选项是将 go.mod 放置在项目的根目录,并将 go.work 文件放在项目之外。要使此方法生效,请将 go.mod 移动到项目的根目录(就像初始提交中的位置一样),并将 go.work 移动到其他位置。导入模块不需要进行任何更改。

英文:

You cannot import a workspace.

Here's the current situation:

There is a module at github.com/zhaoyi0113/test-go-module/apps which declares its module path as github.com/zhaoyi0113/test-go-module. This module is not useable because the actual path to the module and the declared path do not match (notice that /apps is missing in one of the paths).

The xxxx module requires github.com/zhaoyi0113/test-go-module, but there there is no module found at the path. It's in the apps subdirectory as noted in the previous paragraph.

There are two possible fixes depending on where you want to place the module.

If you want to keep imported go.mod in it's current directory then update the module paths to include apps:

go.mod for imported module:

module github.com/zhaoyi0113/test-go-module/apps
go 1.19

go.mod for importing module:

module xxxx
go 1.18
require (
    github.com/zhaoyi0113/test-go-module/apps v0.0.3 // indirect
)

The second and conventional option
is to place the go.mod at the root of the project and put the go.work file outside of the project. To make this work, move go.mod to the root of the project (as it was in the initial commit) and move go.work somewhere else. No changes are required to the importing module.

huangapple
  • 本文由 发表于 2023年5月22日 09:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定