Skip to main content
Help improve this documentation

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!

Core - Type manipulation helpersโ€‹

This page lists all type operations available in the core package of lo.

  • 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]() *T
  • 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)
    // true

    var ptr *int
    result = lo.IsNil(ptr)
    // true

    result = lo.IsNil(42)
    // false

    result = lo.IsNil("hello")
    // false

    var iface interface{}
    result = lo.IsNil(iface)
    // true

    iface = 42
    result = lo.IsNil(iface)
    // false
    Prototype:
    func IsNil(x any) bool
  • 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)
    // false

    var ptr *int
    result = lo.IsNotNil(ptr)
    // false

    result = lo.IsNotNil(42)
    // true

    result = lo.IsNotNil("hello")
    // true

    var iface interface{}
    result = lo.IsNotNil(iface)
    // false

    iface = 42
    result = lo.IsNotNil(iface)
    // true
    Prototype:
    func IsNotNil(x any) bool
  • Returns a pointer to the provided value.

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

    ptr = 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) *T
  • 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: 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)
    Prototype:
    func FromPtr[T any](x *T) T
  • 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)
    // 42

    value = lo.FromPtrOr[string](nil, "default")
    // "default"

    value = lo.FromPtrOr[int](nil, -1)
    // -1

    ptr = nil
    value = lo.FromPtrOr(ptr, 999)
    // 999
    Prototype:
    func FromPtrOr[T any](x *T, fallback T) T
  • 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
    Prototype:
    func EmptyableToPtr[T any](x T) *T
  • 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) []*T
  • Converts a slice of pointers to a slice of values. Nil pointers are converted to zero values.

    a, b, c := 1, 2, 3
    ptrs := []*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) []T
  • 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]() T
  • 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")
    // false

    result = lo.IsEmpty(0)
    // true (zero value for int)

    result = lo.IsEmpty(42)
    // false

    result = lo.IsEmpty([]int{})
    // true (empty slice)

    result = lo.IsEmpty([]int{1, 2, 3})
    // false

    result = lo.IsEmpty(map[string]int{})
    // true (empty map)

    var ptr *int
    result = lo.IsEmpty(ptr)
    // true (nil pointer)
    Prototype:
    func IsEmpty[T comparable](v T) bool
  • 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")
    // true

    result = lo.IsNotEmpty(0)
    // false (zero value for int)

    result = lo.IsNotEmpty(42)
    // true

    result = lo.IsNotEmpty([]int{})
    // false (empty slice)

    result = lo.IsNotEmpty([]int{1, 2, 3})
    // true

    result = lo.IsNotEmpty(map[string]int{})
    // false (empty map)

    var ptr *int
    result = lo.IsNotEmpty(ptr)
    // false (nil pointer)
    Prototype:
    func IsNotEmpty[T comparable](v T) bool
  • 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) []any
  • 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}, true

    data = []any{"a", "b", "c"}
    result, ok = lo.FromAnySlice[string](data)
    // []string{"a", "b", "c"}, true

    data = []any{1, "b", 3} // mixed types
    result, ok = lo.FromAnySlice[int](data)
    // []int{}, false (conversion failed due to string element)

    data = []any{} // empty slice
    result, ok = lo.FromAnySlice[int](data)
    // []int{}, true (empty slice always succeeds)
    Prototype:
    func FromAnySlice[T any](in []any) ([]T, bool)
  • 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 string
    result, ok := lo.Coalesce("", "foo", "bar")
    // result: "foo", ok: true

    // All zero values - returns zero value with false
    result, ok = lo.Coalesce("", "")
    // result: "", ok: false

    // With integers - zero is considered zero value
    result, ok = lo.Coalesce(0, 42, 100)
    // result: 42, ok: true

    // With floats - zero is considered zero value
    result, ok = lo.Coalesce(0.0, 3.14, 2.71)
    // result: 3.14, ok: true

    // With pointers - nil is zero value for pointer types
    var s *string
    str := "hello"
    result, ok = lo.Coalesce(nil, &str)
    // result: &str, ok: true

    // All nil pointers
    result, ok = lo.Coalesce[*string](nil, nil, nil)
    // result: nil, ok: false
    Prototype:
    func Coalesce[T comparable](values ...T) (T, bool)
  • 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)
    // 42

    result = lo.CoalesceOrEmpty(0.0, 0.0, 0.0)
    // 0.0
    Prototype:
    func CoalesceOrEmpty[T comparable](v ...T) T
  • 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}, true

    result, ok = lo.CoalesceSlice([]int{}, []int{})
    // []int{}, false

    result, ok = lo.CoalesceSlice([]string{}, []string{"a", "b"}, []string{"c"})
    // []string{"a", "b"}, true
    Prototype:
    func CoalesceSlice[T any](v ...[]T) ([]T, bool)
  • 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) []T
  • 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}, true

    result, ok = lo.CoalesceMap(map[string]int{}, map[string]int{})
    // map[string]int{}, false

    result, ok = lo.CoalesceMap(map[int]string{}, map[int]string{1: "one"}, map[int]string{2: "two"})
    // map[int]string{1: "one"}, true
    Prototype:
    func CoalesceMap[K comparable, V any](v ...map[K]V) (map[K]V, bool)
  • 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