如何在Go服务中的monorepo中导入包?

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

How to import packages in monorepo within Go service?

问题

我有一个单体仓库,假设它是 github.com/myself/monorepo。我在其中有两个服务:service-oneservice-two。每个服务都有一个 go.mod 文件。

service-one 中,我有一个要导入的类型:

package service-one

type StructOne struct {

}

我从 service-two 中导入它:

package service-two

import pack "github.com/myself/monorepo/service-one"

func MyFunc() {
	var v pack.StructOne
}

service-twogo.mod 文件中,我尝试了两个选项:

引用分支上的临时提交:

require github.com/myself/monorepo/service-one 38e00c33c01a9ac17c177ba25e9764c4d1c1310f

引用 master 分支上的标记提交:

require github.com/myself/monorepo/service-one v0.0.0

两次都出现了类似的错误:

 not found: github.com/myself/monorepo/service-one@v0.0.0 invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/...
   fatal: could not read Username for 'https://github.com': terminal prompts disabled

我应该如何正确安排仓库?我做错了什么?

英文:

I have a monorepo. Let's say it's github.com/myself/monorepo. I have two services in it: service-one and service-two. Each has a go.mod file.

In service-one I have a type to import:

package service-one

type StructOne {

}

And I'm importing it from service-two:

package service-two

import pack "github.com/myself/monorepo/service-one"

func MyFunc() {
	var v pack.StructOne
}

In service-two go.mod file I tried two options:

To refer temporary commit on a branch:

require github.com/myself/monorepo/service-one 38e00c33c01a9ac17c177ba25e9764c4d1c1310f

To refer a tagged commit on master

require github.com/myself/monorepo/service-one v0.0.0

Both times I get an error like:

 not found: github.com/myself/monorepo/service-one@v0.0.0 invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/...
   fatal: could not read Username for 'https://github.com': terminal prompts disabled

How should I arrange repository correctly? What do I do wrong?

答案1

得分: 2

> 致命错误:无法读取“https://github.com”的用户名:终端提示已禁用

如果这是一个私有仓库,你应该:

  1. 设置 GOPRIVATE=github.com/myself/monorepo,这样它就不会尝试从代理服务器下载。
  2. 确保 git 不需要交互式输入密码。例如,使用 凭据助手 来存储凭据。

但在此之前,请问一下你为什么要创建两个模块。如果没有充分的理由,可以创建一个包含多个包的单一模块。

如果必须使用多个模块,可以利用工作区来简化本地开发。请参阅 教程:使用多模块工作区入门

请注意,在仓库的子目录中定义的模块标记方式不同。请参阅 将版本映射到提交

> 如果模块在仓库的子目录中定义,也就是说,模块路径的子目录部分不为空,则每个标签名称必须以模块子目录为前缀,后跟一个斜杠。例如,模块 golang.org/x/tools/gopls 在仓库的根路径 golang.org/x/tools 的 gopls 子目录中定义。该模块的版本 v0.4.0 必须在该仓库中具有名为 gopls/v0.4.0 的标签。

你的问题中还有另一个问题:

> require github.com/myself/monorepo/service-one 38e00c33c01a9ac17c177ba25e9764c4d1c1310f

你不应该手动将提交 ID 放入 go.mod 文件中。运行以下命令,Go 工具将为你将其转换为伪版本并更新 go.mod。请参阅 伪版本

  • go get github.com/myself/monorepo/service-one@38e00c33c01a9ac17c177ba25e9764c4d1c1310f
  • 或者 go get github.com/myself/monorepo/service-one@master
英文:

> fatal: could not read Username for 'https://github.com': terminal prompts disabled

If this is a private repository, you should:

  1. Set GOPRIVATE=github.com/myself/monorepo so that it won't try to download from the proxy server.
  2. Make sure git doesn’t require a password to be entered interactively. For example, use a credential helper to store the credential.

But before that, ask yourself why you want to create two modules. If there is not a good reason, create a single module that containing multiple packages instead.

If you have to go with multiple modules, utilize the workspace to ease local development. See Tutorial: Getting started with multi-module workspaces.

And please note that a module defined in a subdirectory within the repository is tagged differently. See Mapping versions to commits:

> If a module is defined in a subdirectory within the repository, that is, the module subdirectory portion of the module path is not empty, then each tag name must be prefixed with the module subdirectory, followed by a slash. For example, the module golang.org/x/tools/gopls is defined in the gopls subdirectory of the repository with root path golang.org/x/tools. The version v0.4.0 of that module must have the tag named gopls/v0.4.0 in that repository.

There is another issue in your question:

> require github.com/myself/monorepo/service-one 38e00c33c01a9ac17c177ba25e9764c4d1c1310f

You should not put commit id into the go.mod file manually like this. Run the following commands and the Go tool will translate it into a pseudo-version and updated go.mod for you. See Pseudo-versions:

  • go get github.com/myself/monorepo/service-one@38e00c33c01a9ac17c177ba25e9764c4d1c1310f
  • or go get github.com/myself/monorepo/service-one@master

huangapple
  • 本文由 发表于 2023年5月30日 00:06:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358794.html
匿名

发表评论

匿名网友

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

确定