generics/ptr.go

24 lines
441 B
Go

package generics
// Ptr returns pointer for given value.
func Ptr[T any](val T) *T {
return &val
}
// Val returns value of given pointer or default value if pointer is nil.
func Val[T any](ptr *T) T {
var val T
if ptr != nil {
val = *ptr
}
return val
}
// Fallback returns value of given pointer or fallback value if pointer is nil.
func Fallback[T any](ptr *T, fallback T) T {
if ptr != nil {
return *ptr
}
return fallback
}