go-opt/README.md

75 lines
1.4 KiB
Markdown
Raw Normal View History

2017-05-18 18:20:02 +00:00
# go-opt
2022-08-08 17:01:41 +00:00
Package **opt** implements an **optional-type**, for the Go programming language.
2017-05-18 18:20:02 +00:00
In other programming languages, an **optional-type** might be know as: a **option-type**, or a **maybe-type**.
2017-05-18 18:20:02 +00:00
## Documention
Online documentation, which includes examples, can be found at: http://godoc.org/sourcecode.social/reiver/go-opt
2017-05-18 18:20:02 +00:00
[![GoDoc](https://godoc.org/sourcecode.social/reiver/go-opt?status.svg)](https://godoc.org/sourcecode.social/reiver/go-opt)
2022-08-08 17:14:51 +00:00
## Examples
Here is an example **optional-type** that can hold a string:
```go
import "sourcecode.social/reiver/go-opt"
//
2022-08-08 17:14:51 +00:00
var op opt.Optional[string] // the default value is nothing.
// ...
if opt.Nothing[string]() == op {
fmt.Println("nothing")
}
// ...
2022-08-08 17:21:23 +00:00
op = opt.Something("Hello world! 👾")
// ...
2022-08-08 17:14:51 +00:00
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()
2022-08-20 15:11:47 +00:00
if found {
2022-08-08 17:14:51 +00:00
fmt.Println("VALUE:", value)
} else {
fmt.Println("nothing")
}
```
2023-09-28 10:21:19 +00:00
## Import
To import package **opt** use `import` code like the follownig:
```
import "sourcecode.social/reiver/go-opt"
```
## Installation
To install package **opt** do the following:
```
2023-10-27 18:33:08 +00:00
GOPROXY=direct go get https://sourcecode.social/reiver/go-opt
2023-09-28 10:21:19 +00:00
```
## Author
2024-07-04 16:44:29 +00:00
Package **opt** was written by [Charles Iliya Krempeaux](http://reiver.link)