看了下 Go 2 引入了 check & handle
https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md#draft-design```
func CopyFile(src, dst string) error {
handle err {
return fmt.Errorf("copy %s %s: %v", src, dst, err)
}
r := check os.Open(src)
defer r.Close()
w := check os.Create(dst)
handle err {
w.Close()
os.Remove(dst) // (only if a check fails)
}
check io.Copy(w, r)
check w.Close()
return nil
}
```