1.场景:
在程序报错的时候,想要固定的报错信息执行相应的代码逻辑所做的操作:
2.代码:
import strings
if errCheck != nil {
if strings.Contains(errCheck.Error(), "Table 'opstyu.checkdb' doesn't exist") {
// 表示表checkdb不存在,就是没有跑过校验数据
return "1","2"
} else {
initlog.Error.Println(errCheck)
}
}
上述代码表示:看一下errCheck.Error()报错信息里面有没有包含后面的字符串(“Table ‘opstyu.checkdb’ doesn’t exist”),如果包含那就执行下面的逻辑
3.这样做的原因:
errCheck.Error()出来的信息有空格还有各种不需要的信息打印出来,不好做判断,判断是否包含是最好的方法