我不用 elseit

大部分的 else 能够用反转判断来实现。例如:class

if something.OK() {
    something.Lock()
    defer something.Unlock()
    err := something.Do()
    if err == nil {
            stop := StartTimer()
            defer stop()
            log.Println("working...")
            doWork(something)
            <-something.Done() // wait for it
            log.Println("finished")
            return nil
    } else {
            return err
    }
} else {
    return errors.New("something not ok")
}

//
// *守护式编程*
// 上面的代码能够用反转判断来优化。
if !something.OK() {  // flipped
    return errors.New("something not ok")
}
something.Lock()
defer something.Unlock()
err := something.Do()
if err != nil {       // flipped
    return err
}
stop := StartTimer()
defer stop()
log.Println("working...")
doWork(something)
<-something.Done() // wait for it
log.Println("finished")
return nil