GoLand fails to import redis dependencies
There are downloads of guryburd and gomodel on the Internet. Here, install the dependencies according to the official website documents.
Execute the following command in the root directory of the project
Initialize a Go module:
go mod init github.com/my/repo
To install go-redis/v9: To install go-redis/v9:
go get github.com/redis/go-redis/v9
But there is still no package when referencing redis
It may be that which of these two commands was not successfully executed, we analyze them one by one
1. Initialize the mod file
First check whether the mod exists in the root path of the project
If the mod file creation fails or prompts a conflict, delete GOPATH in the settings
2. Install the go-redis package
After the second command is executed, check the go.mod file.
If it is successfully displayed, there is no problem
. If it becomes popular, it may be a problem with GOPROXY. You need to configure the following in settings
GOPROXY=https://goproxy.io
3. Test
command line input
go mod tidy
Then test whether the redis dependency is available in the program
Start the connection to redis and print the redis.Client type variable
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
fmt.Println(client)
}
Available with output
Redis<cnetos:6379 db:0>