简体   繁体   中英

Cannot find module providing package

I'm not sure how to solve a dependency issue I'm finding with "go mod". From what I could gather, it's fetching the wrong version of some sub-dependency which points to a repo that doesn't exist anymore.

I very very new to go so I'm sure I'm screwing up, I'd love some help to understand how to fix this. Please check these examples:

Getting my only dependency with go get works fine

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"

However, getting it with go mod doesn't work

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"

How do I get this last one working?

$ go version
go version go1.12 darwin/amd64

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM