Some (Nil Safety)
Have you ever seen this panic? Something happens when you access data from nil pointer.
panic: runtime error: invalid memory address or nil pointer dereference
Package some
will help you to prevent this problem by wrapping nilable value and way to access it.
Some
A struct that contains the value and state of existence. If there is value, state will be true.
Get
Value getter that returns the value and existence state. If you call this, you need to store two variables.
func Get() (YourType, bool)
Example:
product, ok := some.Some[Product]{}.Get()
if !ok {
fmt.Println("product is not found")
return
}
...
IsPresent
Check existence of value, returns true
if value exists.
func IsPresent() bool
Example:
ok := some.Some[Product]{}.IsPresent()
if !ok {
fmt.Println("product is not found")
return
}
...
OrElse
Get value with default value if not exists.
func OrElse(defaultValue) YourType
Example:
defaultValue := Product{Name: "terang bulan"}
product := some.Some[Product]{}.OrElse(defaultValue)
...
OrElseFunc
Get value but if not exists, it will run function to get default value.
func OrElseFunc(func() YourType) YourType
Example:
product := some.Some[Product]{}.OrElseFunc(func() Product {
return Product{Name: "terang bulan"}
})
...
OrPanic
Get value but if not exists, it will panic. You can put an error as parameter, it will be used as message.
func OrPanic(...error) YourType
Example:
product := some.Some[Product]{}.OrPanic()
...
IfPresent
Receive a callback function that will be called when the value is present.
func IfPresent(func(value YourType))
Example:
some.Some[Product]{}.IfPresent(func(product Product) {
...
})
Helpers
Functions that help you to use some
easily.
Empty
Create empty data with defined type.
some.Empty[YourType]()
Example:
product := some.Empty[Product]()
Of
Wrap data as nil safely. No need to define the data type here, because it will be automatically recognized by data given.
some.Of(yourData)
Example:
product := some.Of(Product{})