gpt4 book ai didi

go - 找不到提供包的模块

转载 作者:数据小太阳 更新时间:2023-10-29 03:20:23 26 4
gpt4 key购买 nike

我不确定如何解决我在“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

最佳答案

根据 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.

据我了解,go mod init 不会获取您的依赖项,而是会初始化一个新模块并创建一个 mod 文件来跟踪您的模块正在使用的依赖项版本。

所以去获取你的依赖关系很好。

另一方面,根据 wiki,Go 模块再次提供某些功能:

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.

关于go - 找不到提供包的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54927867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com