简体   繁体   中英

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 :

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 mod init github.com/my/repo
  1. Install redis/v8 using the command
go get github.com/go-redis/redis/v8

Create a main.go file and write the following code to check for your connection

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 run main.go

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM