简体   繁体   English

下载 go-redis 包时报错找不到包“github.com/go-redis/redis/v8”

[英]Error cannot find package "github.com/go-redis/redis/v8" when downloading go-redis package

I tried to download go-redis using this command go get github.com/go-redis/redis/v8 but I got this following error :我尝试使用此命令下载 go-redis go get github.com/go-redis/redis/v8但出现以下错误:

cannot find package "github.com/go-redis/redis/v8" in any of:
        C:\Go\src\github.com\go-redis\redis\v8 (from $GOROOT)
        E:\Go Workspace\src\github.com\go-redis\redis\v8 (from $GOPATH)

Why did I get this error and how to fix this ?为什么我会收到此错误以及如何解决此问题?

OS : Windows
Go version : go version go1.15 windows/amd64

Following steps solved my problem:以下步骤解决了我的问题:

  1. Initialize the go module (since go-redis supports last 2 Go versions & requires support for Go Modules初始化 go 模块(因为 go-redis 支持最后 2 个 Go 版本并且需要支持Go Modules
go mod init github.com/my/repo
  1. Install redis/v8 using the command使用命令安装 redis/v8
go get github.com/go-redis/redis/v8

Create a main.go file and write the following code to check for your connection创建一个 main.go 文件并编写以下代码来检查您的连接

package main

import (
    "fmt"
    "github.com/go-redis/redis"
)

func main() {
    fmt.Println("Go Redis Connection Test")

    client := redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
        Password: "",
        DB: 0,
    })

    pong, err := client.Ping().Result()
    fmt.Println(pong, err)

}

When we will run this now, we will see that the Go application will successfully ping the redis instance and it will return a successful PONG response:当我们现在运行它时,我们将看到 Go 应用程序将成功 ping redis 实例并返回一个成功的 PONG 响应:

go run main.go

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将键数组传递给 go-redis 包中的 MGET func? - how to pass array of keys to MGET func in go-redis package? 如何使用 go-redis/redis 包在 Go 中创建 Redis 事务? - How to create Redis Transaction in Go using go-redis/redis package? 使用不带WATCH的go-redis包实现流水线和事务到Redis集群 - Implementing pipelining and transaction both to a redis cluster using go-redis package without WATCH 带字符串字段的Go-Redis HMSet提供WRONGTYPE操作 - Go-Redis HMSet with string fields gives WRONGTYPE Operation go-redis:当键不存在时,TTL返回负的持续时间 - go-redis: TTL returns a negative duration when the key does not exist 如何从 golang go-redis 中的 redis.Cmder 获取价值? - How to get get value from redis.Cmder in golang go-redis? 为什么不能用 go-redis 获得的值将字符串转换为 int? - Why can't convert string to int with value got by go-redis? 如何在 go-redis 中从 client.Watch 函数运行非事务管道 - How to run non transaction pipeline from a client.Watch function in go-redis 无法编组,(实现 encoding.BinaryMarshaler)。 带有多个对象的 go-redis Sdd - Can't marshal, (implement encoding.BinaryMarshaler). go-redis Sdd with multiple objects go protobuf:找不到 package“。” 在 github.com/gogo/protobuf/proto 和 m.TimeStamp.MarshalToSizedBuffer 未定义 - go protobuf: Cannot find package "." in github.com/gogo/protobuf/proto and m.TimeStamp.MarshalToSizedBuffer undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM