generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpattern.go
36 lines (32 loc) · 903 Bytes
/
pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package u
// CombineFuncs create a chain of functions.
// This can be particularly useful for creating cleanup function progressively.
// It solves the infinite loop you can have when trying to do it manually: https://play.golang.org/p/NQem8UJ500t.
func CombineFuncs(left func(), right ...func()) func() {
return func() {
left()
for _, fn := range right {
fn()
}
}
}
// CheckErr panics if the passed error is not nil.
func CheckErr(err error) {
if err != nil {
panic(err)
}
}
// Future starts running the given function in background and return a chan that will return the result of the execution.
func Future(fn func() (interface{}, error)) <-chan FutureRet {
c := make(chan FutureRet, 1)
go func() {
ret, err := fn()
c <- FutureRet{Ret: ret, Err: err}
}()
return c
}
// FutureRet is a generic struct returned by Future.
type FutureRet struct {
Ret interface{}
Err error
}