Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

go没有try catch,那怎么统一处理底层的异常呢?

代码如下:

func main() {
    db, err := gorm.Open("mysql", "root:123456mh@/dbtest?charset=utf8&parseTime=True&loc=Local")
    if err != nil {
        panic("failed to connect mysql database")
    }
    defer db.Close()
    rows, err := getCertainTimeTradeData(db, getBackCertainDay(2), getBackCertainDay(0))
    if err != nil {
        panic("query failed")
    }
    var domain string
    var endtime time.Time
    var price uint
    var platform string
    for rows.Next()  {
        rows.Scan(&domain, &endtime, &price, &platform)
        fmt.Println(domain)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

golang 官方不推荐统一处理异常
你需要手动处理每一个返回的错误

这个虽然有争议,但是支持的人也是很多的。
牺牲了代码的简洁性但是增加了可维护性。

这些基础的问题 golang 官方的博客都有讲。

https://blog.golang.org/error...

还有 panic和recover那篇,自行搜索啦。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...