Calling Get() on an empty optional leads to a nil pointer dereference. It seems like it would be okay to return a zero value with a non-nil error in the case that Get() fails. I'm willing to do this work if you approve of my suggested fix, I also understand if crashing is the desired behavior. In the case that you want the crash the Get() method doesn't seem like it needs to call Present().
package main
import (
"fmt"
"github.com/markphelps/optional"
)
func main() {
valOpt := optional.Int64{}
val, err := valOpt.Get() // crash here
if err == nil {
fmt.Println("Got expected nil err for empty optional")
}
fmt.Printf("Got value: '%d' from empty optional\n", val)
}
func (i Int64) Get() (int64, error) {
if !i.Present() {
var zero int64
return zero, errors.New("value not present")
}
return *i.value, nil
}