package main import ( "database/sql" "fmt" "mylog" "time" ) type Middleware struct{} /*如遇错误,执行额外任务*/ func (m *Middleware) Println(v interface{}) { if err, ok := v.(error); ok { mylog.Println("执行额外任务:", err) } mylog.Println("正常执行原本任务:", v) } var dblink *sql.DB /*自行补充初始化过程*/ /*如遇错误,执行额外任务*/ func (m *Middleware) Println_sendln(v ...interface{}) { for _, arg := range v { if err, ok := arg.(error); ok { mylog.Println("错误信息:", err) err_msg := `警告消息:` + err.Error() //错误信息里可能有单引号 dingdingmsg := `exec dingding_add_msg ?,0,'xxk'` // mylog.Println("sql:", dingdingmsg) dblink.Exec(dingdingmsg, err_msg) } } mylog.Println(v...) } func main() { if err := DBinit_sqlserver2(); err != nil { mylog.Println(err) fmt.Println(err) time.Sleep(5 * time.Minute) return } else { fmt.Println(`链接sqlserver2成功`) } mv := &Middleware{} mv.Println(11) mv.Println(22) mv.Println(fmt.Errorf("自定义错误")) mv.Println_sendln(fmt.Errorf("测试自定义错误"), `121`) }
标签:额外,mylog,err,fmt,中间件,Println,mv,GO,Middleware From: https://www.cnblogs.com/agfox123/p/18190690