如何在 Go 工作区中导入一个项目?

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

How do I import a project within a Go Workspace?

问题

我有一个包含多个模块的单体库。其中一些模块是库,其他模块是生成可执行文件的入口点。

这是我的工作空间项目:
https://github.com/alshdavid/go-workspace-example

我有一个名为"bar"的库在这里,我希望可以供单体库之外的消费者导入。

package main

import (
	"fmt"

	"github.com/alshdavid/bar"
    // 或者: "github.com/alshdavid/gotest/modules/bar"
)

func main() {
	fmt.Println(bar.Bar)
}

尝试从工作空间的git仓库安装该包会失败。

go get github.com/alshdavid/gotest
go get github.com/alshdavid/gotest/modules/bar
go get github.com/alshdavid/bar # 试试看

我应该传递给go get的参数来指示它在嵌套文件夹中查找模块,还是我应该设置一个流水线,将我的嵌套模块推送到它自己的仓库中?

英文:

I have a monorepo with multiple modules within it. Some of the modules are libraries, others are entry points that will produce binaries.

Here's my workspace project:
https://github.com/alshdavid/go-workspace-example

I have my "bar" library here which I would like to make available for import by consumers outside of this mono repo.

package main

import (
	"fmt"

	"github.com/alshdavid/bar"
    // Or maybe: "github.com/alshdavid/gotest/modules/bar"
)

func main() {
	fmt.Println(bar.Bar)
}

Trying to install the package from the workspace git repo results in failure.

go get github.com/alshdavid/gotest
go get github.com/alshdavid/gotest/modules/bar
go get github.com/alshdavid/bar # worth a shot

Is there an argument I should pass to go get that instructs to to look for a module in a nested folder or should I instead set up a pipeline that pushes my nested module to its own repository?

答案1

得分: 1

你必须在每个模块中进行工作,即在你的单体库中运行 go mod init 命令。例如,在你的情况下,我假设你有一个名为 bar 的模块,并且你想在另一个模块 foo 中导入 bar 模块。

一个模块有一个 go.mod 文件。在你的 foo 模块的 go.mod 文件中,你可以添加以下内容:

replace github.com/alshdavid/bar => ../bar

你可以在这里了解更多信息。

英文:

You must be working inside a module i.e. ran: go mod init in each of your modules in your mono repo.
For example, in your case I am assuming you have module named bar and you want to import module bar inside another module e.g. foo.

A module has a go.mod file. Inside go.mod file of your foo module you can add the following:

replace github.com/alshdavid/bar => ../bar

You can read more about this here

答案2

得分: -2

我只需要在 go.mod 文件中的完整路径。

英文:

I just needed the complete path in the go.mod file

huangapple
  • 本文由 发表于 2022年6月29日 17:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/72799145.html
匿名

发表评论

匿名网友

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

确定