Hi everyone
I’m trying the new beta 1.18.1 with generics. I have a function that returns a generic type but can also return an error.
In case I return an error, i need to provide a tuple of a value of generic and an error. How can I create a generic value?
Here is my attempt:
type Option[T any] struct {
value T
isSome bool
}
// Val returns the value or default and error if represents null
func (o Option[T]) Val() (T, error) {
if !o.isSome {
return T{}, errors.New("Opt is none")
}
return o.value, nil
}