如何在另一个私有Go项目(作为模块)中导入私有Go库(作为模块) [英] How to import a private Go library (as module) within another private Go project (as module)
问题描述
我正在将一些私有的Go项目移至GitLab,同时摆脱了 Godeps
,带有 vendor
目录的Go dep
我只想使用Go模块.
我正在使用Go版本: go1.12.6 linux/amd64
.
- 我在
gitlab.com/my-company/my-team/my-library
上有一个私人的库"项目/git存储库.可以和go.mod
和go.sum
一起用作Go模块. - 我想将
my-library
用作另一个项目的依赖项,这是gitlab.com/my-company/my-team/my-project
.
URL的结构是相同的,唯一改变的是存储库的名称.
在 my-project
中导入 my-library
时,我遇到各种错误.
- 如何将Go模块作为私有存储库导入其他Go模块作为私有存储库?
- 下面的命令输出有什么问题?
我知道GitLab令牌可以工作,因为 go get
命令会自行计算出 my-library
的提交ID.无论如何,我已经完成了以下操作(请参见 https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html ):
git config \- 全球的 \网址."https://token_name:token_val@gitlab.com"."https://gitlab.com"
这些是错误消息:
$ go get -u gitlab.com/my-company/my-team/my-library.git前往:查找gitlab.com/my-company/my-team/my-library.git最新转到:gitlab.com/my-company/my-team/my-library.git@v0.0.0-20190802120216-712a10fb5fac:解析go.mod:意外的模块路径"gitlab.com/my-company/my-team/my-图书馆"go get:错误加载模块要求$$去获取-u gitlab.com/my-company/my-team/my-library去获得gitlab.com/my-company/my-team/my-library:git ls-remote -q https://gitlab.com/my-company/my-team.git在/home/foo/Development/go中-workspace/pkg/mod/cache/vcs/9be637426eac43b329899d57d9375d12246f2cc0f6ddd098446bc42ed1ca534d:退出状态128:远程:找不到您要查找的项目.致命:找不到存储库'https://token_name:token_val@gitlab.com/my-company/my-team.git/$$ GO111MODULE = off进入-u gitlab.com/my-company/my-team/my-library.git软件包gitlab.com/my-company/my-team/my-library.git:/home/foo/Development/go-workspace/src/gitlab.com/my-company/my-team/my-中没有Go文件library.git$
- 第一次尝试是我认为应该使用的尝试,但是它不起作用,我也不知道为什么.
- 我以为这是由于项目中已经存在某种依赖而导致的.但是,当执行
go mod graph |grep gitlab
我发现输出为空,即没有冲突的依赖关系.
- 我以为这是由于项目中已经存在某种依赖而导致的.但是,当执行
- 应该避免第二次尝试,因为URL并非以
.git
结尾,并且(出于某些我不理解的原因)URL被切成级别我的团队
. -
GO111MODULE = off
似乎迫使go get
检入$ GOPATH
,我认为我应该避免这样做,但我只是试图看看我是否能够获取该依赖关系.
我绝对建议尝试使用Go 1.13 beta:
$ go get golang.org/dl/go1.13beta1$ go1.13beta1下载$ go1.13beta1获取foo
或更妙的是,尝试使用最新的Go on tip/master(鉴于Go 1.13 beta1版本此时已存在一个月以上):
$ go get golang.org/dl/gotip$ gotip下载$ gotip获取foo
Go 1.13改进了使用私有存储库的几个方面(包括 CL 170879以及其他一些改进),并且与Go 1.12相比,它通常具有更好的错误消息.
对于您的第一条错误消息:
$ go get -u gitlab.com/my-company/my-team/my-library.git前往:查找gitlab.com/my-company/my-team/my-library.git最新前往:gitlab.com/my-company/my-team/my-library.git@v0.0.0-20190802120216-712a10fb5fac:解析go.mod:意外的模块路径"gitlab.com/my-company/my-team/my-library"go get:错误加载模块要求
这是 go
命令,抱怨模块的导入/请求方式与模块如何在其 go.mod
.如果模块 foo
导入模块 bar
,则 foo
需要引用 bar
的方式与bar
在 bar
的 go.mod
文件的 module
行上声明其身份.
以另一种方式表示,用于导入模块(或 go
一个模块)的导入路径需要以在 module
行中声明的确切模块路径开头导入的模块的 go.mod
.您可能需要更改导入程序以匹配 go.mod
文件中 module
行上声明的格式,或者可能需要更改 module
go.mod
中的code>行,以匹配导入器使用的格式,但不能不同意.如果他们不同意,您将得到报告的第一个错误.
通常,您可以选择如何将私有存储库与Go模块一起使用.这两篇博客文章概述了几个问题,并介绍了一些方法,如果您还没有阅读它们,那么非常值得一读:
- 使用Go模块获取私有依赖项" ,蒂姆·雷蒙德(Tim Raymond)
- 使用私有模块去Git存储库" ,作者蒂姆·琼斯(Tim Jones)
最后,如果仍然不清楚发生了什么,您可能应该尝试 go get -v foo
或 go get -v -x foo
:>
-
如果问题的本质仍然不清楚,您还可以尝试更详细的
go get -v -x foo
,它还显示了git或其他正在发布的VCS命令.如果有必要,通常可以在go
工具的上下文之外执行相同的git命令,以进行故障排除.
I am moving a few private Go projects to GitLab while getting rid of Godeps
, Go dep
with vendor
directory and all of that because I would like to use just Go modules.
I am using Go version: go1.12.6 linux/amd64
.
- I have a private "library" project / git repository at
gitlab.com/my-company/my-team/my-library
. This works as a Go module withgo.mod
andgo.sum
. - I would like to use
my-library
as a dependency for another project, this:gitlab.com/my-company/my-team/my-project
.
The structure of the URL is the same, the only thing that changes is the name of the repository.
I am facing all sorts of errors while importing my-library
in my-project
.
- How can I import Go modules as private repositories inside other Go modules as private repositories?
- What's wrong with the command outputs below?
I know the GitLab tokens work because the go get
command figures out itself the commit ID of my-library
. Anyway I've done the following (see https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html):
git config \
--global \
url."https://token_name:token_val@gitlab.com".insteadOf \
"https://gitlab.com"
These are the error messages:
$ go get -u gitlab.com/my-company/my-team/my-library.git
go: finding gitlab.com/my-company/my-team/my-library.git latest
go: gitlab.com/my-company/my-team/my-library.git@v0.0.0-20190802120216-712a10fb5fac: parsing go.mod: unexpected module path "gitlab.com/my-company/my-team/my-library"
go get: error loading module requirements
$
$ go get -u gitlab.com/my-company/my-team/my-library
go get gitlab.com/my-company/my-team/my-library: git ls-remote -q https://gitlab.com/my-company/my-team.git in /home/foo/Development/go-workspace/pkg/mod/cache/vcs/9be637426eac43b329899d57d9375d12246f2cc0f6ddd098446bc42ed1ca534d: exit status 128:
remote: The project you were looking for could not be found.
fatal: repository 'https://token_name:token_val@gitlab.com/my-company/my-team.git/' not found
$
$ GO111MODULE=off go get -u gitlab.com/my-company/my-team/my-library.git
package gitlab.com/my-company/my-team/my-library.git: no Go files in /home/foo/Development/go-workspace/src/gitlab.com/my-company/my-team/my-library.git
$
- The first attempt is the one I think I should use, but it does not work and I don't know why.
- I thought this was happening because somehow there was already a dependency like this in the project. But when executing
go mod graph | grep gitlab
I find an empty output, i.e. there is no conflicting dependency.
- I thought this was happening because somehow there was already a dependency like this in the project. But when executing
- The second attempt is probably to be avoided because the URL does not end with
.git
and (for some reason that I don't understand) the URL is cut at the level ofmy-team
. GO111MODULE=off
seems to forcego get
to check in$GOPATH
which I think I should avoid at all, but I was just trying to see if that way I was able to fetch that dependency.
I would definitely suggest trying with Go 1.13 beta:
$ go get golang.org/dl/go1.13beta1
$ go1.13beta1 download
$ go1.13beta1 get foo
or even better, try the latest Go on tip / master (given the Go 1.13 beta1 release is over a month old at this point):
$ go get golang.org/dl/gotip
$ gotip download
$ gotip get foo
Go 1.13 improved several aspects of working with private repositories (including CL 170879 among other improvements), and also it generally has better error messages compared to Go 1.12.
For your first error message:
$ go get -u gitlab.com/my-company/my-team/my-library.git
go: finding gitlab.com/my-company/my-team/my-library.git latest
go: gitlab.com/my-company/my-team/my-library.git@v0.0.0-20190802120216-712a10fb5fac:
parsing go.mod: unexpected module path "gitlab.com/my-company/my-team/my-library"
go get: error loading module requirements
That is the go
command complaining about a mismatch between how a module is imported/required, vs. how it declares its own identity in the module
line of its go.mod
. If module foo
is importing module bar
, then foo
needs to refer to bar
the same way that bar
declares its identity on the the module
line of bar
's go.mod
file.
Said another way, the import path used to import a module (or go get
a module) needs to start with the exact module path declared on the module
line of the imported module's go.mod
. You might need to change the importer to match the form declared on the module
line in the go.mod
file, or you might need to change the module
line in the go.mod
to match the form used by the importer, but they can't disagree. If they disagree, you get the first error you reported.
In general, you have a few choices about how to use private repos with Go modules. These two blog posts outline several of the issues and cover a few approaches, and are well worth a read if you haven't read them:
- "Fetching Private Dependencies with Go Modules", by Tim Raymond
- "Go Modules with Private Git Repositories", by Tim Jones
Finally, if it is still not clear what is going on, you probably should try go get -v foo
or go get -v -x foo
:
The
-v
flag togo get
asks to print more verbose details, including the HTTPS requests, though be mindful that certain "errors" such as 404 errors might be expected based on how a remote repository was configured.If the nature of the problem is still not clear, you can also try the more verbose
go get -v -x foo
, which also shows the git or other VCS commands being issued. If warranted, you can often execute the same git commands outside of the context of thego
tool for troubleshooting purposes.
这篇关于如何在另一个私有Go项目(作为模块)中导入私有Go库(作为模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!