优雅的处理错误

package main

import (
    "errors"
    "fmt"
)

func main() {
    var i = 0
    _ = FnPipe(func() error {
        i++
        return nil
    }, func() error {
        i *= 2
        return nil
    }, FnWithErr(func() {
        fmt.Println(i)
    }), func() error {
        return errors.New("test error")
    }, FnWithErr(func() {
        fmt.Println("end")
    }))
}

func FnWithErr(f func()) func() error {
    return func() error {
        f()
        return nil
    }
}

func FnPipe(f ...func() error) error {
    for i := range f {
        if err := f[i](); err != nil {
            return err
        }
    }
    return nil
}

你可能感兴趣的:(优雅的处理错误)