github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/2-design-patterns/ch04-solid/02_maybe/src/maybe/either_error.go (about) 1 package maybe 2 3 import "fmt" 4 5 type Either interface { 6 SuccessOrFailure 7 Succeeded() StringOption 8 Failed() ErrorOption 9 } 10 11 type either struct { 12 val string 13 err error 14 } 15 16 func EitherSuccessError(val string) Either { 17 return either{val: val} 18 } 19 20 func EitherFailureError(err error) Either { 21 return either{err: err} 22 } 23 24 func (e either) Success() bool { 25 return e.err == nil 26 } 27 28 func (e either) Failure() bool { 29 return e.err != nil 30 } 31 32 func (e either) Succeeded() StringOption { 33 if e.err == nil { 34 return SomeString(e.val) 35 } 36 return EmptyString() 37 } 38 39 func (e either) Failed() ErrorOption { 40 if e.err != nil { 41 return SomeError(e.err) 42 } 43 return EmptyError() 44 } 45 46 func (e either) String() string { 47 if e.Success() { 48 return fmt.Sprintf("Succeeded(%s)", e.val) 49 } 50 return fmt.Sprintf("Failed(%s)", e.err) 51 }