golang连接redis示例

59 阅读1分钟
package main

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
)

func main() {
	// Create a new Redis client
	rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379", // Replace with your Redis server address
		Password: "",               // Replace with your Redis password, if any
		DB:       0,                // Replace with your Redis database number
	})
	// Ping the Redis server to check the connection
	pong, err := rdb.Ping(context.Background()).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(pong) // Output: PONG
}