Back to Lo

Core Fromptr

docs/data/core-fromptr.md

1.53.0593 B
Original Source

Dereferences a pointer and returns the underlying value. If the pointer is nil, returns the zero value for the type. This is a safe way to extract values from optional pointers without risking panics.

go
ptr := lo.ToPtr(42)
value := lo.FromPtr(ptr)
// value: 42

value = lo.FromPtr[string](nil)
// value: "" (zero value for string)

value = lo.FromPtr[int](nil)
// value: 0 (zero value for int)

// Working with structs
type Person struct {
    Name string
    Age  int
}
var personPtr *Person
person := lo.FromPtr(personPtr)
// person: Person{Name: "", Age: 0} (zero value for Person)