首页 > 解决方案 > 找不到提供包的模块

问题描述

我不确定如何解决我在“go mod”中发现的依赖问题。据我所知,它正在获取一些子依赖项的错误版本,这些子依赖项指向一个不再存在的存储库。

我很新,所以我确定我搞砸了,我很想得到一些帮助来了解如何解决这个问题。请检查以下示例:

使用 go get 获得我唯一的依赖可以正常工作

export GOPATH=`mktemp -d`
export MYAPP=`mktemp -d`
cd $MYAPP

cat << EOF > main.go
package main
import (
  "fmt"
  "os"
  "github.com/kubernetes/minikube/pkg/storage"
)
func main() {
  if err := storage.StartStorageProvisioner(); err != nil {
    fmt.Printf("Error starting provisioner: %v\n", err)
    os.Exit(1)
  }
}
EOF

go get github.com/kubernetes/minikube/pkg/storage
go build && echo "WORKED" || echo "FAILED"

但是,使用 go mod 获取它不起作用

export GOPATH=`mktemp -d`
export MYAPP=`mktemp -d`
cd $MYAPP

cat << EOF > main.go
package main
import (
  "fmt"
  "os"
  "github.com/kubernetes/minikube/pkg/storage"
)
func main() {
  if err := storage.StartStorageProvisioner(); err != nil {
    fmt.Printf("Error starting provisioner: %v\n", err)
    os.Exit(1)
  }
}
EOF

go mod init github/my/repo
go build && echo "WORKED" || echo "FAILED"

我如何让最后一个工作?

$ go version
go version go1.12 darwin/amd64

标签: gogo-modules

解决方案


According to the Go modules wiki

Day-to-day upgrading and downgrading of dependencies should be done using 'go get', which will automatically update the go.mod file. Alternatively, you can edit go.mod directly.

To the extent that I've understood go mod init won't go get your dependencies, rather it'll initialize a new module and create a mod file to track the dependency versions that your module is using.

So go getting your dependencies is fine.

Go modules on the other hand will according to the wiki again provide certain functionalities:

Standard commands like go build or go test will automatically add new dependencies as needed to satisfy imports (updating go.mod and downloading the new dependencies).

When needed, more specific versions of dependencies can be chosen with commands such as go get foo@v1.2.3, go get foo@master, go get foo@e3702bed2, or by editing go.mod directly.


推荐阅读