First we define the type, hiding the pointer/non-existent value:
type Optional[Value any] struct { value Value exists bool }
func (o Optional[Value]) Get() (Value, bool) { return o.value, o.exists }
if value, ok := optional.Get(); ok { // value is valid } // value is invalid
Here's a full implementation I wrote a while back: https://gist.github.com/MawrBF2/0a60da26f66b82ee87b98b03336e...
First we define the type, hiding the pointer/non-existent value:
Then we expose it through a method: Accessing the value then has to look like this: This forces us to handle the nil/optional code path.Here's a full implementation I wrote a while back: https://gist.github.com/MawrBF2/0a60da26f66b82ee87b98b03336e...