goroutine, golang 的并发支持很独特,我们的工具支持多个库同时备份,很容易使用 goroutine 来实现并行。
## 代码解析
按功能模块对核心代码进行说明
###main.go,并发、命令行参数
使用命令行参数,接受一个参数,来指定备份的内容
package common
type OpFlag struct {
Tables bool //表结构
Datum bool //表结构和数据
Views bool //视图
Funcs bool //函数与存储过程
}
main.go,程序入口,处理命令行参数
if len(os.Args) > 1 {
flag = common.OpFlag{
Tables: false,
Datum: false,
Views: false,
Funcs: false,
}
switch os.Args[1] { //接受一个参数
case "table":
flag.Tables = true //根据参数设定标识量
case "data":
flag.Tables = true
flag.Datum = true
case "views":
flag.Views = true
case "funcs":
flag.Funcs = true
default: //参数不正确,报错退出
log.Fatal("You arg must be in : table, data, views or funcs.")
}
}else{ //无参数,默认导出所有
flag = common.OpFlag{
Tables: true,
Datum: true,
Views: true,
Funcs: true,
}
}
err := backUp.Export(flag) 根据参数进行数据库备份
###Export.go