登录
首页 >  Golang >  Go问答

"golang.org/x/crypto/pbkdf2@latest无法被作为主模块安装"

来源:stackoverflow

时间:2024-03-28 22:27:30 438浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《"golang.org/x/crypto/pbkdf2@latest无法被作为主模块安装"》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

我是 go 新手,正在尝试运行包含以下内容的 go 脚本:

import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "crypto/sha256"
    "encoding/base64"
    "errors"
    "fmt"
    "io"

    "golang.org/x/crypto/pbkdf2"
)

如果我尝试运行该脚本,似乎我缺少 pbkdf2 包:

$ go run decryptgrafanapassword.go
decryptgrafanapassword.go:12:2: no required module provides package golang.org/x/crypto/pbkdf2: go.mod file not found in current directory or any parent directory; see 'go help modules'

但是当我尝试安装它时,它也抱怨它不是主包:

$ go install golang.org/x/crypto/pbkdf2@latest                                                          
package golang.org/x/crypto/pbkdf2 is not a main package

运行该程序的最简单方法是什么?


正确答案


go install 下载软件包并构建可执行文件。每个可执行文件都必须有一个名为 main 的子模块。由于 golang.org/x/crypto/pbkdf2 没有 main,所以 go install 失败。

实际上,您所需要的只是 go mod tidy。它读取源代码,将所需模块写入 go.mod 并下载它们。我用您的导入创建了一个小示例,这就是 go mod tidy 所做的:

code$ go mod tidy
go: finding module for package golang.org/x/crypto/pbkdf2
go: downloading golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8
go: found golang.org/x/crypto/pbkdf2 in golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8

这是 go.mod,通过此命令更新:

module example.org

go 1.16

require golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8

golang.org/x/crypto 的源代码已自动下载到 $gopath/pkg/mod/golang.org/x/[email protected]/

以上就是《"golang.org/x/crypto/pbkdf2@latest无法被作为主模块安装"》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>