Use the go-redis/redis client library to connect to a Redis server by initializing a client with the server address and calling Ping() to verify the connection.
package main
import (
"context"
"log"
"github.com/redis/go-redis/v9"
)
func main() {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
ctx := context.Background()
_, err := rdb.Ping(ctx).Result()
if err != nil {
log.Fatal(err)
}
}