Files
lo/docs/data/core-emptyabletoptr.md
T
2025-10-06 17:15:49 +02:00

773 B

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
EmptyableToPtr emptyabletoptr type_manipulation.go#L41 core type
func EmptyableToPtr[T any](x T) *T
core#type#emptyabletoptr
core#type#toptr
core#type#fromptr
core#type#fromptror
core#type#tosliceptr
core#type#fromsliceptr
105

Returns a pointer to the provided value, or nil if the value is empty (zero value). This is useful for avoiding pointers to empty values.

ptr := lo.EmptyableToPtr("")
// nil (because empty string is zero value)

ptr = lo.EmptyableToPtr("hello")
// *string pointing to "hello"

ptr = lo.EmptyableToPtr(0)
// nil (because 0 is zero value for int)

ptr = lo.EmptyableToPtr(42)
// *int pointing to 42