github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/2-design-patterns/ch04-solid/02_maybe/src/maybe/string_option.go (about)

     1  package maybe
     2  
     3  import "fmt"
     4  
     5  type StringOption interface {
     6  	Option
     7  	Val() string
     8  }
     9  
    10  type optionalString struct {
    11  	exists bool
    12  	val    string
    13  }
    14  
    15  func EmptyString() StringOption {
    16  	return optionalString{exists: false}
    17  }
    18  
    19  func SomeString(val string) StringOption {
    20  	return optionalString{exists: true, val: val}
    21  }
    22  
    23  func (o optionalString) Empty() bool {
    24  	return !o.exists
    25  }
    26  
    27  func (o optionalString) Val() string {
    28  	return o.val
    29  }
    30  
    31  func (o optionalString) String() string {
    32  	if o.Empty() {
    33  		return "<EMPTY>"
    34  	}
    35  	return fmt.Sprintf("%s", o.val)
    36  }