Type manipulation - Core helpers
This page lists all type operations available in the core package of lo.
This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Returns a nil pointer of type.
lo.Nil[string]()// (*string)(nil)lo.Nil[int]()// (*int)(nil)Useful when you need a nil pointer of a specific type without declaring a variable first.
Prototype:func Nil[T any]() *TIsNilโ
Returns true if the input is nil or points to a nil value. This works with pointers, interfaces, maps, slices, channels, and functions.
result := lo.IsNil(nil)// truevar ptr *intresult = lo.IsNil(ptr)// trueresult = lo.IsNil(42)// falseresult = lo.IsNil("hello")// falsevar iface interface{}result = lo.IsNil(iface)// trueiface = 42result = lo.IsNil(iface)// falseSimilar:Prototype:func IsNil(x any) boolIsNotNilโ
Returns true if the input is not nil and does not point to a nil value. This works with pointers, interfaces, maps, slices, channels, and functions.
result := lo.IsNotNil(nil)// falsevar ptr *intresult = lo.IsNotNil(ptr)// falseresult = lo.IsNotNil(42)// trueresult = lo.IsNotNil("hello")// truevar iface interface{}result = lo.IsNotNil(iface)// falseiface = 42result = lo.IsNotNil(iface)// trueSimilar:Prototype:func IsNotNil(x any) boolToPtrโ
Returns a pointer to the provided value.
ptr := lo.ToPtr(42)// *int pointing to 42ptr = lo.ToPtr("hello")// *string pointing to "hello"ptr = lo.ToPtr([]int{1, 2, 3})// *[]int pointing to []int{1, 2, 3}Prototype:func ToPtr[T any](x T) *TFromPtrโ
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.
ptr := lo.ToPtr(42)value := lo.FromPtr(ptr)// value: 42value = lo.FromPtr[string](nil)// value: "" (zero value for string)value = lo.FromPtr[int](nil)// value: 0 (zero value for int)// Working with structstype Person struct {Name stringAge int}var personPtr *Personperson := lo.FromPtr(personPtr)// person: Person{Name: "", Age: 0} (zero value for Person)Prototype:func FromPtr[T any](x *T) TFromPtrOrโ
Returns the value pointed to by the pointer, or the fallback value if the pointer is nil.
ptr := lo.ToPtr(42)value := lo.FromPtrOr(ptr, 0)// 42value = lo.FromPtrOr[string](nil, "default")// "default"value = lo.FromPtrOr[int](nil, -1)// -1ptr = nilvalue = lo.FromPtrOr(ptr, 999)// 999Prototype:func FromPtrOr[T any](x *T, fallback T) TEmptyableToPtrโ
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 42Prototype:func EmptyableToPtr[T any](x T) *TToSlicePtrโ
Converts a slice of values to a slice of pointers to those values.
slice := []int{1, 2, 3}ptrs := lo.ToSlicePtr(slice)// []*int{&1, &2, &3}slice = []string{"a", "b", "c"}ptrs = lo.ToSlicePtr(slice)// []*string{&"a", &"b", &"c"}slice = []int{}ptrs = lo.ToSlicePtr(slice)// []*int{}Prototype:func ToSlicePtr[T any](collection []T) []*TFromSlicePtrโ
Converts a slice of pointers to a slice of values. Nil pointers are converted to zero values.
a, b, c := 1, 2, 3ptrs := []*int{&a, &b, &c}slice := lo.FromSlicePtr(ptrs)// []int{1, 2, 3}a, b = "hello", "world"ptrs = []*string{&a, nil, &b}slice = lo.FromSlicePtr(ptrs)// []string{"hello", "", "world"} (nil pointer becomes zero value)ptrs = []*int{}slice = lo.FromSlicePtr(ptrs)// []int{}Prototype:func FromSlicePtr[T any](collection []*T) []TEmptyโ
Returns the zero value for the specified type. This is useful when you need an empty value of a specific type.
result := lo.Empty[string]()// "" (zero value for string)result = lo.Empty[int]()// 0 (zero value for int)result = lo.Empty[[]int]()// []int{} (zero value for slice)result = lo.Empty[map[string]int]()// map[string]int{} (zero value for map)result = lo.Empty[*int]()// nil (zero value for pointer)Prototype:func Empty[T any]() TIsEmptyโ
Returns true if the value is empty (zero value) for comparable types. This works with strings, numbers, slices, maps, pointers, etc.
result := lo.IsEmpty("")// true (empty string)result = lo.IsEmpty("hello")// falseresult = lo.IsEmpty(0)// true (zero value for int)result = lo.IsEmpty(42)// falseresult = lo.IsEmpty([]int{})// true (empty slice)result = lo.IsEmpty([]int{1, 2, 3})// falseresult = lo.IsEmpty(map[string]int{})// true (empty map)var ptr *intresult = lo.IsEmpty(ptr)// true (nil pointer)Similar:Prototype:func IsEmpty[T comparable](v T) boolIsNotEmptyโ
Returns true if the value is not empty (not zero value) for comparable types. This is the opposite of IsEmpty.
result := lo.IsNotEmpty("")// false (empty string)result = lo.IsNotEmpty("hello")// trueresult = lo.IsNotEmpty(0)// false (zero value for int)result = lo.IsNotEmpty(42)// trueresult = lo.IsNotEmpty([]int{})// false (empty slice)result = lo.IsNotEmpty([]int{1, 2, 3})// trueresult = lo.IsNotEmpty(map[string]int{})// false (empty map)var ptr *intresult = lo.IsNotEmpty(ptr)// false (nil pointer)Prototype:func IsNotEmpty[T comparable](v T) boolToAnySliceโ
Converts a typed slice to a slice of empty interface values (any). This is useful when working with heterogeneous data.
ints := []int{1, 2, 3}result := lo.ToAnySlice(ints)// []any{1, 2, 3}strings := []string{"a", "b", "c"}result = lo.ToAnySlice(strings)// []any{"a", "b", "c"}custom := []struct{ Name string }{{Name: "Alice"}, {Name: "Bob"}}result = lo.ToAnySlice(custom)// []any{struct{ Name string }{Name: "Alice"}, struct{ Name string }{Name: "Bob"}}Prototype:func ToAnySlice[T any](collection []T) []anyFromAnySliceโ
Converts a slice of empty interface values back to a typed slice. Returns the converted slice and a boolean indicating success. All elements must be of the target type.
data := []any{1, 2, 3}result, ok := lo.FromAnySlice[int](data)// []int{1, 2, 3}, truedata = []any{"a", "b", "c"}result, ok = lo.FromAnySlice[string](data)// []string{"a", "b", "c"}, truedata = []any{1, "b", 3} // mixed typesresult, ok = lo.FromAnySlice[int](data)// []int{}, false (conversion failed due to string element)data = []any{} // empty sliceresult, ok = lo.FromAnySlice[int](data)// []int{}, true (empty slice always succeeds)Similar:Prototype:func FromAnySlice[T any](in []any) ([]T, bool)Coalesceโ
Returns the first non-zero value from the provided comparable arguments, with a boolean indicating if a non-zero value was found.
// With strings - returns first non-empty stringresult, ok := lo.Coalesce("", "foo", "bar")// result: "foo", ok: true// All zero values - returns zero value with falseresult, ok = lo.Coalesce("", "")// result: "", ok: false// With integers - zero is considered zero valueresult, ok = lo.Coalesce(0, 42, 100)// result: 42, ok: true// With floats - zero is considered zero valueresult, ok = lo.Coalesce(0.0, 3.14, 2.71)// result: 3.14, ok: true// With pointers - nil is zero value for pointer typesvar s *stringstr := "hello"result, ok = lo.Coalesce(nil, &str)// result: &str, ok: true// All nil pointersresult, ok = lo.Coalesce[*string](nil, nil, nil)// result: nil, ok: falsePrototype:func Coalesce[T comparable](values ...T) (T, bool)CoalesceOrEmptyโ
Returns the first non-zero value from the provided comparable arguments, or the zero value if all arguments are zero.
result := lo.CoalesceOrEmpty("", "foo", "bar")// "foo"result = lo.CoalesceOrEmpty("", "")// ""result = lo.CoalesceOrEmpty(0, 42, 100)// 42result = lo.CoalesceOrEmpty(0.0, 0.0, 0.0)// 0.0Prototype:func CoalesceOrEmpty[T comparable](v ...T) TCoalesceSliceโ
Returns the first non-empty slice from the provided arguments, with a boolean indicating if a non-empty slice was found.
result, ok := lo.CoalesceSlice([]int{}, []int{1, 2, 3}, []int{4, 5})// []int{1, 2, 3}, trueresult, ok = lo.CoalesceSlice([]int{}, []int{})// []int{}, falseresult, ok = lo.CoalesceSlice([]string{}, []string{"a", "b"}, []string{"c"})// []string{"a", "b"}, truePrototype:func CoalesceSlice[T any](v ...[]T) ([]T, bool)CoalesceSliceOrEmptyโ
Returns the first non-empty slice from the provided arguments, or an empty slice if all arguments are empty.
result := lo.CoalesceSliceOrEmpty([]int{}, []int{1, 2, 3}, []int{4, 5})// []int{1, 2, 3}result = lo.CoalesceSliceOrEmpty([]int{}, []int{})// []int{}result = lo.CoalesceSliceOrEmpty([]string{}, []string{"a", "b"}, []string{"c"})// []string{"a", "b"}Prototype:func CoalesceSliceOrEmpty[T any](v ...[]T) []TCoalesceMapโ
Returns the first non-empty map from the provided arguments, with a boolean indicating if a non-empty map was found.
result, ok := lo.CoalesceMap(map[string]int{}, map[string]int{"a": 1}, map[string]int{"b": 2})// map[string]int{"a": 1}, trueresult, ok = lo.CoalesceMap(map[string]int{}, map[string]int{})// map[string]int{}, falseresult, ok = lo.CoalesceMap(map[int]string{}, map[int]string{1: "one"}, map[int]string{2: "two"})// map[int]string{1: "one"}, truePrototype:func CoalesceMap[K comparable, V any](v ...map[K]V) (map[K]V, bool)CoalesceMapOrEmptyโ
Returns the first non-empty map from the provided arguments, or an empty map if all arguments are empty.
result := lo.CoalesceMapOrEmpty(map[string]int{}, map[string]int{"a": 1}, map[string]int{"b": 2})// map[string]int{"a": 1}result = lo.CoalesceMapOrEmpty(map[string]int{}, map[string]int{})// map[string]int{}result = lo.CoalesceMapOrEmpty(map[int]string{}, map[int]string{1: "one"}, map[int]string{2: "two"})// map[int]string{1: "one"}Prototype:func CoalesceMapOrEmpty[K comparable, V any](v ...map[K]V) map[K]V