This is sort of the opposite of ToPtr(). It dereferences a pointer to its base value or returns the default value for the type if the pointer is nil.
15 lines
260 B
Go
15 lines
260 B
Go
package common
|
|
|
|
func ToPtr[T any](x T) *T {
|
|
return &x
|
|
}
|
|
|
|
// DerefOrDefault returns the dereferenced value of the given pointer or the
|
|
// default value for the type if unset.
|
|
func DerefOrDefault[T any](p *T) T {
|
|
var v T
|
|
if p != nil {
|
|
v = *p
|
|
}
|
|
return v
|
|
}
|