github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/collect/collect.go (about) 1 // Copyright 2015 The Vanadium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package collect 6 7 // Error provides a mechanism for collecting errors from deferred 8 // functions. The mechanism executes all deferred functions 9 // irrespective of their return value, returning the first error it 10 // encounters or nil if no error is encountered. 11 func Error(fn func() error, err *error) { 12 if *err != nil { 13 fn() 14 } else { 15 *err = fn() 16 } 17 } 18 19 // Errors provides a mechanism for collecting errors from deferred 20 // functions. The mechanism executes all deferred functions 21 // irrespective of their return value, collecting all the errors it 22 // encounters. 23 func Errors(fn func() error, errs *[]error) { 24 if err := fn(); err != nil { 25 *errs = append(*errs, err) 26 } 27 }