Home > Blockchain >  go install golang.org/x/crypto/pbkdf2@latest returns 'not a main package'
go install golang.org/x/crypto/pbkdf2@latest returns 'not a main package'

Time:08-20

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?

CodePudding user response:

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/[email protected]/

  • Related