英文:
go install golang.org/x/crypto/pbkdf2@latest returns 'not a main package'
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是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
有什么最简单的方法可以让它运行起来?
英文:
I'm new to go and am trying to run a go script that include the following:
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"io"
"golang.org/x/crypto/pbkdf2"
)
If I try to run the script it appears that I am missing the pbkdf2 package:
$ 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'
But when I try to install it it also complains that it isn't a main package:
$ go install golang.org/x/crypto/pbkdf2@latest
package golang.org/x/crypto/pbkdf2 is not a main package
What is the simplest way to get this running?
答案1
得分: 3
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/crypto@v0.0.0-20220817201139-bc19a97f63c8/
目录下。
英文:
go install
dowloads a package and builds an executable. Every executable must have a submodule called main
. Since golang.org/x/crypto/pbkdf2
has no main
, go install
fails.
Actually, all you need is go mod tidy
. It reads the source code, writes required modules to go.mod
and downloads them. I created a tiny example with your imports and this is what go mod tidy
done:
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
Here is go.mod
, updated by this command:
module example.org
go 1.16
require golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8
The source code for golang.org/x/crypto
was automatically downloaded to $GOPATH/pkg/mod/golang.org/x/crypto@v0.0.0-20220817201139-bc19a97f63c8/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论