【问题标题】:Go modules: "cannot find module providing package" importing sub package of a dependency [closed]Go模块:“找不到提供包的模块”导入依赖项的子包[关闭]
【发布时间】:2019-12-15 17:11:02
【问题描述】:

我在$GOPATH 之外有一个项目,我想使用go mod。 但是,当我从$GOPATH 的项目中复制代码并运行时

$ GO111MODULE=on go mod init github.com/jgoc/modtest
$ GO111MODULE=on go run main.go

我收到一个错误。

go version go1.12.5 windows/amd64

package main

import (
    "github.com/hajimehoshi/ebiten"
    "github.com/hajimehoshi/ebiten/vector"
)

build command-line-arguments: cannot load github.com/hajimehoshi/ebiten/vector: cannot find module providing package github.com/hajimehoshi/ebiten/vector

示例:https://github.com/jgoc/modtest

【问题讨论】:

  • 环境变量被称为GO111MODULE
  • 我编辑重做,还是报错
  • 再一次:相关环境变量的正确拼写是:GO111MODULE,即GO111,因为它是在 Go 1.11 中引入并控制模块的,而不是。 GO111MODULE 不是 GOMODULE111 !!
  • 很难就假冒进口产品提供任何建议。请使用您遇到问题的实际代码和您收到的实际错误来编辑您的问题。
  • 旁注:基于“我在 $GOPATH 之外有一个项目,我想使用 go mod”的评论,我怀疑环境变量的拼写可能是一个红鲱鱼,虽然很好修复。默认值为'auto',这意味着如果你运行'go mod init',你将处于GOPATH之外的模块模式,或者在GOPATH之外的目录中存在'go.mod'文件。模块 wiki 上的这个常见问题解答有详细信息:When do I get old behavior vs. new module-based behavior?

标签: go go-modules


【解决方案1】:

根据最近提供实际包名称的编辑,听起来您需要使用具有vector 包的github.com/hajimehoshi/ebiten 依赖项的版本。

具有有效 semver 发布标签的 github.com/hajimehoshi/ebiten 的最新版本是 https://github.com/hajimehoshi/ebiten/tree/v1.9.3。该版本似乎没有vector 包。

@master 版本确实有一个vector 包。 @v1.10.0-alpha 没有 vector 包。也许从@master 开始,至少看看你是否可以编译?

这对我有用:

go get -d github.com/hajimehoshi/ebiten/vector@master

有关详细信息,请阅读模块 wiki 的 How to Upgrade and Downgrade Dependencies 部分。


另外,您的模块的实际名称是什么?您用于导入该模块中的代码的实际导入路径是什么?

你写道:

go mod init Desktop/modtest

通常,模块的名称(也称为“模块路径”)应以 github.com 之类的主机名开头,并且通常以 repo 开头,例如:

go mod init github.com/my/repo.

然后,您在 .go 代码中使用以您传递给 go mod init 的完整模块路径开头的导入路径导入包,例如:

import "github.com/my/repo/pkg1".

使用您的示例,它将是:

go mod init github.com/<author>/<package>

进口将是:

import (
    "github.com/<author>/<package>"
    "github.com/<author>/<package>/<sub-package>"
)

如果您的模块路径与您的导入路径不一致,您可能会遇到类似于您所看到的错误。 (您的“模块路径”是您作为参数传递给go mod init 的内容,然后您可以在go.mod 文件的module 行中看到它。

请参阅this answer 了解更多上下文和更多详细信息。

【讨论】:

  • 基于链接,我的理解,我的依赖应该是一个存储库(每个子包都有go.mod),子包没有go.mod
  • 我的项目中使用 $GOPATH(v1.10.0-alpha) 的包也使用了与我的 go.mod(v1.9.3) 不同的版本,而 go.mod 版本没有有包裹。
  • 我并没有完全按照您的要求进行操作,但是您刚刚在 github.com/jgoc/modtest 添加的示例中的模块名称与存储库名称不一致。如果 repo 是github.com/jgoc/modtest,那么go.mod 文件的module 行上的模块名称应该是module github.com/jgoc/modtest
  • 另外,带有有效 semver 标签的 github.com/hajimehoshi/ebiten 的最新版本是 github.com/hajimehoshi/ebiten/tree/v1.9.3。该版本似乎没有github.com/hajimehoshi/ebiten/vector。如果你想要一个特定的版本,你应该go get它。例如,master 上的当前提交似乎有一个 vector 包。特别是,这对我有用:go get -d github.com/hajimehoshi/ebiten/vector@master。此外,值得阅读 this FAQ 获取特定版本。
  • 好的。您需要使用具有 vector 包的依赖项版本。 @master 版本确实有一个 vector 包。 @v1.10.0-alpha 没有 vector 包。 @v1.9.3 也没有 vector 包。也许从@master 开始,至少看看你是否可以编译?如上所述,这对我有用:go get -d github.com/hajimehoshi/ebiten/vector@master
猜你喜欢
  • 1970-01-01
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 2021-11-16
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
相关资源
最近更新 更多