1

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
Pajri Aprilio
  • 322
  • 5
  • 16
  • Please use only module builds. GOPATH builds are basically deprecated. – Volker Oct 24 '20 at 09:29
  • i see. so there's no way i can install redis-go without using go module ? – Pajri Aprilio Oct 24 '20 at 09:40
  • Look at the following response in order to structure your project using the `go modules`: https://stackoverflow.com/a/57944766/9361998 – alessiosavi Oct 24 '20 at 10:07
  • 2
    @PajriAprilio You are already using Go 1.15 and you have modules by default only. And it's much better than the `go get` way; trust me. So, please use that. Very simple to use! – shmsr Oct 24 '20 at 12:42
  • 1
    It is dead simple to "install" redis-go without modules, but there is no point. If you cannot get it running with modules you won't be able to do it properly in GOPATH mode either. Modules are the future. Get used to them now. – Volker Oct 24 '20 at 15:59
  • i see. thank you all. i'll use go module for my future project. – Pajri Aprilio Oct 25 '20 at 07:49

1 Answers1

1

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

enter image description here

Deeksha Sharma
  • 3,199
  • 1
  • 19
  • 16