11

I used import "github.com/go-redis/redis/v8" in my code. The environment is go1.17.2 Windows AMD64. I executed 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. What's wrong of my operations or the config of environment. go env GO111MODULE=on.

And when I execute go run main.go, it shows cannot find package at the line of import github.com/go-redis/redis/v8.

Content in 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
)
Eternally_Ascend
  • 163
  • 1
  • 2
  • 7

2 Answers2

1

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.

rustyhu
  • 1,912
  • 19
  • 28
  • Actually, I created `go.mod` file with command `go mod init` and `go mod tidy` already. Can you help me check the content in `go.mod` file in my question? Thanks a lot. And when I execute `go build` it reports `cannot find package` as well. – Eternally_Ascend Nov 10 '21 at 03:20
  • @Eternally_Ascend The answer have been updated. Where is your `go.mod`? Is it in the same directory with `main.go`? Do you execute `go build` in the same directory path? Maybe after `go mod init` you need to `go get github.com/go-redis/redis/v8` manually. – rustyhu Nov 10 '21 at 06:39
1

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.

Eternally_Ascend
  • 163
  • 1
  • 2
  • 7