awesome-patterns/go_design_pattern_book/concurrent_pattern/future/main.go
2018-01-08 23:23:44 +10:00

36 lines
566 B
Go

package main
func main() {
}
type SuccessFunc func(string)
type FailFunc func(error)
type ExecuteStringFunc func() (string, error)
type MaybeString struct {
successFunc SuccessFunc
failFunc FailFunc
}
func (s *MaybeString) Success(f SuccessFunc) *MaybeString {
s.successFunc = f
return s
}
func (s *MaybeString) Fail(f FailFunc) *MaybeString {
s.failFunc = f
return s
}
func (s *MaybeString) Execute(f ExecuteStringFunc) {
go func(s *MaybeString) {
str, err := f()
if err != nil {
s.failFunc(err)
} else {
s.successFunc(str)
}
}(s)
}