Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I execute `go install`, it reports "not a main package"

My environment is go1.17.2 Windows AMD64 with go env GO111MODULE=on.

I add import "github.com/go-redis/redis/v8" in my code. I execute go install github.com/go-redis/redis/v8@latest, but the result is package github.com/go-redis/redis/v8 is not a main package. And when I execute go run main.go, it shows cannot find package at the line of import github.com/go-redis/redis/v8.

What's wrong of my operations or the config of environment?

Content of go.mod (with simple go mod init & go mod tidy):

module ...
go 1.17

require github.com/go-redis/redis/v8 v8.11.4

require (
    github.com/cespare/xxhash/v2 v2.1.2 // indirect
    github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
)
like image 970
Eternally_Ascend Avatar asked Dec 19 '25 03:12

Eternally_Ascend


2 Answers

I use import "github.com/go-redis/redis", and restart the process (include go mod init, go mod tidy, go install), it shows right result finally. But the version of go redis changes to v6.15.9+incompatible in go.mod file automatedly.

like image 93
Eternally_Ascend Avatar answered Dec 21 '25 20:12

Eternally_Ascend


The module name of your go.mod is invalid. I try the similar module name in my environment and compile with go build, it reports:

$ go build
go: malformed module path "...": invalid path element "..."

Try a name like:

module tempredis
go 1.17

require github.com/go-redis/redis/v8 v8.11.4

Or create the module with command go mod init tempredis then add a dependency of github.com/go-redis/redis/v8.

Refer to document.


Update
According to official document:

Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.

In Go 1.18, go get will no longer build packages; it will only be used to add, update, or remove dependencies in go.mod. Specifically, go get will always act as if the -d flag were enabled.

  • When you want to write some new code and manage dependencies like github.com/go-redis/redis/v8@latest, you should use go get;
  • To install executables, use go install:

To install an executable in the context of the current module, use go install, without a version suffix... This applies version requirements and other directives from the go.mod file in the current directory or a parent directory.

To install an executable while ignoring the current module, use go install with a version suffix like @v1.2.3 or @latest... When used with a version suffix, go install does not read or update the go.mod file in the current directory or a parent directory.

like image 37
rustyhu Avatar answered Dec 21 '25 19:12

rustyhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!