当我使用`go install`时,它返回的是一个非主包。

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

When I using `go install`, it returns not a main package

问题

我在我的代码中使用了import "github.com/go-redis/redis/v8"。我的环境是go1.17.2 Windows AMD64。我执行了go install github.com/go-redis/redis/v8@latest,但结果显示package github.com/go-redis/redis/v8 is not a main package。我的操作或环境配置有什么问题?go env GO111MODULE=on

当我执行go run main.go时,它在import github.com/go-redis/redis/v8这一行显示cannot find package

go.mod文件中的内容(使用简单的go mod initgo 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
)
英文:

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
)

答案1

得分: 1

你的go.mod文件中的模块名是无效的。我在我的环境中尝试了类似的模块名,并使用go build进行编译,结果报错:

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

尝试使用以下类似的模块名:

module tempredis
go 1.17

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

或者使用命令go mod init tempredis创建模块,然后添加github.com/go-redis/redis/v8的依赖。

请参考文档

英文:

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.

答案2

得分: 1

我使用 import "github.com/go-redis/redis",并重新启动进程(包括 go mod initgo mod tidygo install),最终显示正确的结果。但是在 go.mod 文件中,go redis 的版本自动更改为 v6.15.9+incompatible

英文:

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.

huangapple
  • 本文由 发表于 2021年11月10日 11:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/69907512.html
匿名

发表评论

匿名网友

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

确定