types/opt: add (Value[T]).GetOr(def T) T method

Updates #12736

Signed-off-by: Nick Khyl <nickk@tailscale.com>
pull/12081/head^2
Nick Khyl 4 months ago committed by Nick Khyl
parent 20562a4fb9
commit bd54b61746

@ -64,6 +64,14 @@ func (o Value[T]) Get() T {
return o.value return o.value
} }
// GetOr returns the value of o or def if a value hasn't been set.
func (o Value[T]) GetOr(def T) T {
if o.set {
return o.value
}
return def
}
// Get returns the value and a flag indicating whether the value is set. // Get returns the value and a flag indicating whether the value is set.
func (o Value[T]) GetOk() (v T, ok bool) { func (o Value[T]) GetOk() (v T, ok bool) {
return o.value, o.set return o.value, o.set

Loading…
Cancel
Save