From 9cbd049b34fc59072aaaff53ade85bed49ce4c60 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Mon, 8 Aug 2022 10:14:51 -0700 Subject: [PATCH] opt.Optional[T] --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index 989b945..872849f 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,40 @@ In other programming languages, an **optional-type** might be know as: a **optio Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-opt [![GoDoc](https://godoc.org/github.com/reiver/go-opt?status.svg)](https://godoc.org/github.com/reiver/go-opt) + +## Examples + +Here is an example **optional-type** that can hold a string: +```go +var op opt.Optional[string] // the default value is nothing. + +// ... + +if opt.Nothing[string]() == op { + fmt.Println("nothing") +} + +// ... + +switch op { +case op.Nothing[string](): + //@TODO +case op.Something("apple"): + //@TODO +case op.Something("banana"): + //@TODO +case op.Something("cherry"): + //@TODO +default: + //@TODO +} + +// ... + +value, found := op.Get() +if { + fmt.Println("VALUE:", value) +} else { + fmt.Println("nothing") +} +```