`go install golang.org/x/crypto/pbkdf2@latest` 返回 ‘not a main package’ 的错误信息。

huangapple go评论81阅读模式
英文:

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/

huangapple
  • 本文由 发表于 2022年8月19日 23:58:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/73419396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定