Back to Lo

Core Emptyabletoptr

docs/data/core-emptyabletoptr.md

1.53.0407 B
Original Source

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.

go
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