package main
import (
"context"
"fmt"
"log"
"os"
"strconv"
"github.com/go-redis/redis/v8"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
// Retrieve environment variables
redisAddr := os.Getenv("REDIS_ADDR")
redisPassword := os.Getenv("REDIS_PASSWORD")
redisDBStr := os.Getenv("REDIS_DB")
// Convert redisDBStr to an integer
redisDB, err := strconv.Atoi(redisDBStr)
if err != nil {
log.Fatalf("Invalid REDIS_DB value: %v", err)
}
// Connect to Redis
client := redis.NewClient(&redis.Options{
Addr: redisAddr, // host and port
Password: redisPassword, // password
DB: redisDB, // DB Number
})
// Check Connection
pong, err := client.Ping(context.Background()).Result()
if err != nil {
fmt.Println("Error connecting to Redis:", err)
return
}
fmt.Println("Connected to Redis:", pong)
}