
Cut out summary from your post content here.
The remaining content of your post.
- 使用工厂模式创建db对象
- 使用策略模式,分别为ping、execCmd、execFile创建接口与实现
- 默认都要实现Ping,其他的根据参数添加策略实现
package main
import (
    "bufio"
    "fmt"
    "os"
    "github.com/go-redis/redis/v8"
)
func main() {
    // 连接 Redis
    client := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })
    // 打开文件
    file, err := os.Open("redis_commands.txt")
    if err != nil {
        fmt.Println("Failed to open file:", err)
        return
    }
    defer file.Close()
    // 逐行读取文件并执行命令
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        command := scanner.Text()
        if command != "" {
            // 执行 Redis 命令
            _, err := client.Do(command).Result()
            if err != nil {
                fmt.Println("Failed to execute command:", err)
            } else {
                fmt.Println("Command executed successfully:", command)
            }
        }
    }
    if err := scanner.Err(); err != nil {
        fmt.Println("Error reading file:", err)
    }
}
package main
import (
    "bufio"
    "fmt"
    "os"
    "strings"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
    // 连接 MongoDB
    client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
    if err != nil {
        fmt.Println("Failed to connect to MongoDB:", err)
        return
    }
    defer client.Disconnect(context.TODO())
    // 打开文件
    file, err := os.Open("mongodb_commands.txt")
    if err != nil {
        fmt.Println("Failed to open file:", err)
        return
    }
    defer file.Close()
    // 创建一个数据库和集合的示例
    collection := client.Database("mydatabase").Collection("mycollection")
    // 逐行读取文件并执行命令
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        command := strings.TrimSpace(scanner.Text())
        if command != "" {
            // 执行 MongoDB 命令
            _, err := collection.Database().RunCommand(context.TODO(), command)
            if err != nil {
                fmt.Println("Failed to execute command:", err)
            } else {
                fmt.Println("Command executed successfully:", command)
            }
        }
    }
    if err := scanner.Err(); err != nil {
        fmt.Println("Error reading file:", err)
    }
}