lo

package module
v1.52.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2025 License: MIT Imports: 18 Imported by: 10,978

README

lo - Iterate over slices, maps, channels...

tag Go Version GoDoc Build Status Go report Coverage Contributors License

samber/lo is a Lodash-style Go library based on Go 1.18+ Generics.

A utility library based on Go 1.18+ generics that makes it easier to work with slices, maps, strings, channels, and functions. It provides dozens of handy methods to simplify common coding tasks and improve code readability. It may look like Lodash in some aspects.

5 to 10 helpers may overlap with those from the Go standard library, in packages slices and maps. I feel this library is legitimate and offers many more valuable abstractions.

See also:

  • samber/do: A dependency injection toolkit based on Go 1.18+ Generics
  • samber/mo: Monads based on Go 1.18+ Generics (Option, Result, Either...)

💖 Support This Project

I’m going all-in on open-source for the coming months.
Help sustain development: Become an individual sponsor or join as a corporate sponsor.


Why this name?

I wanted a short name, similar to "Lodash", and no Go package uses this name.

lo

🚀 Install

go get github.com/samber/lo@v1

This library is v1 and follows SemVer strictly.

No breaking changes will be made to exported APIs before v2.0.0.

This library has no dependencies outside the Go standard library.

💡 Usage

You can import lo using:

import (
    "github.com/samber/lo"
    lop "github.com/samber/lo/parallel"
    lom "github.com/samber/lo/mutable"
    loi "github.com/samber/lo/it"
)

Then use one of the helpers below:

names := lo.Uniq([]string{"Samuel", "John", "Samuel"})
// []string{"Samuel", "John"}
Tips for lazy developers

I cannot recommend it, but in case you are too lazy for repeating lo. everywhere, you can import the entire library into the namespace.

import (
    . "github.com/samber/lo"
)

I take no responsibility for this junk. 😁 💩

🤠 Spec

GoDoc: godoc.org/github.com/samber/lo

Documentation: lo.samber.dev

Supported helpers for slices:

Supported helpers for maps:

Supported math helpers:

Supported helpers for strings:

Supported helpers for tuples:

Supported helpers for time and duration:

Supported helpers for channels:

Supported intersection helpers:

Supported search helpers:

Conditional helpers:

Type manipulation helpers:

Function helpers:

Concurrency helpers:

Error handling:

Constraints:

  • Clonable
Filter

Iterates over a collection and returns a slice of all the elements the predicate function returns true for.

even := lo.Filter([]int{1, 2, 3, 4}, func(x int, index int) bool {
    return x%2 == 0
})
// []int{2, 4}

[play]

Mutable: like lo.Filter(), but the slice is updated in place.

import lom "github.com/samber/lo/mutable"

list := []int{1, 2, 3, 4}
newList := lom.Filter(list, func(x int) bool {
    return x%2 == 0
})

list
// []int{2, 4, 3, 4}

newList
// []int{2, 4}
Map

Manipulates a slice of one type and transforms it into a slice of another type:

import "github.com/samber/lo"

lo.Map([]int64{1, 2, 3, 4}, func(x int64, index int) string {
    return strconv.FormatInt(x, 10)
})
// []string{"1", "2", "3", "4"}

[play]

Parallel processing: like lo.Map(), but the mapper function is called in a goroutine. Results are returned in the same order.

import lop "github.com/samber/lo/parallel"

lop.Map([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
    return strconv.FormatInt(x, 10)
})
// []string{"1", "2", "3", "4"}

[play]

Mutable: like lo.Map(), but the slice is updated in place.

import lom "github.com/samber/lo/mutable"

list := []int{1, 2, 3, 4}
lom.Map(list, func(x int) int {
    return x*2
})
// []int{2, 4, 6, 8}

[play]

UniqMap

Manipulates a slice and transforms it to a slice of another type with unique values.

type User struct {
    Name string
    Age  int
}
users := []User{{Name: "Alex", Age: 10}, {Name: "Alex", Age: 12}, {Name: "Bob", Age: 11}, {Name: "Alice", Age: 20}}

names := lo.UniqMap(users, func(u User, index int) string {
    return u.Name
})
// []string{"Alex", "Bob", "Alice"}

[play]

FilterMap

Returns a slice obtained after both filtering and mapping using the given callback function.

The callback function should return two values: the result of the mapping operation and whether the result element should be included or not.

matching := lo.FilterMap([]string{"cpu", "gpu", "mouse", "keyboard"}, func(x string, _ int) (string, bool) {
    if strings.HasSuffix(x, "pu") {
        return "xpu", true
    }
    return "", false
})
// []string{"xpu", "xpu"}

[play]

FlatMap

Manipulates a slice and transforms and flattens it to a slice of another type. The transform function can either return a slice or a nil, and in the nil case no value is added to the final slice.

lo.FlatMap([]int64{0, 1, 2}, func(x int64, _ int) []string {
    return []string{
        strconv.FormatInt(x, 10),
        strconv.FormatInt(x, 10),
    }
})
// []string{"0", "0", "1", "1", "2", "2"}

[play]

Reduce

Reduces a collection to a single value. The value is calculated by accumulating the result of running each element in the collection through an accumulator function. Each successive invocation is supplied with the return value returned by the previous call.

sum := lo.Reduce([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
    return agg + item
}, 0)
// 10

[play]

ReduceRight

Like lo.Reduce except that it iterates over elements of collection from right to left.

result := lo.ReduceRight([][]int{{0, 1}, {2, 3}, {4, 5}}, func(agg []int, item []int, _ int) []int {
    return append(agg, item...)
}, []int{})
// []int{4, 5, 2, 3, 0, 1}

[play]

ForEach

Iterates over elements of a collection and invokes the function over each element.

import "github.com/samber/lo"

lo.ForEach([]string{"hello", "world"}, func(x string, _ int) {
    println(x)
})
// prints "hello\nworld\n"

[play]

Parallel processing: like lo.ForEach(), but the callback is called as a goroutine.

import lop "github.com/samber/lo/parallel"

lop.ForEach([]string{"hello", "world"}, func(x string, _ int) {
    println(x)
})
// prints "hello\nworld\n" or "world\nhello\n"
ForEachWhile

Iterates over collection elements and invokes iteratee for each element collection return value decide to continue or break, like do while().

list := []int64{1, 2, -42, 4}

lo.ForEachWhile(list, func(x int64, _ int) bool {
	if x < 0 {
		return false
	}
	fmt.Println(x)
	return true
})
// 1
// 2

[play]

Times

Times invokes the iteratee n times, returning a slice of the results of each invocation. The iteratee is invoked with index as argument.

import "github.com/samber/lo"

lo.Times(3, func(i int) string {
    return strconv.FormatInt(int64(i), 10)
})
// []string{"0", "1", "2"}

[play]

Parallel processing: like lo.Times(), but callback is called in goroutine.

import lop "github.com/samber/lo/parallel"

lop.Times(3, func(i int) string {
    return strconv.FormatInt(int64(i), 10)
})
// []string{"0", "1", "2"}
Uniq

Returns a duplicate-free version of a slice, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the slice.

uniqValues := lo.Uniq([]int{1, 2, 2, 1})
// []int{1, 2}

[play]

UniqBy

Returns a duplicate-free version of a slice, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the slice. It accepts iteratee which is invoked for each element in the slice to generate the criterion by which uniqueness is computed.

uniqValues := lo.UniqBy([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
    return i%3
})
// []int{0, 1, 2}

[play]

GroupBy

Returns an object composed of keys generated from the results of running each element of collection through iteratee.

import lo "github.com/samber/lo"

groups := lo.GroupBy([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
    return i%3
})
// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}

[play]

Parallel processing: like lo.GroupBy(), but callback is called in goroutine.

import lop "github.com/samber/lo/parallel"

lop.GroupBy([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
    return i%3
})
// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}
GroupByMap

Returns an object composed of keys generated from the results of running each element of collection through iteratee.

import lo "github.com/samber/lo"

groups := lo.GroupByMap([]int{0, 1, 2, 3, 4, 5}, func(i int) (int, int) {
    return i%3, i*2
})
// map[int][]int{0: []int{0, 6}, 1: []int{2, 8}, 2: []int{4, 10}}

[play]

Chunk

Returns a slice of elements split into groups of length size. If the slice can't be split evenly, the final chunk will be the remaining elements.

lo.Chunk([]int{0, 1, 2, 3, 4, 5}, 2)
// [][]int{{0, 1}, {2, 3}, {4, 5}}

lo.Chunk([]int{0, 1, 2, 3, 4, 5, 6}, 2)
// [][]int{{0, 1}, {2, 3}, {4, 5}, {6}}

lo.Chunk([]int{}, 2)
// [][]int{}

lo.Chunk([]int{0}, 2)
// [][]int{{0}}

[play]

PartitionBy

Returns a slice of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee.

import lo "github.com/samber/lo"

partitions := lo.PartitionBy([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string {
    if x < 0 {
        return "negative"
    } else if x%2 == 0 {
        return "even"
    }
    return "odd"
})
// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}

[play]

Parallel processing: like lo.PartitionBy(), but callback is called in goroutine. Results are returned in the same order.

import lop "github.com/samber/lo/parallel"

partitions := lop.PartitionBy([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string {
    if x < 0 {
        return "negative"
    } else if x%2 == 0 {
        return "even"
    }
    return "odd"
})
// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}
Flatten

Returns a slice a single level deep.

flat := lo.Flatten([][]int{{0, 1}, {2, 3, 4, 5}})
// []int{0, 1, 2, 3, 4, 5}

[play]

Interleave

Round-robin alternating input slices and sequentially appending value at index into result.

interleaved := lo.Interleave([]int{1, 4, 7}, []int{2, 5, 8}, []int{3, 6, 9})
// []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

interleaved := lo.Interleave([]int{1}, []int{2, 5, 8}, []int{3, 6}, []int{4, 7, 9, 10})
// []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

[play]

Shuffle

Returns a slice of shuffled values. Uses the Fisher-Yates shuffle algorithm.

⚠️ This helper is mutable.

import lom "github.com/samber/lo/mutable"

list := []int{0, 1, 2, 3, 4, 5}
lom.Shuffle(list)

list
// []int{1, 4, 0, 3, 5, 2}

[play]

Reverse

Reverses a slice so that the first element becomes the last, the second element becomes the second to last, and so on.

⚠️ This helper is mutable.

import lom "github.com/samber/lo/mutable"

list := []int{0, 1, 2, 3, 4, 5}
lom.Reverse(list)

list
// []int{5, 4, 3, 2, 1, 0}

[play]

Fill

Fills elements of a slice with initial value.

type foo struct {
  bar string
}

func (f foo) Clone() foo {
  return foo{f.bar}
}

initializedSlice := lo.Fill([]foo{foo{"a"}, foo{"a"}}, foo{"b"})
// []foo{foo{"b"}, foo{"b"}}

[play]

Repeat

Builds a slice with N copies of initial value.

type foo struct {
  bar string
}

func (f foo) Clone() foo {
  return foo{f.bar}
}

slice := lo.Repeat(2, foo{"a"})
// []foo{foo{"a"}, foo{"a"}}

[play]

RepeatBy

Builds a slice with values returned by N calls of callback.

slice := lo.RepeatBy(0, func (i int) string {
    return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})
// []string{}

slice := lo.RepeatBy(5, func(i int) string {
    return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})
// []string{"0", "1", "4", "9", "16"}

[play]

KeyBy

Transforms a slice or a slice of structs to a map based on a pivot callback.

m := lo.KeyBy([]string{"a", "aa", "aaa"}, func(str string) int {
    return len(str)
})
// map[int]string{1: "a", 2: "aa", 3: "aaa"}

type Character struct {
  dir  string
  code int
}
characters := []Character{
    {dir: "left", code: 97},
    {dir: "right", code: 100},
}
result := lo.KeyBy(characters, func(char Character) string {
    return string(rune(char.code))
})
//map[a:{dir:left code:97} d:{dir:right code:100}]

[play]

SliceToMap (alias: Associate)

Returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs have the same key the last one gets added to the map.

The order of keys in returned map is not specified and is not guaranteed to be the same from the original slice.

in := []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}}

aMap := lo.SliceToMap(in, func (f *foo) (string, int) {
    return f.baz, f.bar
})
// map[string][int]{ "apple":1, "banana":2 }

[play]

FilterSliceToMap

Returns a map containing key-value pairs provided by transform function applied to elements of the given slice.

If any of two pairs have the same key the last one gets added to the map.

The order of keys in returned map is not specified and is not guaranteed to be the same from the original slice.

The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map.

list := []string{"a", "aa", "aaa"}

result := lo.FilterSliceToMap(list, func(str string) (string, int, bool) {
    return str, len(str), len(str) > 1
})
// map[string][int]{"aa":2 "aaa":3}

[play]

Keyify

Returns a map with each unique element of the slice as a key.

set := lo.Keyify([]int{1, 1, 2, 3, 4})
// map[int]struct{}{1:{}, 2:{}, 3:{}, 4:{}}

[play]

Drop

Drops n elements from the beginning of a slice.

l := lo.Drop([]int{0, 1, 2, 3, 4, 5}, 2)
// []int{2, 3, 4, 5}

[play]

DropRight

Drops n elements from the end of a slice.

l := lo.DropRight([]int{0, 1, 2, 3, 4, 5}, 2)
// []int{0, 1, 2, 3}

[play]

DropWhile

Drop elements from the beginning of a slice while the predicate returns true.

l := lo.DropWhile([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
    return len(val) <= 2
})
// []string{"aaa", "aa", "aa"}

[play]

DropRightWhile

Drop elements from the end of a slice while the predicate returns true.

l := lo.DropRightWhile([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
    return len(val) <= 2
})
// []string{"a", "aa", "aaa"}

[play]

DropByIndex

Drops elements from a slice by the index. A negative index will drop elements from the end of the slice.

l := lo.DropByIndex([]int{0, 1, 2, 3, 4, 5}, 2, 4, -1)
// []int{0, 1, 3}

[play]

Reject

The opposite of Filter, this method returns the elements of collection that predicate does not return true for.

odd := lo.Reject([]int{1, 2, 3, 4}, func(x int, _ int) bool {
    return x%2 == 0
})
// []int{1, 3}

[play]

RejectMap

The opposite of FilterMap, this method returns a slice obtained after both filtering and mapping using the given callback function.

The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.
items := lo.RejectMap([]int{1, 2, 3, 4}, func(x int, _ int) (int, bool) {
    return x*10, x%2 == 0
})
// []int{10, 30}
FilterReject

Mixes Filter and Reject, this method returns two slices, one for the elements of collection that predicate returns true for and one for the elements that predicate does not return true for.

kept, rejected := lo.FilterReject([]int{1, 2, 3, 4}, func(x int, _ int) bool {
    return x%2 == 0
})
// []int{2, 4}
// []int{1, 3}
Count

Counts the number of elements in the collection that equal value.

count := lo.Count([]int{1, 5, 1}, 1)
// 2

[play]

CountBy

Counts the number of elements in the collection for which predicate is true.

count := lo.CountBy([]int{1, 5, 1}, func(i int) bool {
    return i < 4
})
// 2

[play]

CountValues

Counts the number of each element in the collection.

lo.CountValues([]int{})
// map[int]int{}

lo.CountValues([]int{1, 2})
// map[int]int{1: 1, 2: 1}

lo.CountValues([]int{1, 2, 2})
// map[int]int{1: 1, 2: 2}

lo.CountValues([]string{"foo", "bar", ""})
// map[string]int{"": 1, "foo": 1, "bar": 1}

lo.CountValues([]string{"foo", "bar", "bar"})
// map[string]int{"foo": 1, "bar": 2}

[play]

CountValuesBy

Counts the number of each element in the collection. It is equivalent to chaining lo.Map and lo.CountValues.

isEven := func(v int) bool {
    return v%2==0
}

lo.CountValuesBy([]int{}, isEven)
// map[bool]int{}

lo.CountValuesBy([]int{1, 2}, isEven)
// map[bool]int{false: 1, true: 1}

lo.CountValuesBy([]int{1, 2, 2}, isEven)
// map[bool]int{false: 1, true: 2}

length := func(v string) int {
    return len(v)
}

lo.CountValuesBy([]string{"foo", "bar", ""}, length)
// map[int]int{0: 1, 3: 2}

lo.CountValuesBy([]string{"foo", "bar", "bar"}, length)
// map[int]int{3: 3}

[play]

Subset

Returns a copy of a slice from offset up to length elements. Like slice[start:start+length], but does not panic on overflow.

in := []int{0, 1, 2, 3, 4}

sub := lo.Subset(in, 2, 3)
// []int{2, 3, 4}

sub := lo.Subset(in, -4, 3)
// []int{1, 2, 3}

sub := lo.Subset(in, -2, math.MaxUint)
// []int{3, 4}

[play]

Slice

Returns a copy of a slice from start up to, but not including end. Like slice[start:end], but does not panic on overflow.

in := []int{0, 1, 2, 3, 4}

slice := lo.Slice(in, 0, 5)
// []int{0, 1, 2, 3, 4}

slice := lo.Slice(in, 2, 3)
// []int{2}

slice := lo.Slice(in, 2, 6)
// []int{2, 3, 4}

slice := lo.Slice(in, 4, 3)
// []int{}

[play]

Replace

Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.

in := []int{0, 1, 0, 1, 2, 3, 0}

slice := lo.Replace(in, 0, 42, 1)
// []int{42, 1, 0, 1, 2, 3, 0}

slice := lo.Replace(in, -1, 42, 1)
// []int{0, 1, 0, 1, 2, 3, 0}

slice := lo.Replace(in, 0, 42, 2)
// []int{42, 1, 42, 1, 2, 3, 0}

slice := lo.Replace(in, 0, 42, -1)
// []int{42, 1, 42, 1, 2, 3, 42}

[play]

ReplaceAll

Returns a copy of the slice with all non-overlapping instances of old replaced by new.

in := []int{0, 1, 0, 1, 2, 3, 0}

slice := lo.ReplaceAll(in, 0, 42)
// []int{42, 1, 42, 1, 2, 3, 42}

slice := lo.ReplaceAll(in, -1, 42)
// []int{0, 1, 0, 1, 2, 3, 0}

[play]

Compact

Returns a slice of all non-zero elements.

in := []string{"", "foo", "", "bar", ""}

slice := lo.Compact(in)
// []string{"foo", "bar"}

[play]

IsSorted

Checks if a slice is sorted.

slice := lo.IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
// true

[play]

IsSortedByKey

Checks if a slice is sorted by iteratee.

slice := lo.IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
    return len(s)
})
// true

[play]

Splice

Splice inserts multiple elements at index i. A negative index counts back from the end of the slice. The helper is protected against overflow errors.

result := lo.Splice([]string{"a", "b"}, 1, "1", "2")
// []string{"a", "1", "2", "b"}

// negative
result = lo.Splice([]string{"a", "b"}, -1, "1", "2")
// []string{"a", "1", "2", "b"}

// overflow
result = lo.Splice([]string{"a", "b"}, 42, "1", "2")
// []string{"a", "b", "1", "2"}

[play]

Cut

Slices collection around the first instance of separator, returning the part of collection before and after separator. The found result reports whether separator appears in collection. If separator does not appear in s, cut returns collection, empty slice of []T, false.

actualLeft, actualRight, result = lo.Cut([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{"b", "c", "d"})
// actualLeft: []string{"a"}
// actualRight: []string{"e", "f", "g"}
// result: true

result = lo.Cut([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{"z"})
// actualLeft: []string{"a", "b", "c", "d", "e", "f", "g"}
// actualRight: []string{}
// result: false

result = lo.Cut([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{"a", "b"})
// actualLeft: []string{}
// actualRight: []string{"c", "d", "e", "f", "g"}
// result: true

[play]

CutPrefix

Returns collection without the provided leading prefix []T and reports whether it found the prefix. If s doesn't start with prefix, CutPrefix returns collection, false. If prefix is the empty []T, CutPrefix returns collection, true.

actualRight, result = lo.CutPrefix([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{"a", "b", "c"})
// actualRight: []string{"d", "e", "f", "g"}
// result: true

result = lo.CutPrefix([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{"b"})
// actualRight: []string{"a", "b", "c", "d", "e", "f", "g"}
// result: false

result = lo.CutPrefix([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{})
// actualRight: []string{"a", "b", "c", "d", "e", "f", "g"}
// result: true

[play]

CutSuffix

Returns collection without the provided ending suffix []T and reports whether it found the suffix. If it doesn't end with suffix, CutSuffix returns collection, false. If suffix is the empty []T, CutSuffix returns collection, true.

actualLeft, result = lo.CutSuffix([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{"f", "g"})
// actualLeft: []string{"a", "b", "c", "d", "e"}
// result: true

actualLeft, result = lo.CutSuffix([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{"b"})
// actualLeft: []string{"a", "b", "c", "d", "e", "f", "g"}
// result: false

actualLeft, result = lo.CutSuffix([]string{"a", "b", "c", "d", "e", "f", "g"}, []string{})
// actualLeft: []string{"a", "b", "c", "d", "e", "f", "g"}
// result: true

[play]

Trim

Removes all the leading and trailing cutset from the collection.

result := lo.Trim([]int{0, 1, 2, 0, 3, 0}, []int{1, 0})
// []int{2, 0, 3}

result := lo.Trim([]string{"hello", "world", " "}, []string{" ", ""})
// []string{"hello", "world"}

[play]

TrimLeft

Removes all the leading cutset from the collection.

result := lo.TrimLeft([]int{0, 1, 2, 0, 3, 0}, []int{1, 0})
// []int{2, 0, 3, 0}

result := lo.TrimLeft([]string{"hello", "world", " "}, []string{" ", ""})
// []string{"hello", "world", " "}

[play]

TrimPrefix

Removes all the leading prefix from the collection.

result := lo.TrimPrefix([]int{1, 2, 1, 2, 3, 1, 2, 4}, []int{1, 2})
// []int{3, 1, 2, 4}

result := lo.TrimPrefix([]string{"hello", "world", "hello", "test"}, []string{"hello"})
// []string{"world", "hello", "test"}

[play]

TrimRight

Removes all the trailing cutset from the collection.

result := lo.TrimRight([]int{0, 1, 2, 0, 3, 0}, []int{0, 3})
// []int{0, 1, 2}

result := lo.TrimRight([]string{"hello", "world", "  "}, []string{" ", ""})
// []string{"hello", "world", ""}

[play]

TrimSuffix

Removes all the trailing suffix from the collection.

result := lo.TrimSuffix([]int{1, 2, 3, 1, 2, 4, 2, 4, 2, 4}, []int{2, 4})
// []int{1, 2, 3, 1}

result := lo.TrimSuffix([]string{"hello", "world", "hello", "test"}, []string{"test"})
// []string{"hello", "world", "hello"}

[play]

Keys

Creates a slice of the map keys.

Use the UniqKeys variant to deduplicate common keys.

keys := lo.Keys(map[string]int{"foo": 1, "bar": 2})
// []string{"foo", "bar"}

keys := lo.Keys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"baz": 3})
// []string{"foo", "bar", "baz"}

keys := lo.Keys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"bar": 3})
// []string{"foo", "bar", "bar"}

[play]

UniqKeys

Creates a slice of unique map keys.

keys := lo.UniqKeys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"baz": 3})
// []string{"foo", "bar", "baz"}

keys := lo.UniqKeys(map[string]int{"foo": 1, "bar": 2}, map[string]int{"bar": 3})
// []string{"foo", "bar"}

[play]

HasKey

Returns whether the given key exists.

exists := lo.HasKey(map[string]int{"foo": 1, "bar": 2}, "foo")
// true

exists := lo.HasKey(map[string]int{"foo": 1, "bar": 2}, "baz")
// false

[play]

Values

Creates a slice of the map values.

Use the UniqValues variant to deduplicate common values.

values := lo.Values(map[string]int{"foo": 1, "bar": 2})
// []int{1, 2}

values := lo.Values(map[string]int{"foo": 1, "bar": 2}, map[string]int{"baz": 3})
// []int{1, 2, 3}

values := lo.Values(map[string]int{"foo": 1, "bar": 2}, map[string]int{"bar": 2})
// []int{1, 2, 2}

[play]

UniqValues

Creates a slice of unique map values.

values := lo.UniqValues(map[string]int{"foo": 1, "bar": 2})
// []int{1, 2}

values := lo.UniqValues(map[string]int{"foo": 1, "bar": 2}, map[string]int{"baz": 3})
// []int{1, 2, 3}

values := lo.UniqValues(map[string]int{"foo": 1, "bar": 2}, map[string]int{"bar": 2})
// []int{1, 2}

[play]

ValueOr

Returns the value of the given key or the fallback value if the key is not present.

value := lo.ValueOr(map[string]int{"foo": 1, "bar": 2}, "foo", 42)
// 1

value := lo.ValueOr(map[string]int{"foo": 1, "bar": 2}, "baz", 42)
// 42

[play]

PickBy

Returns same map type filtered by given predicate.

m := lo.PickBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool {
    return value%2 == 1
})
// map[string]int{"foo": 1, "baz": 3}

[play]

PickByKeys

Returns same map type filtered by given keys.

m := lo.PickByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
// map[string]int{"foo": 1, "baz": 3}

[play]

PickByValues

Returns same map type filtered by given values.

m := lo.PickByValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
// map[string]int{"foo": 1, "baz": 3}

[play]

OmitBy

Returns same map type filtered by given predicate.

m := lo.OmitBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool {
    return value%2 == 1
})
// map[string]int{"bar": 2}

[play]

OmitByKeys

Returns same map type filtered by given keys.

m := lo.OmitByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
// map[string]int{"bar": 2}

[play]

OmitByValues

Returns same map type filtered by given values.

m := lo.OmitByValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
// map[string]int{"bar": 2}

[play]

Entries (alias: ToPairs)

Transforms a map into a slice of key/value pairs.

entries := lo.Entries(map[string]int{"foo": 1, "bar": 2})
// []lo.Entry[string, int]{
//     {
//         Key: "foo",
//         Value: 1,
//     },
//     {
//         Key: "bar",
//         Value: 2,
//     },
// }

[play]

FromEntries (alias: FromPairs)

Transforms a slice of key/value pairs into a map.

m := lo.FromEntries([]lo.Entry[string, int]{
    {
        Key: "foo",
        Value: 1,
    },
    {
        Key: "bar",
        Value: 2,
    },
})
// map[string]int{"foo": 1, "bar": 2}

[play]

Invert

Creates a map composed of the inverted keys and values. If map contains duplicate values, subsequent values overwrite property assignments of previous values.

m1 := lo.Invert(map[string]int{"a": 1, "b": 2})
// map[int]string{1: "a", 2: "b"}

m2 := lo.Invert(map[string]int{"a": 1, "b": 2, "c": 1})
// map[int]string{1: "c", 2: "b"}

[play]

Assign

Merges multiple maps from left to right.

mergedMaps := lo.Assign(
    map[string]int{"a": 1, "b": 2},
    map[string]int{"b": 3, "c": 4},
)
// map[string]int{"a": 1, "b": 3, "c": 4}

[play]

ChunkEntries

Splits a map into a slice of elements in groups of length equal to its size. If the map cannot be split evenly, the final chunk will contain the remaining elements.

maps := lo.ChunkEntries(
    map[string]int{
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
        "e": 5,
    },
    3,
)
// []map[string]int{
//    {"a": 1, "b": 2, "c": 3},
//    {"d": 4, "e": 5},
// }

[play]

MapKeys

Manipulates map keys and transforms it to a map of another type.

m2 := lo.MapKeys(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(_ int, v int) string {
    return strconv.FormatInt(int64(v), 10)
})
// map[string]int{"1": 1, "2": 2, "3": 3, "4": 4}

[play]

MapValues

Manipulates map values and transforms it to a map of another type.

m1 := map[int]int64{1: 1, 2: 2, 3: 3}

m2 := lo.MapValues(m1, func(x int64, _ int) string {
    return strconv.FormatInt(x, 10)
})
// map[int]string{1: "1", 2: "2", 3: "3"}

[play]

MapEntries

Manipulates map entries and transforms it to a map of another type.

in := map[string]int{"foo": 1, "bar": 2}

out := lo.MapEntries(in, func(k string, v int) (int, string) {
    return v,k
})
// map[int]string{1: "foo", 2: "bar"}

[play]

MapToSlice

Transforms a map into a slice based on specified iteratee.

m := map[int]int64{1: 4, 2: 5, 3: 6}

s := lo.MapToSlice(m, func(k int, v int64) string {
    return fmt.Sprintf("%d_%d", k, v)
})
// []string{"1_4", "2_5", "3_6"}

[play]

FilterMapToSlice

Transforms a map into a slice based on specified iteratee. The iteratee returns a value and a boolean. If the boolean is true, the value is added to the result slice.

If the boolean is false, the value is not added to the result slice. The order of the keys in the input map is not specified and the order of the keys in the output slice is not guaranteed.

kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}

result := lo.FilterMapToSlice(kv, func(k int, v int64) (string, bool) {
    return fmt.Sprintf("%d_%d", k, v), k%2 == 0
})
// []{"2_2", "4_4"}
FilterKeys

Transforms a map into a slice based on predicate returns true for specific elements. It is a mix of lo.Filter() and lo.Keys().

kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}

result := FilterKeys(kv, func(k int, v string) bool {
    return v == "foo"
})
// [1]

[play]

FilterValues

Transforms a map into a slice based on predicate returns true for specific elements. It is a mix of lo.Filter() and lo.Values().

kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}

result := FilterValues(kv, func(k int, v string) bool {
    return v == "foo"
})
// ["foo"]

[play]

Range / RangeFrom / RangeWithSteps

Creates a slice of numbers (positive and/or negative) progressing from start up to, but not including end.

result := lo.Range(4)
// [0, 1, 2, 3]

result := lo.Range(-4)
// [0, -1, -2, -3]

result := lo.RangeFrom(1, 5)
// [1, 2, 3, 4, 5]

result := lo.RangeFrom[float64](1.0, 5)
// [1.0, 2.0, 3.0, 4.0, 5.0]

result := lo.RangeWithSteps(0, 20, 5)
// [0, 5, 10, 15]

result := lo.RangeWithSteps[float32](-1.0, -4.0, -1.0)
// [-1.0, -2.0, -3.0]

result := lo.RangeWithSteps(1, 4, -1)
// []

result := lo.Range(0)
// []

[play]

Clamp

Clamps number within the inclusive lower and upper bounds.

r1 := lo.Clamp(0, -10, 10)
// 0

r2 := lo.Clamp(-42, -10, 10)
// -10

r3 := lo.Clamp(42, -10, 10)
// 10

[play]

Sum

Sums the values in a collection.

If collection is empty 0 is returned.

list := []int{1, 2, 3, 4, 5}
sum := lo.Sum(list)
// 15

[play]

SumBy

Summarizes the values in a collection using the given return value from the iteration function.

If collection is empty 0 is returned.

strings := []string{"foo", "bar"}
sum := lo.SumBy(strings, func(item string) int {
    return len(item)
})
// 6
Product

Calculates the product of the values in a collection.

If collection is empty 0 is returned.

list := []int{1, 2, 3, 4, 5}
product := lo.Product(list)
// 120

[play]

ProductBy

Calculates the product of the values in a collection using the given return value from the iteration function.

If collection is empty 0 is returned.

strings := []string{"foo", "bar"}
product := lo.ProductBy(strings, func(item string) int {
    return len(item)
})
// 9

[play]

Mean

Calculates the mean of a collection of numbers.

If collection is empty 0 is returned.

mean := lo.Mean([]int{2, 3, 4, 5})
// 3

mean := lo.Mean([]float64{2, 3, 4, 5})
// 3.5

mean := lo.Mean([]float64{})
// 0
MeanBy

Calculates the mean of a collection of numbers using the given return value from the iteration function.

If collection is empty 0 is returned.

list := []string{"aa", "bbb", "cccc", "ddddd"}
mapper := func(item string) float64 {
    return float64(len(item))
}

mean := lo.MeanBy(list, mapper)
// 3.5

mean := lo.MeanBy([]float64{}, mapper)
// 0

[play]

Mode

Calculates the mode (most frequent value) of a collection of numbers.

If multiple values have the same highest frequency, then multiple values are returned.

If the collection is empty, the zero value of T[] is returned.

mode := lo.Mode([]int{2, 2, 3, 4})
// [2]

mode := lo.Mode([]float64{2, 2, 3, 3})
// [2, 3]

mode := lo.Mode([]float64{})
// []

mode := lo.Mode([]int{1, 2, 3, 4, 5, 6, 7, 8, 9})
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
RandomString

Returns a random string of the specified length and made of the specified charset.

str := lo.RandomString(5, lo.LettersCharset)
// example: "eIGbt"

[play]

Substring

Return part of a string.

sub := lo.Substring("hello", 2, 3)
// "llo"

sub := lo.Substring("hello", -4, 3)
// "ell"

sub := lo.Substring("hello", -2, math.MaxUint)
// "lo"

[play]

ChunkString

Returns a slice of strings split into groups of length size. If the string can't be split evenly, the final chunk will be the remaining characters.

lo.ChunkString("123456", 2)
// []string{"12", "34", "56"}

lo.ChunkString("1234567", 2)
// []string{"12", "34", "56", "7"}

lo.ChunkString("", 2)
// []string{""}

lo.ChunkString("1", 2)
// []string{"1"}

[play]

RuneLength

An alias to utf8.RuneCountInString which returns the number of runes in string.

sub := lo.RuneLength("hellô")
// 5

sub := len("hellô")
// 6

[play]

PascalCase

Converts string to pascal case.

str := lo.PascalCase("hello_world")
// HelloWorld

[play]

CamelCase

Converts string to camel case.

str := lo.CamelCase("hello_world")
// helloWorld

[play]

KebabCase

Converts string to kebab case.

str := lo.KebabCase("helloWorld")
// hello-world

[play]

SnakeCase

Converts string to snake case.

str := lo.SnakeCase("HelloWorld")
// hello_world

[play]

Words

Splits string into a slice of its words.

str := lo.Words("helloWorld")
// []string{"hello", "world"}

[play]

Capitalize

Converts the first character of string to upper case and the remaining to lower case.

str := lo.Capitalize("heLLO")
// Hello

[play]

Ellipsis

Trims and truncates a string to a specified length in bytes and appends an ellipsis if truncated. If the string contains non-ASCII characters (which may occupy multiple bytes in UTF-8), truncating by byte length may split a character in the middle, potentially resulting in garbled output.

str := lo.Ellipsis("  Lorem Ipsum  ", 5)
// Lo...

str := lo.Ellipsis("Lorem Ipsum", 100)
// Lorem Ipsum

str := lo.Ellipsis("Lorem Ipsum", 3)
// ...

[play]

T2 -> T9

Creates a tuple from a list of values.

tuple1 := lo.T2("x", 1)
// Tuple2[string, int]{A: "x", B: 1}

func example() (string, int) { return "y", 2 }
tuple2 := lo.T2(example())
// Tuple2[string, int]{A: "y", B: 2}

[play]

Unpack2 -> Unpack9

Returns values contained in a tuple.

r1, r2 := lo.Unpack2(lo.Tuple2[string, int]{"a", 1})
// "a", 1

Unpack is also available as a method of TupleX.

tuple2 := lo.T2("a", 1)
a, b := tuple2.Unpack()
// "a", 1

[play]

Zip2 -> Zip9

Zip creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on.

When collections are different sizes, the Tuple attributes are filled with zero value.

tuples := lo.Zip2([]string{"a", "b"}, []int{1, 2})
// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}

[play]

ZipBy2 -> ZipBy9

ZipBy creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on.

When collections are different sizes, the Tuple attributes are filled with zero value.

items := lo.ZipBy2([]string{"a", "b"}, []int{1, 2}, func(a string, b int) string {
    return fmt.Sprintf("%s-%d", a, b)
})
// []string{"a-1", "b-2"}
Unzip2 -> Unzip9

Unzip accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration.

a, b := lo.Unzip2([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}})
// []string{"a", "b"}
// []int{1, 2}

[play]

UnzipBy2 -> UnzipBy9

UnzipBy2 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration.

a, b := lo.UnzipBy2([]string{"hello", "john", "doe"}, func(str string) (string, int) {
    return str, len(str)
})
// []string{"hello", "john", "doe"}
// []int{5, 4, 3}
CrossJoin2 -> CrossJoin9

Combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty.

result := lo.CrossJoin2([]string{"hello", "john", "doe"}, []int{1, 2})
// lo.Tuple2{"hello", 1}
// lo.Tuple2{"hello", 2}
// lo.Tuple2{"john", 1}
// lo.Tuple2{"john", 2}
// lo.Tuple2{"doe", 1}
// lo.Tuple2{"doe", 2}
CrossJoinBy2 -> CrossJoinBy9

Combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty.

result := lo.CrossJoinBy2([]string{"hello", "john", "doe"}, []int{1, 2}, func(a A, b B) string {
    return fmt.Sprintf("%s - %d", a, b)
})
// "hello - 1"
// "hello - 2"
// "john - 1"
// "john - 2"
// "doe - 1"
// "doe - 2"
Duration

Returns the time taken to execute a function.

duration := lo.Duration(func() {
    // very long job
})
// 3s

[play]

Duration0 -> Duration10

Returns the time taken to execute a function.

duration := lo.Duration0(func() {
    // very long job
})
// 3s

err, duration := lo.Duration1(func() error {
    // very long job
    return errors.New("an error")
})
// an error
// 3s

str, nbr, err, duration := lo.Duration3(func() (string, int, error) {
    // very long job
    return "hello", 42, nil
})
// hello
// 42
// nil
// 3s
ChannelDispatcher

Distributes messages from input channels into N child channels. Close events are propagated to children.

Underlying channels can have a fixed buffer capacity or be unbuffered when cap is 0.

ch := make(chan int, 42)
for i := 0; i <= 10; i++ {
    ch <- i
}

children := lo.ChannelDispatcher(ch, 5, 10, DispatchingStrategyRoundRobin[int])
// []<-chan int{...}

consumer := func(c <-chan int) {
    for {
        msg, ok := <-c
        if !ok {
            println("closed")

            break
        }

        println(msg)
    }
}

for i := range children {
    go consumer(children[i])
}

[play]

Many distributions strategies are available:

Some strategies bring fallback, in order to favor non-blocking behaviors. See implementations.

For custom strategies, just implement the lo.DispatchingStrategy prototype:

type DispatchingStrategy[T any] func(message T, messageIndex uint64, channels []<-chan T) int

Eg:

type Message struct {
    TenantID uuid.UUID
}

func hash(id uuid.UUID) int {
    h := fnv.New32a()
    h.Write([]byte(id.String()))
    return int(h.Sum32())
}

// Routes messages per TenantID.
customStrategy := func(message string, messageIndex uint64, channels []<-chan string) int {
    destination := hash(message) % len(channels)

    // check if channel is full
    if len(channels[destination]) < cap(channels[destination]) {
        return destination
    }

    // fallback when child channel is full
    return utils.DispatchingStrategyRoundRobin(message, uint64(destination), channels)
}

children := lo.ChannelDispatcher(ch, 5, 10, customStrategy)
...
SliceToChannel

Returns a read-only channel of collection elements. Channel is closed after last element. Channel capacity can be customized.

list := []int{1, 2, 3, 4, 5}

for v := range lo.SliceToChannel(2, list) {
    println(v)
}
// prints 1, then 2, then 3, then 4, then 5

[play]

ChannelToSlice

Returns a slice built from channel items. Blocks until channel closes.

list := []int{1, 2, 3, 4, 5}
ch := lo.SliceToChannel(2, list)

items := ChannelToSlice(ch)
// []int{1, 2, 3, 4, 5}
Generator

Implements the generator design pattern. Channel is closed after last element. Channel capacity can be customized.

generator := func(yield func(int)) {
    yield(1)
    yield(2)
    yield(3)
}

for v := range lo.Generator(2, generator) {
    println(v)
}
// prints 1, then 2, then 3
Buffer

Creates a slice of n elements from a channel. Returns the slice, the slice length, the read time and the channel status (opened/closed).

ch := lo.SliceToChannel(2, []int{1, 2, 3, 4, 5})

items1, length1, duration1, ok1 := lo.Buffer(ch, 3)
// []int{1, 2, 3}, 3, 0s, true
items2, length2, duration2, ok2 := lo.Buffer(ch, 3)
// []int{4, 5}, 2, 0s, false

Example: RabbitMQ consumer 👇

ch := readFromQueue()

for {
    // read 1k items
    items, length, _, ok := lo.Buffer(ch, 1000)

    // do batching stuff

    if !ok {
        break
    }
}
BufferWithContext

Creates a slice of n elements from a channel, with timeout. Returns the slice, the slice length, the read time and the channel status (opened/closed).

ctx, cancel := context.WithCancel(context.TODO())
go func() {
    ch <- 0
    time.Sleep(10*time.Millisecond)
    ch <- 1
    time.Sleep(10*time.Millisecond)
    ch <- 2
    time.Sleep(10*time.Millisecond)
    ch <- 3
    time.Sleep(10*time.Millisecond)
    ch <- 4
    time.Sleep(10*time.Millisecond)
    cancel()
}()

items1, length1, duration1, ok1 := lo.BufferWithContext(ctx, ch, 3)
// []int{0, 1, 2}, 3, 20ms, true
items2, length2, duration2, ok2 := lo.BufferWithContext(ctx, ch, 3)
// []int{3, 4}, 2, 30ms, false
BufferWithTimeout

Creates a slice of n elements from a channel, with timeout. Returns the slice, the slice length, the read time and the channel status (opened/closed).

generator := func(yield func(int)) {
    for i := 0; i < 5; i++ {
        yield(i)
        time.Sleep(35*time.Millisecond)
    }
}

ch := lo.Generator(0, generator)

items1, length1, duration1, ok1 := lo.BufferWithTimeout(ch, 3, 100*time.Millisecond)
// []int{1, 2}, 2, 100ms, true
items2, length2, duration2, ok2 := lo.BufferWithTimeout(ch, 3, 100*time.Millisecond)
// []int{3, 4, 5}, 3, 75ms, true
items3, length3, duration2, ok3 := lo.BufferWithTimeout(ch, 3, 100*time.Millisecond)
// []int{}, 0, 10ms, false

Example: RabbitMQ consumer 👇

ch := readFromQueue()

for {
    // read 1k items
    // wait up to 1 second
    items, length, _, ok := lo.BufferWithTimeout(ch, 1000, 1*time.Second)

    // do batching stuff

    if !ok {
        break
    }
}

Example: Multithreaded RabbitMQ consumer 👇

ch := readFromQueue()

// 5 workers
// prefetch 1k messages per worker
children := lo.ChannelDispatcher(ch, 5, 1000, lo.DispatchingStrategyFirst[int])

consumer := func(c <-chan int) {
    for {
        // read 1k items
        // wait up to 1 second
        items, length, _, ok := lo.BufferWithTimeout(ch, 1000, 1*time.Second)

        // do batching stuff

        if !ok {
            break
        }
    }
}

for i := range children {
    go consumer(children[i])
}
FanIn

Merge messages from multiple input channels into a single buffered channel. Output messages have no priority. When all upstream channels reach EOF, downstream channel closes.

stream1 := make(chan int, 42)
stream2 := make(chan int, 42)
stream3 := make(chan int, 42)

all := lo.FanIn(100, stream1, stream2, stream3)
// <-chan int
FanOut

Broadcasts all the upstream messages to multiple downstream channels. When upstream channel reaches EOF, downstream channels close. If any downstream channels is full, broadcasting is paused.

stream := make(chan int, 42)

all := lo.FanOut(5, 100, stream)
// [5]<-chan int
Contains

Returns true if an element is present in a collection.

present := lo.Contains([]int{0, 1, 2, 3, 4, 5}, 5)
// true

[play]

ContainsBy

Returns true if the predicate function returns true.

present := lo.ContainsBy([]int{0, 1, 2, 3, 4, 5}, func(x int) bool {
    return x == 3
})
// true
Every

Returns true if all elements of a subset are contained in a collection or if the subset is empty.

ok := lo.Every([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// true

ok := lo.Every([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
// false
EveryBy

Returns true if the predicate returns true for all elements in the collection or if the collection is empty.

b := EveryBy([]int{1, 2, 3, 4}, func(x int) bool {
    return x < 5
})
// true

[play]

Some

Returns true if at least 1 element of a subset is contained in a collection. If the subset is empty Some returns false.

ok := lo.Some([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
// true

[play]

ok := lo.Some([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6}) // false


### SomeBy

Returns true if the predicate returns true for any of the elements in the collection.
If the collection is empty SomeBy returns false.

```go
b := SomeBy([]int{1, 2, 3, 4}, func(x int) bool {
    return x < 3
})
// true
None

Returns true if no element of a subset is contained in a collection or if the subset is empty.

b := None([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// false
b := None([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// true

[play]

NoneBy

Returns true if the predicate returns true for none of the elements in the collection or if the collection is empty.

b := NoneBy([]int{1, 2, 3, 4}, func(x int) bool {
    return x < 0
})
// true

[play]

Intersect

Returns the intersection between two collections.

result1 := lo.Intersect([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// []int{0, 2}

result2 := lo.Intersect([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
// []int{0}

result3 := lo.Intersect([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// []int{}
Difference

Returns the difference between two collections.

  • The first value is the collection of elements absent from list2.
  • The second value is the collection of elements absent from list1.
left, right := lo.Difference([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 6})
// []int{1, 3, 4, 5}, []int{6}

left, right := lo.Difference([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5})
// []int{}, []int{}

[play]

Union

Returns all distinct elements from given collections. Result will not change the order of elements relatively.

union := lo.Union([]int{0, 1, 2, 3, 4, 5}, []int{0, 2}, []int{0, 10})
// []int{0, 1, 2, 3, 4, 5, 10}
Without

Returns a slice excluding all given values.

subset := lo.Without([]int{0, 2, 10}, 2)
// []int{0, 10}

subset := lo.Without([]int{0, 2, 10}, 0, 1, 2, 3, 4, 5)
// []int{10}
WithoutBy

Filters a slice by excluding elements whose extracted keys match any in the exclude list.

Returns a new slice containing only the elements whose keys are not in the exclude list.

type struct User {
    ID int
    Name string
}

// original users
users := []User{
    {ID: 1, Name: "Alice"},
    {ID: 2, Name: "Bob"},
    {ID: 3, Name: "Charlie"},
}

// extract function to get the user ID
getID := func(user User) int {
    return user.ID
}

// exclude users with IDs 2 and 3
excludedIDs := []int{2, 3}

// filtering users
filteredUsers := lo.WithoutBy(users, getID, excludedIDs...)
// []User[{ID: 1, Name: "Alice"}]
WithoutEmpty

Returns a slice excluding zero values.

subset := lo.WithoutEmpty([]int{0, 2, 10})
// []int{2, 10}
WithoutNth

Returns a slice excluding the nth value.

subset := lo.WithoutNth([]int{-2, -1, 0, 1, 2}, 3, -42, 1)
// []int{-2, 0, 2}
ElementsMatch

Returns true if lists contain the same set of elements (including empty set).

If there are duplicate elements, the number of occurrences in each list should match.

The order of elements is not checked.

b := lo.ElementsMatch([]int{1, 1, 2}, []int{2, 1, 1})
// true
ElementsMatchBy

Returns true if lists contain the same set of elements' keys (including empty set).

If there are duplicate keys, the number of occurrences in each list should match.

The order of elements is not checked.

b := lo.ElementsMatchBy(
    []someType{a, b},
    []someType{b, a},
    func(item someType) string { return item.ID() },
)
// true
IndexOf

Returns the index at which the first occurrence of a value is found in a slice or -1 if the value cannot be found.

found := lo.IndexOf([]int{0, 1, 2, 1, 2, 3}, 2)
// 2

notFound := lo.IndexOf([]int{0, 1, 2, 1, 2, 3}, 6)
// -1

[play]

LastIndexOf

Returns the index at which the last occurrence of a value is found in a slice or -1 if the value cannot be found.

found := lo.LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 2)
// 4

notFound := lo.LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 6)
// -1
HasPrefix

Returns true if the collection has the prefix.

ok := lo.HasPrefix([]int{1, 2, 3, 4}, []int{42})
// false

ok := lo.HasPrefix([]int{1, 2, 3, 4}, []int{1, 2})
// true

[play]

HasSuffix

Returns true if the collection has the suffix.

ok := lo.HasSuffix([]int{1, 2, 3, 4}, []int{42})
// false

ok := lo.HasSuffix([]int{1, 2, 3, 4}, []int{3, 4})
// true

[play]

Find

Searches for an element in a slice based on a predicate. Returns element and true if element was found.

str, ok := lo.Find([]string{"a", "b", "c", "d"}, func(i string) bool {
    return i == "b"
})
// "b", true

str, ok := lo.Find([]string{"foobar"}, func(i string) bool {
    return i == "b"
})
// "", false

[play]

FindIndexOf

FindIndexOf searches for an element in a slice based on a predicate and returns the index and true. Returns -1 and false if the element is not found.

str, index, ok := lo.FindIndexOf([]string{"a", "b", "a", "b"}, func(i string) bool {
    return i == "b"
})
// "b", 1, true

str, index, ok := lo.FindIndexOf([]string{"foobar"}, func(i string) bool {
    return i == "b"
})
// "", -1, false

[play]

FindLastIndexOf

FindLastIndexOf searches for the last element in a slice based on a predicate and returns the index and true. Returns -1 and false if the element is not found.

str, index, ok := lo.FindLastIndexOf([]string{"a", "b", "a", "b"}, func(i string) bool {
    return i == "b"
})
// "b", 4, true

str, index, ok := lo.FindLastIndexOf([]string{"foobar"}, func(i string) bool {
    return i == "b"
})
// "", -1, false

[play]

FindOrElse

Searches for an element in a slice based on a predicate. Returns the element if found or a given fallback value otherwise.

str := lo.FindOrElse([]string{"a", "b", "c", "d"}, "x", func(i string) bool {
    return i == "b"
})
// "b"

str := lo.FindOrElse([]string{"foobar"}, "x", func(i string) bool {
    return i == "b"
})
// "x"
FindKey

Returns the key of the first value matching.

result1, ok1 := lo.FindKey(map[string]int{"foo": 1, "bar": 2, "baz": 3}, 2)
// "bar", true

result2, ok2 := lo.FindKey(map[string]int{"foo": 1, "bar": 2, "baz": 3}, 42)
// "", false

type test struct {
    foobar string
}
result3, ok3 := lo.FindKey(map[string]test{"foo": test{"foo"}, "bar": test{"bar"}, "baz": test{"baz"}}, test{"foo"})
// "foo", true
FindKeyBy

Returns the key of the first element predicate returns true for.

result1, ok1 := lo.FindKeyBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(k string, v int) bool {
    return k == "foo"
})
// "foo", true

result2, ok2 := lo.FindKeyBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(k string, v int) bool {
    return false
})
// "", false
FindUniques

Returns a slice with all the elements that appear in the collection only once. The order of result values is determined by the order they occur in the slice.

uniqueValues := lo.FindUniques([]int{1, 2, 2, 1, 2, 3})
// []int{3}
FindUniquesBy

Returns a slice with all the elements that appear in the collection only once. The order of result values is determined by the order they occur in the slice. It accepts iteratee which is invoked for each element in the slice to generate the criterion by which uniqueness is computed.

uniqueValues := lo.FindUniquesBy([]int{3, 4, 5, 6, 7}, func(i int) int {
    return i%3
})
// []int{5}
FindDuplicates

Returns a slice with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order they occur in the slice.

duplicatedValues := lo.FindDuplicates([]int{1, 2, 2, 1, 2, 3})
// []int{1, 2}
FindDuplicatesBy

Returns a slice with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order they occur in the slice. It accepts iteratee which is invoked for each element in the slice to generate the criterion by which uniqueness is computed.

duplicatedValues := lo.FindDuplicatesBy([]int{3, 4, 5, 6, 7}, func(i int) int {
    return i%3
})
// []int{3, 4}
Min

Search the minimum value of a collection.

Returns zero value when the collection is empty.

min := lo.Min([]int{1, 2, 3})
// 1

min := lo.Min([]int{})
// 0

min := lo.Min([]time.Duration{time.Second, time.Hour})
// 1s

[play]

MinIndex

Search the minimum value of a collection and the index of the minimum value.

Returns (zero value, -1) when the collection is empty.

min, index := lo.MinIndex([]int{1, 2, 3})
// 1, 0

min, index := lo.MinIndex([]int{})
// 0, -1

min, index := lo.MinIndex([]time.Duration{time.Second, time.Hour})
// 1s, 0
MinBy

Search the minimum value of a collection using the given comparison function.

If several values of the collection are equal to the smallest value, returns the first such value.

Returns zero value when the collection is empty.

min := lo.MinBy([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
    return len(item) < len(min)
})
// "s1"

min := lo.MinBy([]string{}, func(item string, min string) bool {
    return len(item) < len(min)
})
// ""
MinIndexBy

Search the minimum value of a collection using the given comparison function and the index of the minimum value.

If several values of the collection are equal to the smallest value, returns the first such value.

Returns (zero value, -1) when the collection is empty.

min, index := lo.MinIndexBy([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
    return len(item) < len(min)
})
// "s1", 0

min, index := lo.MinIndexBy([]string{}, func(item string, min string) bool {
    return len(item) < len(min)
})
// "", -1
Earliest

Search the minimum time.Time of a collection.

Returns zero value when the collection is empty.

earliest := lo.Earliest(time.Now(), time.Time{})
// 0001-01-01 00:00:00 +0000 UTC
EarliestBy

Search the minimum time.Time of a collection using the given iteratee function.

Returns zero value when the collection is empty.

type foo struct {
    bar time.Time
}

earliest := lo.EarliestBy([]foo{{time.Now()}, {}}, func(i foo) time.Time {
    return i.bar
})
// {bar:{2023-04-01 01:02:03 +0000 UTC}}
Max

Search the maximum value of a collection.

Returns zero value when the collection is empty.

max := lo.Max([]int{1, 2, 3})
// 3

max := lo.Max([]int{})
// 0

max := lo.Max([]time.Duration{time.Second, time.Hour})
// 1h
MaxIndex

Search the maximum value of a collection and the index of the maximum value.

Returns (zero value, -1) when the collection is empty.

max, index := lo.MaxIndex([]int{1, 2, 3})
// 3, 2

max, index := lo.MaxIndex([]int{})
// 0, -1

max, index := lo.MaxIndex([]time.Duration{time.Second, time.Hour})
// 1h, 1
MaxBy

Search the maximum value of a collection using the given comparison function.

If several values of the collection are equal to the greatest value, returns the first such value.

Returns zero value when the collection is empty.

max := lo.MaxBy([]string{"string1", "s2", "string3"}, func(item string, max string) bool {
    return len(item) > len(max)
})
// "string1"

max := lo.MaxBy([]string{}, func(item string, max string) bool {
    return len(item) > len(max)
})
// ""
MaxIndexBy

Search the maximum value of a collection using the given comparison function and the index of the maximum value.

If several values of the collection are equal to the greatest value, returns the first such value.

Returns (zero value, -1) when the collection is empty.

max, index := lo.MaxIndexBy([]string{"string1", "s2", "string3"}, func(item string, max string) bool {
    return len(item) > len(max)
})
// "string1", 0

max, index := lo.MaxIndexBy([]string{}, func(item string, max string) bool {
    return len(item) > len(max)
})
// "", -1
Latest

Search the maximum time.Time of a collection.

Returns zero value when the collection is empty.

latest := lo.Latest(time.Now(), time.Time{})
// 2023-04-01 01:02:03 +0000 UTC
LatestBy

Search the maximum time.Time of a collection using the given iteratee function.

Returns zero value when the collection is empty.

type foo struct {
    bar time.Time
}

latest := lo.LatestBy([]foo{{time.Now()}, {}}, func(i foo) time.Time {
    return i.bar
})
// {bar:{2023-04-01 01:02:03 +0000 UTC}}
First

Returns the first element of a collection and check for availability of the first element.

first, ok := lo.First([]int{1, 2, 3})
// 1, true

first, ok := lo.First([]int{})
// 0, false
FirstOrEmpty

Returns the first element of a collection or zero value if empty.

first := lo.FirstOrEmpty([]int{1, 2, 3})
// 1

first := lo.FirstOrEmpty([]int{})
// 0
FirstOr

Returns the first element of a collection or the fallback value if empty.

first := lo.FirstOr([]int{1, 2, 3}, 245)
// 1

first := lo.FirstOr([]int{}, 31)
// 31
Last

Returns the last element of a collection or error if empty.

last, ok := lo.Last([]int{1, 2, 3})
// 3
// true

last, ok := lo.Last([]int{})
// 0
// false
LastOrEmpty

Returns the last element of a collection or zero value if empty.

last := lo.LastOrEmpty([]int{1, 2, 3})
// 3

last := lo.LastOrEmpty([]int{})
// 0
LastOr

Returns the last element of a collection or the fallback value if empty.

last := lo.LastOr([]int{1, 2, 3}, 245)
// 3

last := lo.LastOr([]int{}, 31)
// 31
Nth

Returns the element at index nth of collection. If nth is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds.

nth, err := lo.Nth([]int{0, 1, 2, 3}, 2)
// 2

nth, err := lo.Nth([]int{0, 1, 2, 3}, -2)
// 2
NthOr

Returns the element at index nth of the collection. If nth is negative, it returns the nth element from the end. If nth is out of slice bounds, it returns the provided fallback value

nth := lo.NthOr([]int{10, 20, 30, 40, 50}, 2, -1)
// 30

nth := lo.NthOr([]int{10, 20, 30, 40, 50}, -1, -1)
// 50

nth := lo.NthOr([]int{10, 20, 30, 40, 50}, 5, -1)
// -1 (fallback value)
NthOrEmpty

Returns the element at index nth of the collection. If nth is negative, it returns the nth element from the end. If nth is out of slice bounds, it returns the zero value for the element type (e.g., 0 for integers, "" for strings, etc).

nth := lo.NthOrEmpty([]int{10, 20, 30, 40, 50}, 2)
// 30

nth := lo.NthOrEmpty([]int{10, 20, 30, 40, 50}, -1)
// 50

nth := lo.NthOrEmpty([]int{10, 20, 30, 40, 50}, 5)
// 0 (zero value for int)

nth := lo.NthOrEmpty([]string{"apple", "banana", "cherry"}, 2)
// "cherry"

nth := lo.NthOrEmpty([]string{"apple", "banana", "cherry"}, 5)
// "" (zero value for string)
Sample

Returns a random item from collection.

lo.Sample([]string{"a", "b", "c"})
// a random string from []string{"a", "b", "c"}

lo.Sample([]string{})
// ""

[play]

SampleBy

Returns a random item from collection, using a given random integer generator.

import "math/rand"

r := rand.New(rand.NewSource(42))
lo.SampleBy([]string{"a", "b", "c"}, r.Intn)
// a random string from []string{"a", "b", "c"}, using a seeded random generator

lo.SampleBy([]string{}, r.Intn)
// ""
Samples

Returns N random unique items from collection.

lo.Samples([]string{"a", "b", "c"}, 3)
// []string{"a", "b", "c"} in random order
SamplesBy

Returns N random unique items from collection, using a given random integer generator.

r := rand.New(rand.NewSource(42))
lo.SamplesBy([]string{"a", "b", "c"}, 3, r.Intn)
// []string{"a", "b", "c"} in random order, using a seeded random generator
Ternary

A single line if/else statement.

result := lo.Ternary(true, "a", "b")
// "a"

result := lo.Ternary(false, "a", "b")
// "b"

Take care to avoid dereferencing potentially nil pointers in your A/B expressions, because they are both evaluated. See TernaryF to avoid this problem.

[play]

TernaryF

A single line if/else statement whose options are functions.

result := lo.TernaryF(true, func() string { return "a" }, func() string { return "b" })
// "a"

result := lo.TernaryF(false, func() string { return "a" }, func() string { return "b" })
// "b"

Useful to avoid nil-pointer dereferencing in initializations, or avoid running unnecessary code

var s *string

someStr := TernaryF(s == nil, func() string { return uuid.New().String() }, func() string { return *s })
// ef782193-c30c-4e2e-a7ae-f8ab5e125e02

[play]

If / ElseIf / Else
result := lo.If(true, 1).
    ElseIf(false, 2).
    Else(3)
// 1

result := lo.If(false, 1).
    ElseIf(true, 2).
    Else(3)
// 2

result := lo.If(false, 1).
    ElseIf(false, 2).
    Else(3)
// 3

Using callbacks:

result := lo.IfF(true, func () int {
        return 1
    }).
    ElseIfF(false, func () int {
        return 2
    }).
    ElseF(func () int {
        return 3
    })
// 1

Mixed:

result := lo.IfF(true, func () int {
        return 1
    }).
    Else(42)
// 1

[play]

Switch / Case / Default
result := lo.Switch(1).
    Case(1, "1").
    Case(2, "2").
    Default("3")
// "1"

result := lo.Switch(2).
    Case(1, "1").
    Case(2, "2").
    Default("3")
// "2"

result := lo.Switch(42).
    Case(1, "1").
    Case(2, "2").
    Default("3")
// "3"

Using callbacks:

result := lo.Switch(1).
    CaseF(1, func() string {
        return "1"
    }).
    CaseF(2, func() string {
        return "2"
    }).
    DefaultF(func() string {
        return "3"
    })
// "1"

Mixed:

result := lo.Switch(1).
    CaseF(1, func() string {
        return "1"
    }).
    Default("42")
// "1"

[play]

IsNil

Checks if a value is nil or if it's a reference type with a nil underlying value.

var x int
lo.IsNil(x)
// false

var k struct{}
lo.IsNil(k)
// false

var i *int
lo.IsNil(i)
// true

var ifaceWithNilValue any = (*string)(nil)
lo.IsNil(ifaceWithNilValue)
// true
ifaceWithNilValue == nil
// false
IsNotNil

Checks if a value is not nil or if it's not a reference type with a nil underlying value.

var x int
lo.IsNotNil(x)
// true

var k struct{}
lo.IsNotNil(k)
// true

var i *int
lo.IsNotNil(i)
// false

var ifaceWithNilValue any = (*string)(nil)
lo.IsNotNil(ifaceWithNilValue)
// false
ifaceWithNilValue == nil
// true
ToPtr

Returns a pointer copy of the value.

ptr := lo.ToPtr("hello world")
// *string{"hello world"}

[play]

Nil

Returns a nil pointer of type.

ptr := lo.Nil[float64]()
// nil
EmptyableToPtr

Returns a pointer copy of value if it's nonzero. Otherwise, returns nil pointer.

ptr := lo.EmptyableToPtr(nil)
// nil

ptr := lo.EmptyableToPtr("")
// nil

ptr := lo.EmptyableToPtr([]int{})
// *[]int{}

ptr := lo.EmptyableToPtr("hello world")
// *string{"hello world"}
FromPtr

Returns the pointer value or empty.

str := "hello world"
value := lo.FromPtr(&str)
// "hello world"

value := lo.FromPtr(nil)
// ""
FromPtrOr

Returns the pointer value or the fallback value.

str := "hello world"
value := lo.FromPtrOr(&str, "empty")
// "hello world"

value := lo.FromPtrOr(nil, "empty")
// "empty"
ToSlicePtr

Returns a slice of pointers to each value.

ptr := lo.ToSlicePtr([]string{"hello", "world"})
// []*string{"hello", "world"}
FromSlicePtr

Returns a slice with the pointer values. Returns a zero value in case of a nil pointer element.

str1 := "hello"
str2 := "world"

ptr := lo.FromSlicePtr[string]([]*string{&str1, &str2, nil})
// []string{"hello", "world", ""}

ptr := lo.Compact(
    lo.FromSlicePtr[string]([]*string{&str1, &str2, nil}),
)
// []string{"hello", "world"}
FromSlicePtrOr

Returns a slice with the pointer values or the fallback value.

str1 := "hello"
str2 := "world"

ptr := lo.FromSlicePtrOr([]*string{&str1, nil, &str2}, "fallback value")
// []string{"hello", "fallback value", "world"}

[play]

ToAnySlice

Returns a slice with all elements mapped to any type.

elements := lo.ToAnySlice([]int{1, 5, 1})
// []any{1, 5, 1}
FromAnySlice

Returns a slice with all elements mapped to a type. Returns false in case of type conversion failure.

elements, ok := lo.FromAnySlice([]any{"foobar", 42})
// []string{}, false

elements, ok := lo.FromAnySlice([]any{"foobar", "42"})
// []string{"foobar", "42"}, true
Empty

Returns the zero value.

lo.Empty[int]()
// 0
lo.Empty[string]()
// ""
lo.Empty[bool]()
// false
IsEmpty

Returns true if argument is a zero value.

lo.IsEmpty(0)
// true
lo.IsEmpty(42)
// false

lo.IsEmpty("")
// true
lo.IsEmpty("foobar")
// false

type test struct {
    foobar string
}

lo.IsEmpty(test{foobar: ""})
// true
lo.IsEmpty(test{foobar: "foobar"})
// false
IsNotEmpty

Returns true if argument is a zero value.

lo.IsNotEmpty(0)
// false
lo.IsNotEmpty(42)
// true

lo.IsNotEmpty("")
// false
lo.IsNotEmpty("foobar")
// true

type test struct {
    foobar string
}

lo.IsNotEmpty(test{foobar: ""})
// false
lo.IsNotEmpty(test{foobar: "foobar"})
// true
Coalesce

Returns the first non-empty arguments. Arguments must be comparable.

result, ok := lo.Coalesce(0, 1, 2, 3)
// 1 true

result, ok := lo.Coalesce("")
// "" false

var nilStr *string
str := "foobar"
result, ok := lo.Coalesce(nil, nilStr, &str)
// &"foobar" true
CoalesceOrEmpty

Returns the first non-empty arguments. Arguments must be comparable.

result := lo.CoalesceOrEmpty(0, 1, 2, 3)
// 1

result := lo.CoalesceOrEmpty("")
// ""

var nilStr *string
str := "foobar"
result := lo.CoalesceOrEmpty(nil, nilStr, &str)
// &"foobar"
CoalesceSlice

Returns the first non-zero slice.

result, ok := lo.CoalesceSlice([]int{1, 2, 3}, []int{4, 5, 6})
// [1, 2, 3]
// true

result, ok := lo.CoalesceSlice(nil, []int{})
// []
// true

result, ok := lo.CoalesceSlice([]int(nil))
// []
// false
CoalesceSliceOrEmpty

Returns the first non-zero slice.

result := lo.CoalesceSliceOrEmpty([]int{1, 2, 3}, []int{4, 5, 6})
// [1, 2, 3]

result := lo.CoalesceSliceOrEmpty(nil, []int{})
// []
CoalesceMap

Returns the first non-zero map.

result, ok := lo.CoalesceMap(map[string]int{"1": 1, "2": 2, "3": 3}, map[string]int{"4": 4, "5": 5, "6": 6})
// {"1": 1, "2": 2, "3": 3}
// true

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

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

Returns the first non-zero map.

result := lo.CoalesceMapOrEmpty(map[string]int{"1": 1, "2": 2, "3": 3}, map[string]int{"4": 4, "5": 5, "6": 6})
// {"1": 1, "2": 2, "3": 3}

result := lo.CoalesceMapOrEmpty(nil, map[string]int{})
// {}
Partial

Returns new function that, when called, has its first argument set to the provided value.

add := func(x, y int) int { return x + y }
f := lo.Partial(add, 5)

f(10)
// 15

f(42)
// 47

[play]

Partial2 -> Partial5

Returns new function that, when called, has its first argument set to the provided value.

add := func(x, y, z int) int { return x + y + z }
f := lo.Partial2(add, 42)

f(10, 5)
// 57

f(42, -4)
// 80

[play]

Attempt

Invokes a function N times until it returns valid output. Returns either the caught error or nil.

When the first argument is less than 1, the function runs until a successful response is returned.

iter, err := lo.Attempt(42, func(i int) error {
    if i == 5 {
        return nil
    }

    return errors.New("failed")
})
// 6
// nil

iter, err := lo.Attempt(2, func(i int) error {
    if i == 5 {
        return nil
    }

    return errors.New("failed")
})
// 2
// error "failed"

iter, err := lo.Attempt(0, func(i int) error {
    if i < 42 {
        return errors.New("failed")
    }

    return nil
})
// 43
// nil

For more advanced retry strategies (delay, exponential backoff...), please take a look at cenkalti/backoff.

[play]

AttemptWithDelay

Invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil.

When the first argument is less than 1, the function runs until a successful response is returned.

iter, duration, err := lo.AttemptWithDelay(5, 2*time.Second, func(i int, duration time.Duration) error {
    if i == 2 {
        return nil
    }

    return errors.New("failed")
})
// 3
// ~ 4 seconds
// nil

For more advanced retry strategies (delay, exponential backoff...), please take a look at cenkalti/backoff.

[play]

AttemptWhile

Invokes a function N times until it returns valid output. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false.

When the first argument is less than 1, the function runs until a successful response is returned.

count1, err1 := lo.AttemptWhile(5, func(i int) (error, bool) {
    err := doMockedHTTPRequest(i)
    if err != nil {
        if errors.Is(err, ErrBadRequest) { // let's assume ErrBadRequest is a critical error that needs to terminate the invoke
            return err, false // flag the second return value as false to terminate the invoke
        }

        return err, true
    }

    return nil, false
})

For more advanced retry strategies (delay, exponential backoff...), please take a look at cenkalti/backoff.

[play]

AttemptWhileWithDelay

Invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false.

When the first argument is less than 1, the function runs until a successful response is returned.

count1, time1, err1 := lo.AttemptWhileWithDelay(5, time.Millisecond, func(i int, d time.Duration) (error, bool) {
    err := doMockedHTTPRequest(i)
    if err != nil {
        if errors.Is(err, ErrBadRequest) { // let's assume ErrBadRequest is a critical error that needs to terminate the invoke
            return err, false // flag the second return value as false to terminate the invoke
        }

        return err, true
    }

    return nil, false
})

For more advanced retry strategies (delay, exponential backoff...), please take a look at cenkalti/backoff.

[play]

Debounce

NewDebounce creates a debounced instance that delays invoking functions given until after wait milliseconds have elapsed, until cancel is called.

f := func() {
    println("Called once after 100ms when debounce stopped invoking!")
}

debounce, cancel := lo.NewDebounce(100 * time.Millisecond, f)
for j := 0; j < 10; j++ {
    debounce()
}

time.Sleep(1 * time.Second)
cancel()

[play]

DebounceBy

NewDebounceBy creates a debounced instance for each distinct key, that delays invoking functions given until after wait milliseconds have elapsed, until cancel is called.

f := func(key string, count int) {
    println(key + ": Called once after 100ms when debounce stopped invoking!")
}

debounce, cancel := lo.NewDebounceBy(100 * time.Millisecond, f)
for j := 0; j < 10; j++ {
    debounce("first key")
    debounce("second key")
}

time.Sleep(1 * time.Second)
cancel("first key")
cancel("second key")

[play]

Throttle

Creates a throttled instance that invokes given functions only once in every interval.

This returns 2 functions, First one is throttled function and Second one is a function to reset interval.

f := func() {
	println("Called once in every 100ms")
}

throttle, reset := lo.NewThrottle(100 * time.Millisecond, f)

for j := 0; j < 10; j++ {
	throttle()
	time.Sleep(30 * time.Millisecond)
}

reset()
throttle()

NewThrottleWithCount is NewThrottle with count limit, throttled function will be invoked count times in every interval.

f := func() {
	println("Called three times in every 100ms")
}

throttle, reset := lo.NewThrottleWithCount(100 * time.Millisecond, f)

for j := 0; j < 10; j++ {
	throttle()
	time.Sleep(30 * time.Millisecond)
}

reset()
throttle()

NewThrottleBy and NewThrottleByWithCount are NewThrottle with sharding key, throttled function will be invoked count times in every interval.

f := func(key string) {
	println(key, "Called three times in every 100ms")
}

throttle, reset := lo.NewThrottleByWithCount(100 * time.Millisecond, f)

for j := 0; j < 10; j++ {
	throttle("foo")
	time.Sleep(30 * time.Millisecond)
}

reset()
throttle()
Synchronize

Wraps the underlying callback in a mutex. It receives an optional mutex.

s := lo.Synchronize()

for i := 0; i < 10; i++ {
    go s.Do(func () {
        println("will be called sequentially")
    })
}

It is equivalent to:

mu := sync.Mutex{}

func foobar() {
    mu.Lock()
    defer mu.Unlock()

    // ...
}
Async

Executes a function in a goroutine and returns the result in a channel.

ch := lo.Async(func() error { time.Sleep(10 * time.Second); return nil })
// chan error (nil)
Async{0->6}

Executes a function in a goroutine and returns the result in a channel. For functions with multiple return values, the results will be returned as a tuple inside the channel. For functions without return, struct{} will be returned in the channel.

ch := lo.Async0(func() { time.Sleep(10 * time.Second) })
// chan struct{}

ch := lo.Async1(func() int {
  time.Sleep(10 * time.Second);
  return 42
})
// chan int (42)

ch := lo.Async2(func() (int, string) {
  time.Sleep(10 * time.Second);
  return 42, "Hello"
})
// chan lo.Tuple2[int, string] ({42, "Hello"})
Transaction

Implements a Saga pattern.

transaction := NewTransaction().
    Then(
        func(state int) (int, error) {
            fmt.Println("step 1")
            return state + 10, nil
        },
        func(state int) int {
            fmt.Println("rollback 1")
            return state - 10
        },
    ).
    Then(
        func(state int) (int, error) {
            fmt.Println("step 2")
            return state + 15, nil
        },
        func(state int) int {
            fmt.Println("rollback 2")
            return state - 15
        },
    ).
    Then(
        func(state int) (int, error) {
            fmt.Println("step 3")

            if true {
                return state, errors.New("error")
            }

            return state + 42, nil
        },
        func(state int) int {
            fmt.Println("rollback 3")
            return state - 42
        },
    )

_, _ = transaction.Process(-5)

// Output:
// step 1
// step 2
// step 3
// rollback 2
// rollback 1
WaitFor

Runs periodically until a condition is validated.

alwaysTrue := func(i int) bool { return true }
alwaysFalse := func(i int) bool { return false }
laterTrue := func(i int) bool {
    return i > 5
}

iterations, duration, ok := lo.WaitFor(alwaysTrue, 10*time.Millisecond, 2 * time.Millisecond)
// 1
// 1ms
// true

iterations, duration, ok := lo.WaitFor(alwaysFalse, 10*time.Millisecond, time.Millisecond)
// 10
// 10ms
// false

iterations, duration, ok := lo.WaitFor(laterTrue, 10*time.Millisecond, time.Millisecond)
// 7
// 7ms
// true

iterations, duration, ok := lo.WaitFor(laterTrue, 10*time.Millisecond, 5*time.Millisecond)
// 2
// 10ms
// false

[play]

WaitForWithContext

Runs periodically until a condition is validated or context is invalid.

The condition receives also the context, so it can invalidate the process in the condition checker

ctx := context.Background()

alwaysTrue := func(_ context.Context, i int) bool { return true }
alwaysFalse := func(_ context.Context, i int) bool { return false }
laterTrue := func(_ context.Context, i int) bool {
    return i >= 5
}

iterations, duration, ok := lo.WaitForWithContext(ctx, alwaysTrue, 10*time.Millisecond, 2 * time.Millisecond)
// 1
// 1ms
// true

iterations, duration, ok := lo.WaitForWithContext(ctx, alwaysFalse, 10*time.Millisecond, time.Millisecond)
// 10
// 10ms
// false

iterations, duration, ok := lo.WaitForWithContext(ctx, laterTrue, 10*time.Millisecond, time.Millisecond)
// 5
// 5ms
// true

iterations, duration, ok := lo.WaitForWithContext(ctx, laterTrue, 10*time.Millisecond, 5*time.Millisecond)
// 2
// 10ms
// false

expiringCtx, cancel := context.WithTimeout(ctx, 5*time.Millisecond)
iterations, duration, ok := lo.WaitForWithContext(expiringCtx, alwaysFalse, 100*time.Millisecond, time.Millisecond)
// 5
// 5.1ms
// false

[play]

Validate

Helper function that creates an error when a condition is not met.

slice := []string{"a"}
val := lo.Validate(len(slice) == 0, "Slice should be empty but contains %v", slice)
// error("Slice should be empty but contains [a]")

slice := []string{}
val := lo.Validate(len(slice) == 0, "Slice should be empty but contains %v", slice)
// nil

[play]

Must

Wraps a function call and panics if second argument is error or false, returns the value otherwise.

val := lo.Must(time.Parse("2006-01-02", "2022-01-15"))
// 2022-01-15

val := lo.Must(time.Parse("2006-01-02", "bad-value"))
// panics

[play]

Must{0->6}

Must* has the same behavior as Must but returns multiple values.

func example0() (error)
func example1() (int, error)
func example2() (int, string, error)
func example3() (int, string, time.Date, error)
func example4() (int, string, time.Date, bool, error)
func example5() (int, string, time.Date, bool, float64, error)
func example6() (int, string, time.Date, bool, float64, byte, error)

lo.Must0(example0())
val1 := lo.Must1(example1())    // alias to Must
val1, val2 := lo.Must2(example2())
val1, val2, val3 := lo.Must3(example3())
val1, val2, val3, val4 := lo.Must4(example4())
val1, val2, val3, val4, val5 := lo.Must5(example5())
val1, val2, val3, val4, val5, val6 := lo.Must6(example6())

You can wrap functions like func (...) (..., ok bool).

// math.Signbit(float64) bool
lo.Must0(math.Signbit(v))

// bytes.Cut([]byte,[]byte) ([]byte, []byte, bool)
before, after := lo.Must2(bytes.Cut(s, sep))

You can give context to the panic message by adding some printf-like arguments.

val, ok := lo.Find(myString, func(i string) bool {
    return i == requiredChar
})
lo.Must0(ok, "'%s' must always contain '%s'", myString, requiredChar)

list := []int{0, 1, 2}
item := 5
lo.Must0(lo.Contains(list, item), "'%s' must always contain '%s'", list, item)
...

[play]

Try

Calls the function and returns false in case of error and panic.

ok := lo.Try(func() error {
    panic("error")
    return nil
})
// false

ok := lo.Try(func() error {
    return nil
})
// true

ok := lo.Try(func() error {
    return errors.New("error")
})
// false

[play]

Try{0->6}

The same behavior as Try, but the callback returns 2 variables.

ok := lo.Try2(func() (string, error) {
    panic("error")
    return "", nil
})
// false

[play]

TryOr

Calls the function and return a default value in case of error and on panic.

str, ok := lo.TryOr(func() (string, error) {
    panic("error")
    return "hello", nil
}, "world")
// world
// false

str, ok := lo.TryOr(func() error {
    return "hello", nil
}, "world")
// hello
// true

str, ok := lo.TryOr(func() error {
    return "hello", errors.New("error")
}, "world")
// world
// false

[play]

TryOr{0->6}

The same behavior as TryOr, but the callback returns X variables.

str, nbr, ok := lo.TryOr2(func() (string, int, error) {
    panic("error")
    return "hello", 42, nil
}, "world", 21)
// world
// 21
// false

[play]

TryWithErrorValue

The same behavior as Try, but also returns the value passed to panic.

err, ok := lo.TryWithErrorValue(func() error {
    panic("error")
    return nil
})
// "error", false

[play]

TryCatch

The same behavior as Try, but calls the catch function in case of error.

caught := false

ok := lo.TryCatch(func() error {
    panic("error")
    return nil
}, func() {
    caught = true
})
// false
// caught == true

[play]

TryCatchWithErrorValue

The same behavior as TryWithErrorValue, but calls the catch function in case of error.

caught := false

ok := lo.TryCatchWithErrorValue(func() error {
    panic("error")
    return nil
}, func(val any) {
    caught = val == "error"
})
// false
// caught == true

[play]

ErrorsAs

A shortcut for:

err := doSomething()

var rateLimitErr *RateLimitError
if ok := errors.As(err, &rateLimitErr); ok {
    // retry later
}

single line lo helper:

err := doSomething()

if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok {
    // retry later
}

[play]

Assert

Does nothing when the condition is true, otherwise it panics with an optional message.

Think twice before using it, given that Go intentionally omits assertions from its standard library.

age := getUserAge()

lo.Assert(age >= 15)
age := getUserAge()

lo.Assert(age >= 15, "user age must be >= 15")

[play]

Assertf

Like Assert, but with fmt.Printf-like formatting.

Think twice before using it, given that Go intentionally omits assertions from its standard library.

age := getUserAge()

lo.Assertf(age >= 15, "user age must be >= 15, got %d", age)

[play]

🛩 Benchmark

We executed a simple benchmark with a dead-simple lo.Map loop:

See the full implementation here.

_ = lo.Map[int64](arr, func(x int64, i int) string {
    return strconv.FormatInt(x, 10)
})

Result:

Here is a comparison between lo.Map, lop.Map, go-funk library and a simple Go for loop.

$ go test -benchmem -bench ./...
goos: linux
goarch: amd64
pkg: github.com/samber/lo
cpu: Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
cpu: Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz
BenchmarkMap/lo.Map-8         	       8	 132728237 ns/op	39998945 B/op	 1000002 allocs/op
BenchmarkMap/lop.Map-8        	       2	 503947830 ns/op	119999956 B/op	 3000007 allocs/op
BenchmarkMap/reflect-8        	       2	 826400560 ns/op	170326512 B/op	 4000042 allocs/op
BenchmarkMap/for-8            	       9	 126252954 ns/op	39998674 B/op	 1000001 allocs/op
PASS
ok  	github.com/samber/lo	6.657s
  • lo.Map is way faster (x7) than go-funk, a reflection-based Map implementation.
  • lo.Map has the same allocation profile as for.
  • lo.Map is 4% slower than for.
  • lop.Map is slower than lo.Map because it implies more memory allocation and locks. lop.Map is useful for long-running callbacks, such as i/o bound processing.
  • for beats other implementations for memory and CPU.

🤝 Contributing

Don't hesitate ;)

Helper naming: helpers must be self-explanatory and respect standards (other languages, libraries...). Feel free to suggest many names in your contributions.

# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

👤 Contributors

Contributors

💫 Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

📝 License

Copyright © 2022 Samuel Berthe.

This project is under MIT license.

Documentation

Overview

Example (IfElse_Else)
result1 := If(true, 1).
	ElseIf(false, 2).
	Else(3)

result2 := If(false, 1).
	ElseIf(true, 2).
	Else(3)

result3 := If(false, 1).
	ElseIf(false, 2).
	Else(3)

result4 := IfF(true, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

result5 := IfF(false, func() int { return 1 }).
	ElseIfF(true, func() int { return 2 }).
	ElseF(func() int { return 3 })

result6 := IfF(false, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3
Example (IfElse_ElseF)
result1 := If(true, 1).
	ElseIf(false, 2).
	Else(3)

result2 := If(false, 1).
	ElseIf(true, 2).
	Else(3)

result3 := If(false, 1).
	ElseIf(false, 2).
	Else(3)

result4 := IfF(true, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

result5 := IfF(false, func() int { return 1 }).
	ElseIfF(true, func() int { return 2 }).
	ElseF(func() int { return 3 })

result6 := IfF(false, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3
Example (IfElse_ElseIf)
result1 := If(true, 1).
	ElseIf(false, 2).
	Else(3)

result2 := If(false, 1).
	ElseIf(true, 2).
	Else(3)

result3 := If(false, 1).
	ElseIf(false, 2).
	Else(3)

result4 := IfF(true, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

result5 := IfF(false, func() int { return 1 }).
	ElseIfF(true, func() int { return 2 }).
	ElseF(func() int { return 3 })

result6 := IfF(false, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3
Example (IfElse_ElseIfF)
result1 := If(true, 1).
	ElseIf(false, 2).
	Else(3)

result2 := If(false, 1).
	ElseIf(true, 2).
	Else(3)

result3 := If(false, 1).
	ElseIf(false, 2).
	Else(3)

result4 := IfF(true, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

result5 := IfF(false, func() int { return 1 }).
	ElseIfF(true, func() int { return 2 }).
	ElseF(func() int { return 3 })

result6 := IfF(false, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3
Example (SwitchCase_Case)
result1 := Switch[int, string](1).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result2 := Switch[int, string](2).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result3 := Switch[int, string](42).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result4 := Switch[int, string](1).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result5 := Switch[int, string](2).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result6 := Switch[int, string](42).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3
Example (SwitchCase_CaseF)
result1 := Switch[int, string](1).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result2 := Switch[int, string](2).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result3 := Switch[int, string](42).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result4 := Switch[int, string](1).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result5 := Switch[int, string](2).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result6 := Switch[int, string](42).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3
Example (SwitchCase_Default)
result1 := Switch[int, string](1).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result2 := Switch[int, string](2).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result3 := Switch[int, string](42).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result4 := Switch[int, string](1).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result5 := Switch[int, string](2).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result6 := Switch[int, string](42).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3
Example (SwitchCase_DefaultF)
result1 := Switch[int, string](1).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result2 := Switch[int, string](2).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result3 := Switch[int, string](42).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result4 := Switch[int, string](1).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result5 := Switch[int, string](2).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result6 := Switch[int, string](42).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	//nolint:revive
	LowerCaseLettersCharset = []rune("abcdefghijklmnopqrstuvwxyz")
	UpperCaseLettersCharset = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	LettersCharset          = append(LowerCaseLettersCharset, UpperCaseLettersCharset...)
	NumbersCharset          = []rune("0123456789")
	AlphanumericCharset     = append(LettersCharset, NumbersCharset...)
	SpecialCharset          = []rune("!@#$%^&*()_+-=[]{}|;':\",./<>?")
	AllCharset              = append(AlphanumericCharset, SpecialCharset...)
)

Functions

func Assert added in v1.51.0

func Assert(condition bool, message ...string)

Assert does nothing when the condition is true, otherwise it panics with an optional message. Play: https://go.dev/play/p/Xv8LLKBMNwI

Example
defer func() {
	if r := recover(); r != nil {
		fmt.Println(r)
	}
}()

age := 20

// won't panic
Assert(age >= 18)

// won't panic
Assert(age >= 18, "age must be at least 18")

// will panic
Assert(age < 18)

// will panic
Assert(age < 18, "age must be less than 18")
Output:

assertion failed

func Assertf added in v1.51.0

func Assertf(condition bool, format string, args ...any)

Assertf does nothing when the condition is true, otherwise it panics with a formatted message. Play: https://go.dev/play/p/TVPEmVcyrdY

Example
defer func() {
	if r := recover(); r != nil {
		fmt.Println(r)
	}
}()

age := 20

// won't panic
Assertf(age >= 18, "age must be at least 18, got %d", age)

// will panic
Assertf(age < 18, "age must be less than 18, got %d", age)
Output:

assertion failed: age must be less than 18, got 20

func Assign

func Assign[K comparable, V any, Map ~map[K]V](maps ...Map) Map

Assign merges multiple maps from left to right. Play: https://go.dev/play/p/VhwfJOyxf5o

Example
result := Assign(
	map[string]int{"a": 1, "b": 2},
	map[string]int{"b": 3, "c": 4},
)

fmt.Printf("%v %v %v %v", len(result), result["a"], result["b"], result["c"])
Output:

3 1 3 4

func Associate added in v1.26.0

func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V

Associate returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original slice. Play: https://go.dev/play/p/WHa2CfMO3Lr

func AssociateI added in v1.52.0

func AssociateI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V)) map[K]V

AssociateI returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original slice. Play: https://go.dev/play/p/Ugmz6S22rRO

func Async added in v1.12.0

func Async[A any](f func() A) <-chan A

Async executes a function in a goroutine and returns the result in a channel. Play: https://go.dev/play/p/uo35gosuTLw

func Async0 added in v1.17.0

func Async0(f func()) <-chan struct{}

Async0 executes a function in a goroutine and returns a channel set once the function finishes. Play: https://go.dev/play/p/tNqf1cClG_o

func Async1 added in v1.17.0

func Async1[A any](f func() A) <-chan A

Async1 is an alias to Async. Play: https://go.dev/play/p/uo35gosuTLw

func Async2 added in v1.17.0

func Async2[A, B any](f func() (A, B)) <-chan Tuple2[A, B]

Async2 has the same behavior as Async, but returns the 2 results as a tuple inside the channel. Play: https://go.dev/play/p/7W7mKQi0AhA

func Async3 added in v1.17.0

func Async3[A, B, C any](f func() (A, B, C)) <-chan Tuple3[A, B, C]

Async3 has the same behavior as Async, but returns the 3 results as a tuple inside the channel. Play: https://go.dev/play/p/L1d6o6l6q0d

func Async4 added in v1.17.0

func Async4[A, B, C, D any](f func() (A, B, C, D)) <-chan Tuple4[A, B, C, D]

Async4 has the same behavior as Async, but returns the 4 results as a tuple inside the channel. Play: https://go.dev/play/p/1X7q6oL0TqF

func Async5 added in v1.17.0

func Async5[A, B, C, D, E any](f func() (A, B, C, D, E)) <-chan Tuple5[A, B, C, D, E]

Async5 has the same behavior as Async, but returns the 5 results as a tuple inside the channel. Play: https://go.dev/play/p/2W7q4oL1TqG

func Async6 added in v1.17.0

func Async6[A, B, C, D, E, F any](f func() (A, B, C, D, E, F)) <-chan Tuple6[A, B, C, D, E, F]

Async6 has the same behavior as Async, but returns the 6 results as a tuple inside the channel. Play: https://go.dev/play/p/3X8q5pM2UrH

func Attempt added in v1.5.0

func Attempt(maxIteration int, f func(index int) error) (int, error)

Attempt invokes a function N times until it returns valid output. Returns either the caught error or nil. When the first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/3ggJZ2ZKcMj

Example
count1, err1 := Attempt(2, func(i int) error {
	if i == 0 {
		return errors.New("error")
	}

	return nil
})

count2, err2 := Attempt(2, func(i int) error {
	if i < 10 {
		return errors.New("error")
	}

	return nil
})

fmt.Printf("%v %v\n", count1, err1)
fmt.Printf("%v %v\n", count2, err2)
Output:

2 <nil>
2 error

func AttemptWhile added in v1.36.0

func AttemptWhile(maxIteration int, f func(int) (error, bool)) (int, error)

AttemptWhile invokes a function N times until it returns valid output. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false. When the first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/1VS7HxlYMOG

func AttemptWhileWithDelay added in v1.36.0

func AttemptWhileWithDelay(maxIteration int, delay time.Duration, f func(int, time.Duration) (error, bool)) (int, time.Duration, error)

AttemptWhileWithDelay invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false. When the first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/mhufUjJfLEF

func AttemptWithDelay added in v1.16.0

func AttemptWithDelay(maxIteration int, delay time.Duration, f func(index int, duration time.Duration) error) (int, time.Duration, error)

AttemptWithDelay invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil. When the first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/tVs6CygC7m1

Example
count1, time1, err1 := AttemptWithDelay(2, time.Millisecond, func(i int, _ time.Duration) error {
	if i == 0 {
		return errors.New("error")
	}

	return nil
})

count2, time2, err2 := AttemptWithDelay(2, time.Millisecond, func(i int, _ time.Duration) error {
	if i < 10 {
		return errors.New("error")
	}

	return nil
})

fmt.Printf("%v %v %v\n", count1, time1.Truncate(time.Millisecond), err1)
fmt.Printf("%v %v %v\n", count2, time2.Truncate(time.Millisecond), err2)
Output:

2 1ms <nil>
2 1ms error

func Batch deprecated added in v1.31.0

func Batch[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool)

Batch creates a slice of n elements from a channel. Returns the slice and the slice length.

Deprecated: Use Buffer instead.

func BatchWithTimeout deprecated added in v1.31.0

func BatchWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool)

BatchWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length.

Deprecated: Use BufferWithTimeout instead.

func Buffer added in v1.35.0

func Buffer[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool)

Buffer creates a slice of n elements from a channel. Returns the slice and the slice length. @TODO: we should probably provide a helper that reuses the same buffer. Play: https://go.dev/play/p/gPQ-6xmcKQI

func BufferWithContext added in v1.48.0

func BufferWithContext[T any](ctx context.Context, ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool)

BufferWithContext creates a slice of n elements from a channel, with context. Returns the slice and the slice length. @TODO: we should probably provide a helper that reuses the same buffer. Play: https://go.dev/play/p/oRfOyJWK9YF

func BufferWithTimeout added in v1.35.0

func BufferWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool)

BufferWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length. Play: https://go.dev/play/p/sxyEM3koo4n

func CamelCase added in v1.40.0

func CamelCase(str string) string

CamelCase converts string to camel case. Play: https://go.dev/play/p/Go6aKwUiq59

func Capitalize added in v1.40.0

func Capitalize(str string) string

Capitalize converts the first character of string to upper case and the remaining to lower case. Play: https://go.dev/play/p/uLTZZQXqnsa

func ChannelDispatcher added in v1.28.0

func ChannelDispatcher[T any](stream <-chan T, count, channelBufferCap int, strategy DispatchingStrategy[T]) []<-chan T

ChannelDispatcher distributes messages from input channels into N child channels. Close events are propagated to children. Underlying channels can have a fixed buffer capacity or be unbuffered when cap is 0. Play: https://go.dev/play/p/UZGu2wVg3J2

func ChannelMerge deprecated added in v1.33.0

func ChannelMerge[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T

ChannelMerge collects messages from multiple input channels into a single buffered channel. Output messages have no priority. When all upstream channels reach EOF, downstream channel closes.

Deprecated: Use FanIn instead.

func ChannelToSlice added in v1.32.0

func ChannelToSlice[T any](ch <-chan T) []T

ChannelToSlice returns a slice built from channel items. Blocks until channel closes. Play: https://go.dev/play/p/lIbSY3QmiEg

func Chunk

func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice

Chunk returns a slice of elements split into groups of length size. If the slice can't be split evenly, the final chunk will be the remaining elements. Play: https://go.dev/play/p/kEMkFbdu85g

Example
list := []int{0, 1, 2, 3, 4}

result := Chunk(list, 2)

for _, item := range result {
	fmt.Printf("%v\n", item)
}
Output:

[0 1]
[2 3]
[4]

func ChunkEntries added in v1.49.0

func ChunkEntries[K comparable, V any](m map[K]V, size int) []map[K]V

ChunkEntries splits a map into a slice of elements in groups of length equal to its size. If the map cannot be split evenly, the final chunk will contain the remaining elements. Play: https://go.dev/play/p/X_YQL6mmoD-

Example
result := ChunkEntries(
	map[string]int{
		"a": 1,
		"b": 2,
		"c": 3,
		"d": 4,
		"e": 5,
	},
	3,
)

for i := range result {
	fmt.Printf("%d\n", len(result[i]))
}
Output:

3
2

func ChunkString added in v1.27.0

func ChunkString[T ~string](str T, size int) []T

ChunkString returns a slice of strings split into groups of length size. If the string can't be split evenly, the final chunk will be the remaining characters. Play: https://go.dev/play/p/__FLTuJVz54

Example
result1 := ChunkString("123456", 2)
result2 := ChunkString("1234567", 2)
result3 := ChunkString("", 2)
result4 := ChunkString("1", 2)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
Output:

[12 34 56]
[12 34 56 7]
[]
[1]

func Clamp added in v1.13.0

func Clamp[T constraints.Ordered](value, mIn, mAx T) T

Clamp clamps number within the inclusive lower and upper bounds. Play: https://go.dev/play/p/RU4lJNC2hlI

Example
result1 := Clamp(0, -10, 10)
result2 := Clamp(-42, -10, 10)
result3 := Clamp(42, -10, 10)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
Output:

0
-10
10

func Coalesce added in v1.14.0

func Coalesce[T comparable](values ...T) (T, bool)

Coalesce returns the first non-empty arguments. Arguments must be comparable. Play: https://go.dev/play/p/Gyo9otyvFHH

func CoalesceMap added in v1.48.0

func CoalesceMap[K comparable, V any](v ...map[K]V) (map[K]V, bool)

CoalesceMap returns the first non-zero map. Play: https://go.dev/play/p/Gyo9otyvFHH

func CoalesceMapOrEmpty added in v1.48.0

func CoalesceMapOrEmpty[K comparable, V any](v ...map[K]V) map[K]V

CoalesceMapOrEmpty returns the first non-zero map. Play: https://go.dev/play/p/Gyo9otyvFHH

func CoalesceOrEmpty added in v1.41.0

func CoalesceOrEmpty[T comparable](v ...T) T

CoalesceOrEmpty returns the first non-empty arguments. Arguments must be comparable. Play: https://go.dev/play/p/Gyo9otyvFHH

func CoalesceSlice added in v1.48.0

func CoalesceSlice[T any](v ...[]T) ([]T, bool)

CoalesceSlice returns the first non-zero slice. Play: https://go.dev/play/p/Gyo9otyvFHH

func CoalesceSliceOrEmpty added in v1.48.0

func CoalesceSliceOrEmpty[T any](v ...[]T) []T

CoalesceSliceOrEmpty returns the first non-zero slice. Play: https://go.dev/play/p/Gyo9otyvFHH

func Compact added in v1.22.0

func Compact[T comparable, Slice ~[]T](collection Slice) Slice

Compact returns a slice of all non-zero elements. Play: https://go.dev/play/p/tXiy-iK6PAc

Example
list := []string{"", "foo", "", "bar", ""}

result := Compact(list)

fmt.Printf("%v", result)
Output:

[foo bar]

func Contains

func Contains[T comparable](collection []T, element T) bool

Contains returns true if an element is present in a collection. Play: https://go.dev/play/p/W1EvyqY6t9j

func ContainsBy added in v1.7.0

func ContainsBy[T any](collection []T, predicate func(item T) bool) bool

ContainsBy returns true if predicate function return true. Play: https://go.dev/play/p/W1EvyqY6t9j

func Count added in v1.12.0

func Count[T comparable](collection []T, value T) int

Count counts the number of elements in the collection that equal value. Play: https://go.dev/play/p/Y3FlK54yveC

Example
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}

result := Count(list, 2)

fmt.Printf("%v", result)
Output:

2

func CountBy added in v1.12.0

func CountBy[T any](collection []T, predicate func(item T) bool) int

CountBy counts the number of elements in the collection for which predicate is true. Play: https://go.dev/play/p/ByQbNYQQi4X

Example
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}

result := CountBy(list, func(i int) bool {
	return i < 4
})

fmt.Printf("%v", result)
Output:

8

func CountValues added in v1.32.0

func CountValues[T comparable](collection []T) map[T]int

CountValues counts the number of each element in the collection. Play: https://go.dev/play/p/-p-PyLT4dfy

Example
result1 := CountValues([]int{})
result2 := CountValues([]int{1, 2})
result3 := CountValues([]int{1, 2, 2})
result4 := CountValues([]string{"foo", "bar", ""})
result5 := CountValues([]string{"foo", "bar", "bar"})

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output:

map[]
map[1:1 2:1]
map[1:1 2:2]
map[:1 bar:1 foo:1]
map[bar:2 foo:1]

func CountValuesBy added in v1.32.0

func CountValuesBy[T any, U comparable](collection []T, mapper func(item T) U) map[U]int

CountValuesBy counts the number of each element returned from mapper function. Is equivalent to chaining lo.Map and lo.CountValues. Play: https://go.dev/play/p/2U0dG1SnOmS

Example
isEven := func(v int) bool {
	return v%2 == 0
}

result1 := CountValuesBy([]int{}, isEven)
result2 := CountValuesBy([]int{1, 2}, isEven)
result3 := CountValuesBy([]int{1, 2, 2}, isEven)

length := func(v string) int {
	return len(v)
}

result4 := CountValuesBy([]string{"foo", "bar", ""}, length)
result5 := CountValuesBy([]string{"foo", "bar", "bar"}, length)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output:

map[]
map[false:1 true:1]
map[false:1 true:2]
map[0:1 3:2]
map[3:3]

func CrossJoinBy2 added in v1.48.0

func CrossJoinBy2[A, B, Out any](listA []A, listB []B, project func(a A, b B) Out) []Out

CrossJoinBy2 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/8Y7btpvuA-C

Example
result := CrossJoinBy2([]string{"a", "b"}, []int{1, 2, 3, 4}, func(a string, b int) string {
	return fmt.Sprintf("%v-%v", a, b)
})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1
a-2
a-3
a-4
b-1
b-2
b-3
b-4

func CrossJoinBy3 added in v1.48.0

func CrossJoinBy3[A, B, C, Out any](listA []A, listB []B, listC []C, project func(a A, b B, c C) Out) []Out

CrossJoinBy3 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/3z4y5x6w7v8

Example
result := CrossJoinBy3([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, func(a string, b int, c bool) string {
	return fmt.Sprintf("%v-%v-%v", a, b, c)
})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true
a-1-false
a-2-true
a-2-false
a-3-true
a-3-false
a-4-true
a-4-false
b-1-true
b-1-false
b-2-true
b-2-false
b-3-true
b-3-false
b-4-true
b-4-false

func CrossJoinBy4 added in v1.48.0

func CrossJoinBy4[A, B, C, D, Out any](listA []A, listB []B, listC []C, listD []D, project func(a A, b B, c C, d D) Out) []Out

CrossJoinBy4 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/8b9c0d1e2f3

Example
result := CrossJoinBy4([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, func(a string, b int, c bool, d foo) string {
	return fmt.Sprintf("%v-%v-%v-%v", a, b, c, d)
})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}
a-1-false-{bar}
a-2-true-{bar}
a-2-false-{bar}
a-3-true-{bar}
a-3-false-{bar}
a-4-true-{bar}
a-4-false-{bar}
b-1-true-{bar}
b-1-false-{bar}
b-2-true-{bar}
b-2-false-{bar}
b-3-true-{bar}
b-3-false-{bar}
b-4-true-{bar}
b-4-false-{bar}

func CrossJoinBy5 added in v1.48.0

func CrossJoinBy5[A, B, C, D, E, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, project func(a A, b B, c C, d D, e E) Out) []Out

CrossJoinBy5 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/4g5h6i7j8k9

Example
result := CrossJoinBy5([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, func(a string, b int, c bool, d foo, e float64) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v", a, b, c, d, e)
})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2
a-1-false-{bar}-4.2
a-2-true-{bar}-4.2
a-2-false-{bar}-4.2
a-3-true-{bar}-4.2
a-3-false-{bar}-4.2
a-4-true-{bar}-4.2
a-4-false-{bar}-4.2
b-1-true-{bar}-4.2
b-1-false-{bar}-4.2
b-2-true-{bar}-4.2
b-2-false-{bar}-4.2
b-3-true-{bar}-4.2
b-3-false-{bar}-4.2
b-4-true-{bar}-4.2
b-4-false-{bar}-4.2

func CrossJoinBy6 added in v1.48.0

func CrossJoinBy6[A, B, C, D, E, F, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, project func(a A, b B, c C, d D, e E, f F) Out) []Out

CrossJoinBy6 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/1l2m3n4o5p6

Example
result := CrossJoinBy6([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, func(a string, b int, c bool, d foo, e float64, f string) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v-%v", a, b, c, d, e, f)
})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2-plop
a-1-false-{bar}-4.2-plop
a-2-true-{bar}-4.2-plop
a-2-false-{bar}-4.2-plop
a-3-true-{bar}-4.2-plop
a-3-false-{bar}-4.2-plop
a-4-true-{bar}-4.2-plop
a-4-false-{bar}-4.2-plop
b-1-true-{bar}-4.2-plop
b-1-false-{bar}-4.2-plop
b-2-true-{bar}-4.2-plop
b-2-false-{bar}-4.2-plop
b-3-true-{bar}-4.2-plop
b-3-false-{bar}-4.2-plop
b-4-true-{bar}-4.2-plop
b-4-false-{bar}-4.2-plop

func CrossJoinBy7 added in v1.48.0

func CrossJoinBy7[A, B, C, D, E, F, G, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, project func(a A, b B, c C, d D, e E, f F, g G) Out) []Out

CrossJoinBy7 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/7q8r9s0t1u2

Example
result := CrossJoinBy7([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, func(a string, b int, c bool, d foo, e float64, f string, g bool) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g)
})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2-plop-false
a-1-false-{bar}-4.2-plop-false
a-2-true-{bar}-4.2-plop-false
a-2-false-{bar}-4.2-plop-false
a-3-true-{bar}-4.2-plop-false
a-3-false-{bar}-4.2-plop-false
a-4-true-{bar}-4.2-plop-false
a-4-false-{bar}-4.2-plop-false
b-1-true-{bar}-4.2-plop-false
b-1-false-{bar}-4.2-plop-false
b-2-true-{bar}-4.2-plop-false
b-2-false-{bar}-4.2-plop-false
b-3-true-{bar}-4.2-plop-false
b-3-false-{bar}-4.2-plop-false
b-4-true-{bar}-4.2-plop-false
b-4-false-{bar}-4.2-plop-false

func CrossJoinBy8 added in v1.48.0

func CrossJoinBy8[A, B, C, D, E, F, G, H, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, project func(a A, b B, c C, d D, e E, f F, g G, h H) Out) []Out

CrossJoinBy8 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/3v4w5x6y7z8

Example
result := CrossJoinBy8([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, func(a string, b int, c bool, d foo, e float64, f string, g bool, h int) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g, h)
})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2-plop-false-42
a-1-false-{bar}-4.2-plop-false-42
a-2-true-{bar}-4.2-plop-false-42
a-2-false-{bar}-4.2-plop-false-42
a-3-true-{bar}-4.2-plop-false-42
a-3-false-{bar}-4.2-plop-false-42
a-4-true-{bar}-4.2-plop-false-42
a-4-false-{bar}-4.2-plop-false-42
b-1-true-{bar}-4.2-plop-false-42
b-1-false-{bar}-4.2-plop-false-42
b-2-true-{bar}-4.2-plop-false-42
b-2-false-{bar}-4.2-plop-false-42
b-3-true-{bar}-4.2-plop-false-42
b-3-false-{bar}-4.2-plop-false-42
b-4-true-{bar}-4.2-plop-false-42
b-4-false-{bar}-4.2-plop-false-42

func CrossJoinBy9 added in v1.48.0

func CrossJoinBy9[A, B, C, D, E, F, G, H, I, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, listI []I, project func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) []Out

CrossJoinBy9 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/9a0b1c2d3e4

Example
result := CrossJoinBy9([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"}, func(a string, b int, c bool, d foo, e float64, f string, g bool, h int, i string) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g, h, i)
})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2-plop-false-42-hello world
a-1-false-{bar}-4.2-plop-false-42-hello world
a-2-true-{bar}-4.2-plop-false-42-hello world
a-2-false-{bar}-4.2-plop-false-42-hello world
a-3-true-{bar}-4.2-plop-false-42-hello world
a-3-false-{bar}-4.2-plop-false-42-hello world
a-4-true-{bar}-4.2-plop-false-42-hello world
a-4-false-{bar}-4.2-plop-false-42-hello world
b-1-true-{bar}-4.2-plop-false-42-hello world
b-1-false-{bar}-4.2-plop-false-42-hello world
b-2-true-{bar}-4.2-plop-false-42-hello world
b-2-false-{bar}-4.2-plop-false-42-hello world
b-3-true-{bar}-4.2-plop-false-42-hello world
b-3-false-{bar}-4.2-plop-false-42-hello world
b-4-true-{bar}-4.2-plop-false-42-hello world
b-4-false-{bar}-4.2-plop-false-42-hello world

func Cut added in v1.52.0

func Cut[T comparable, Slice ~[]T](collection, separator Slice) (before, after Slice, found bool)

Cut slices collection around the first instance of separator, returning the part of collection before and after separator. The found result reports whether separator appears in collection. If separator does not appear in s, cut returns collection, empty slice of []T, false. Play: https://go.dev/play/p/GiL3qhpIP3f

Example
collection := []string{"a", "b", "c", "d", "e", "f", "g"}

// Test with valid separator
before, after, found := Cut(collection, []string{"b", "c", "d"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before, after, found)

// Test with separator not found
before2, after2, found2 := Cut(collection, []string{"z"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before2, after2, found2)

// Test with separator at beginning
before3, after3, found3 := Cut(collection, []string{"a", "b"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before3, after3, found3)
Output:

Before: [a], After: [e f g], Found: true
Before: [a b c d e f g], After: [], Found: false
Before: [], After: [c d e f g], Found: true

func CutPrefix added in v1.52.0

func CutPrefix[T comparable, Slice ~[]T](collection, separator Slice) (after Slice, found bool)

CutPrefix returns collection without the provided leading prefix []T and reports whether it found the prefix. If s doesn't start with prefix, CutPrefix returns collection, false. If prefix is the empty []T, CutPrefix returns collection, true. Play: https://go.dev/play/p/7Plak4a1ICl

Example
collection := []string{"a", "b", "c", "d", "e", "f", "g"}

// Test with valid prefix
after, found := CutPrefix(collection, []string{"a", "b", "c"})
fmt.Printf("After: %v, Found: %t\n", after, found)

// Test with prefix not found
after2, found2 := CutPrefix(collection, []string{"b"})
fmt.Printf("After: %v, Found: %t\n", after2, found2)

// Test with empty prefix
after3, found3 := CutPrefix(collection, []string{})
fmt.Printf("After: %v, Found: %t\n", after3, found3)
Output:

After: [d e f g], Found: true
After: [a b c d e f g], Found: false
After: [a b c d e f g], Found: true

func CutSuffix added in v1.52.0

func CutSuffix[T comparable, Slice ~[]T](collection, separator Slice) (before Slice, found bool)

CutSuffix returns collection without the provided ending suffix []T and reports whether it found the suffix. If s doesn't end with suffix, CutSuffix returns collection, false. If suffix is the empty []T, CutSuffix returns collection, true. Play: https://go.dev/play/p/7FKfBFvPTaT

Example
collection := []string{"a", "b", "c", "d", "e", "f", "g"}

// Test with valid suffix
before, found := CutSuffix(collection, []string{"f", "g"})
fmt.Printf("Before: %v, Found: %t\n", before, found)

// Test with suffix not found
before2, found2 := CutSuffix(collection, []string{"b"})
fmt.Printf("Before: %v, Found: %t\n", before2, found2)

// Test with empty suffix
before3, found3 := CutSuffix(collection, []string{})
fmt.Printf("Before: %v, Found: %t\n", before3, found3)
Output:

Before: [a b c d e], Found: true
Before: [a b c d e f g], Found: false
Before: [a b c d e f g], Found: true

func Difference

func Difference[T comparable, Slice ~[]T](list1, list2 Slice) (Slice, Slice)

Difference returns the difference between two collections. The first value is the collection of elements absent from list2. The second value is the collection of elements absent from list1. Play: https://go.dev/play/p/pKE-JgzqRpz

func DispatchingStrategyFirst added in v1.28.0

func DispatchingStrategyFirst[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyFirst distributes messages in the first non-full channel. If the capacity of the first channel is exceeded, the second channel will be selected and so on. Play: https://go.dev/play/p/OrJCvOmk42f

func DispatchingStrategyLeast added in v1.28.0

func DispatchingStrategyLeast[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyLeast distributes messages in the emptiest channel. Play: https://go.dev/play/p/ypy0jrRcEe7

func DispatchingStrategyMost added in v1.28.0

func DispatchingStrategyMost[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyMost distributes messages in the fullest channel. If the channel capacity is exceeded, the next channel will be selected and so on. Play: https://go.dev/play/p/erHHone7rF9

func DispatchingStrategyRandom added in v1.28.0

func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyRandom distributes messages in a random manner. If the channel capacity is exceeded, another random channel will be selected and so on. Play: https://go.dev/play/p/GEyGn3TdGk4

func DispatchingStrategyRoundRobin added in v1.28.0

func DispatchingStrategyRoundRobin[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyRoundRobin distributes messages in a rotating sequential manner. If the channel capacity is exceeded, the next channel will be selected and so on. Play: https://go.dev/play/p/UZGu2wVg3J2

func Drop added in v1.9.0

func Drop[T any, Slice ~[]T](collection Slice, n int) Slice

Drop drops n elements from the beginning of a slice. Play: https://go.dev/play/p/JswS7vXRJP2

Example
list := []int{0, 1, 2, 3, 4, 5}

result := Drop(list, 2)

fmt.Printf("%v", result)
Output:

[2 3 4 5]

func DropByIndex added in v1.44.0

func DropByIndex[T any, Slice ~[]T](collection Slice, indexes ...int) Slice

DropByIndex drops elements from a slice by the index. A negative index will drop elements from the end of the slice. Play: https://go.dev/play/p/bPIH4npZRxS

Example
list := []int{0, 1, 2, 3, 4, 5}

result := DropByIndex(list, 2)

fmt.Printf("%v", result)
Output:

[0 1 3 4 5]

func DropRight added in v1.9.0

func DropRight[T any, Slice ~[]T](collection Slice, n int) Slice

DropRight drops n elements from the end of a slice. Play: https://go.dev/play/p/GG0nXkSJJa3

Example
list := []int{0, 1, 2, 3, 4, 5}

result := DropRight(list, 2)

fmt.Printf("%v", result)
Output:

[0 1 2 3]

func DropRightWhile added in v1.9.0

func DropRightWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice

DropRightWhile drops elements from the end of a slice while the predicate returns true. Play: https://go.dev/play/p/3-n71oEC0Hz

Example
list := []int{0, 1, 2, 3, 4, 5}

result := DropRightWhile(list, func(val int) bool {
	return val > 2
})

fmt.Printf("%v", result)
Output:

[0 1 2]

func DropWhile added in v1.9.0

func DropWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice

DropWhile drops elements from the beginning of a slice while the predicate returns true. Play: https://go.dev/play/p/7gBPYw2IK16

Example
list := []int{0, 1, 2, 3, 4, 5}

result := DropWhile(list, func(val int) bool {
	return val < 2
})

fmt.Printf("%v", result)
Output:

[2 3 4 5]

func Duration added in v1.41.0

func Duration(cb func()) time.Duration

Duration returns the time taken to execute a function. Play: https://go.dev/play/p/HQfbBbAXaFP

func Duration0 added in v1.41.0

func Duration0(cb func()) time.Duration

Duration0 returns the time taken to execute a function. Play: https://go.dev/play/p/HQfbBbAXaFP

func Duration1 added in v1.41.0

func Duration1[A any](cb func() A) (A, time.Duration)

Duration1 returns the time taken to execute a function. Play: https://go.dev/play/p/HQfbBbAXaFP

func Duration10 added in v1.41.0

func Duration10[A, B, C, D, E, F, G, H, I, J any](cb func() (A, B, C, D, E, F, G, H, I, J)) (A, B, C, D, E, F, G, H, I, J, time.Duration)

Duration10 returns the time taken to execute a function. Play: https://go.dev/play/p/Y3n7oJXqJbk

func Duration2 added in v1.41.0

func Duration2[A, B any](cb func() (A, B)) (A, B, time.Duration)

Duration2 returns the time taken to execute a function. Play: https://go.dev/play/p/HQfbBbAXaFP

func Duration3 added in v1.41.0

func Duration3[A, B, C any](cb func() (A, B, C)) (A, B, C, time.Duration)

Duration3 returns the time taken to execute a function. Play: https://go.dev/play/p/xr863iwkAxQ

func Duration4 added in v1.41.0

func Duration4[A, B, C, D any](cb func() (A, B, C, D)) (A, B, C, D, time.Duration)

Duration4 returns the time taken to execute a function. Play: https://go.dev/play/p/xr863iwkAxQ

func Duration5 added in v1.41.0

func Duration5[A, B, C, D, E any](cb func() (A, B, C, D, E)) (A, B, C, D, E, time.Duration)

Duration5 returns the time taken to execute a function. Play: https://go.dev/play/p/xr863iwkAxQ

func Duration6 added in v1.41.0

func Duration6[A, B, C, D, E, F any](cb func() (A, B, C, D, E, F)) (A, B, C, D, E, F, time.Duration)

Duration6 returns the time taken to execute a function. Play: https://go.dev/play/p/mR4bTQKO-Tf

func Duration7 added in v1.41.0

func Duration7[A, B, C, D, E, F, G any](cb func() (A, B, C, D, E, F, G)) (A, B, C, D, E, F, G, time.Duration)

Duration7 returns the time taken to execute a function. Play: https://go.dev/play/p/jgIAcBWWInS

func Duration8 added in v1.41.0

func Duration8[A, B, C, D, E, F, G, H any](cb func() (A, B, C, D, E, F, G, H)) (A, B, C, D, E, F, G, H, time.Duration)

Duration8 returns the time taken to execute a function. Play: https://go.dev/play/p/T8kxpG1c5Na

func Duration9 added in v1.41.0

func Duration9[A, B, C, D, E, F, G, H, I any](cb func() (A, B, C, D, E, F, G, H, I)) (A, B, C, D, E, F, G, H, I, time.Duration)

Duration9 returns the time taken to execute a function. Play: https://go.dev/play/p/bg9ix2VrZ0j

func Earliest added in v1.41.0

func Earliest(times ...time.Time) time.Time

Earliest search the minimum time.Time of a collection. Returns zero value when the collection is empty.

Example
now := time.Now()
past := now.Add(-time.Hour)
future := now.Add(time.Hour)

result := Earliest(future, now, past)

fmt.Printf("%t", result.Equal(past))
Output:

true

func EarliestBy added in v1.45.0

func EarliestBy[T any](collection []T, iteratee func(item T) time.Time) T

EarliestBy search the minimum time.Time of a collection using the given iteratee function. Returns zero value when the collection is empty.

Example
type Event struct {
	Name string
	Time time.Time
}

now := time.Now()
events := []Event{
	{Name: "Event A", Time: now.Add(time.Hour)},
	{Name: "Event B", Time: now},
	{Name: "Event C", Time: now.Add(-time.Hour)},
}

result := EarliestBy(events, func(event Event) time.Time {
	return event.Time
})

fmt.Printf("%s", result.Name)
Output:

Event C

func ElementsMatch added in v1.50.0

func ElementsMatch[T comparable, Slice ~[]T](list1, list2 Slice) bool

ElementsMatch returns true if lists contain the same set of elements (including empty set). If there are duplicate elements, the number of occurrences in each list should match. The order of elements is not checked. Play: https://go.dev/play/p/XWSEM4Ic_t0

func ElementsMatchBy added in v1.50.0

func ElementsMatchBy[T any, K comparable](list1, list2 []T, iteratee func(item T) K) bool

ElementsMatchBy returns true if lists contain the same set of elements' keys (including empty set). If there are duplicate keys, the number of occurrences in each list should match. The order of elements is not checked. Play: https://go.dev/play/p/XWSEM4Ic_t0

func Elipse deprecated added in v1.41.0

func Elipse(str string, length int) string

Elipse trims and truncates a string to a specified length and appends an ellipsis if truncated.

Deprecated: Use Ellipsis instead.

func Ellipsis added in v1.48.0

func Ellipsis(str string, length int) string

Ellipsis trims and truncates a string to a specified length **in bytes** and appends an ellipsis if truncated. If the string contains non-ASCII characters (which may occupy multiple bytes in UTF-8), truncating by byte length may split a character in the middle, potentially resulting in garbled output. Play: https://go.dev/play/p/qE93rgqe1TW

func EmptyableToPtr added in v1.38.0

func EmptyableToPtr[T any](x T) *T

EmptyableToPtr returns a pointer copy of value if it's nonzero. Otherwise, returns nil pointer. Play: https://go.dev/play/p/P2sD0PMXw4F

func ErrorsAs added in v1.29.0

func ErrorsAs[T error](err error) (T, bool)

ErrorsAs is a shortcut for errors.As(err, &&T). Play: https://go.dev/play/p/8wk5rH8UfrE

Example
doSomething := func() error {
	return &myError{}
}

err := doSomething()

if rateLimitErr, ok := ErrorsAs[*myError](err); ok {
	fmt.Printf("is type myError, err: %s", rateLimitErr.Error())
} else {
	fmt.Printf("is not type myError")
}
Output:

is type myError, err: my error

func Every

func Every[T comparable](collection, subset []T) bool

Every returns true if all elements of a subset are contained in a collection or if the subset is empty. Play: https://go.dev/play/p/W1EvyqY6t9j

func EveryBy added in v1.18.0

func EveryBy[T any](collection []T, predicate func(item T) bool) bool

EveryBy returns true if the predicate returns true for all elements in the collection or if the collection is empty. Play: https://go.dev/play/p/dn1-vhHsq9x

func FanIn added in v1.34.0

func FanIn[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T

FanIn collects messages from multiple input channels into a single buffered channel. Output messages have no priority. When all upstream channels reach EOF, downstream channel closes. Play: https://go.dev/play/p/FH8Wq-T04Jb

func FanOut added in v1.34.0

func FanOut[T any](count, channelsBufferCap int, upstream <-chan T) []<-chan T

FanOut broadcasts all the upstream messages to multiple downstream channels. When upstream channel reaches EOF, downstream channels close. If any downstream channels is full, broadcasting is paused. Play: https://go.dev/play/p/2LHxcjKX23L

func Fill

func Fill[T Clonable[T], Slice ~[]T](collection Slice, initial T) Slice

Fill fills elements of a slice with `initial` value. Play: https://go.dev/play/p/VwR34GzqEub

Example
list := []foo{{"a"}, {"a"}}

result := Fill(list, foo{"b"})

fmt.Printf("%v", result)
Output:

[{b} {b}]

func Filter

func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice

Filter iterates over elements of collection, returning a slice of all elements predicate returns true for. Play: https://go.dev/play/p/Apjg3WeSi7K

Example
list := []int64{1, 2, 3, 4}

result := Filter(list, func(nbr int64, index int) bool {
	return nbr%2 == 0
})

fmt.Printf("%v", result)
Output:

[2 4]

func FilterKeys added in v1.52.0

func FilterKeys[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []K

FilterKeys transforms a map into a slice based on predicate returns true for specific elements. It is a mix of lo.Filter() and lo.Keys(). Play: https://go.dev/play/p/OFlKXlPrBAe

Example
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}

result := FilterKeys(kv, func(k int, v string) bool {
	return v == "foo"
})

fmt.Printf("%v", result)
Output:

[1]

func FilterMap added in v1.12.0

func FilterMap[T, R any](collection []T, callback func(item T, index int) (R, bool)) []R

FilterMap returns a slice obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/CgHYNUpOd1I

Example
list := []int64{1, 2, 3, 4}

result := FilterMap(list, func(nbr int64, index int) (string, bool) {
	return strconv.FormatInt(nbr*2, 10), nbr%2 == 0
})

fmt.Printf("%v", result)
Output:

[4 8]

func FilterMapToSlice added in v1.50.0

func FilterMapToSlice[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) (R, bool)) []R

FilterMapToSlice transforms a map into a slice based on specified iteratee. The iteratee returns a value and a boolean. If the boolean is true, the value is added to the result slice. If the boolean is false, the value is not added to the result slice. The order of the keys in the input map is not specified and the order of the keys in the output slice is not guaranteed. Play: https://go.dev/play/p/jgsD_Kil9pV

Example
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}

result := FilterMapToSlice(kv, func(k int, v int64) (string, bool) {
	return fmt.Sprintf("%d_%d", k, v), k%2 == 0
})

sort.Strings(result)
fmt.Printf("%v", result)
Output:

[2_2 4_4]

func FilterReject added in v1.41.0

func FilterReject[T any, Slice ~[]T](collection Slice, predicate func(T, int) bool) (kept, rejected Slice)

FilterReject mixes Filter and Reject, this method returns two slices, one for the elements of collection that predicate returns true for and one for the elements that predicate does not return true for. Play: https://go.dev/play/p/lHSEGSznJjB

func FilterSliceToMap added in v1.49.0

func FilterSliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V, bool)) map[K]V

FilterSliceToMap returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original slice. The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map. Play: https://go.dev/play/p/2z0rDz2ZSGU

Example
list := []string{"a", "aa", "aaa"}

result := FilterSliceToMap(list, func(str string) (string, int, bool) {
	return str, len(str), len(str) > 1
})

fmt.Printf("%v", result)
Output:

map[aa:2 aaa:3]

func FilterSliceToMapI added in v1.52.0

func FilterSliceToMapI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V, bool)) map[K]V

FilterSliceToMapI returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original slice. The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map. Play: https://go.dev/play/p/mSz_bUIk9aJ

func FilterValues added in v1.52.0

func FilterValues[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []V

FilterValues transforms a map into a slice based on predicate returns true for specific elements. It is a mix of lo.Filter() and lo.Values(). Play: https://go.dev/play/p/YVD5r_h-LX-

Example
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}

result := FilterValues(kv, func(k int, v string) bool {
	return v == "foo"
})

fmt.Printf("%v", result)
Output:

[foo]

func Find

func Find[T any](collection []T, predicate func(item T) bool) (T, bool)

Find searches for an element in a slice based on a predicate. Returns element and true if element was found. Play: https://go.dev/play/p/Eo7W0lvKTky

Example
type User struct {
	Name string
	Age  int
}

users := []User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
}

result, found := Find(users, func(user User) bool {
	return user.Age > 30
})

fmt.Printf("%s %t", result.Name, found)
Output:

Charlie true
Example (NotFound)
list := []int{1, 2, 3, 4, 5}

result, found := Find(list, func(n int) bool {
	return n > 10
})

fmt.Printf("%d %t", result, found)
Output:

0 false

func FindDuplicates added in v1.25.0

func FindDuplicates[T comparable, Slice ~[]T](collection Slice) Slice

FindDuplicates returns a slice with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order they occur in the collection.

Example
list := []int{1, 2, 2, 3, 3, 3, 4, 5}

result := FindDuplicates(list)

fmt.Printf("%v", result)
Output:

[2 3]

func FindDuplicatesBy added in v1.25.0

func FindDuplicatesBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice

FindDuplicatesBy returns a slice with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order they occur in the slice. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed.

Example
type User struct {
	Name string
	Age  int
}

users := []User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 25},
	{Name: "David", Age: 30},
	{Name: "Eve", Age: 35},
}

result := FindDuplicatesBy(users, func(user User) int {
	return user.Age
})

fmt.Printf("%d", len(result))
Output:

2

func FindIndexOf added in v1.12.0

func FindIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool)

FindIndexOf searches for an element in a slice based on a predicate and returns the index and true. Returns -1 and false if the element is not found. Play: https://go.dev/play/p/XWSEM4Ic_t0

Example
list := []int{1, 2, 3, 4, 5}

result, index, found := FindIndexOf(list, func(n int) bool {
	return n > 2
})

fmt.Printf("%d %d %t", result, index, found)
Output:

3 2 true
Example (NotFound)
list := []int{1, 2, 3, 4, 5}

result, index, found := FindIndexOf(list, func(n int) bool {
	return n > 10
})

fmt.Printf("%d %d %t", result, index, found)
Output:

0 -1 false

func FindKey added in v1.23.0

func FindKey[K, V comparable](object map[K]V, value V) (K, bool)

FindKey returns the key of the first value matching. Play: https://go.dev/play/p/Bg0w1VDPYXx

Example
users := map[string]int{
	"Alice":   25,
	"Bob":     30,
	"Charlie": 35,
}

key, found := FindKey(users, 30)

fmt.Printf("%s %t", key, found)
Output:

Bob true
Example (NotFound)
users := map[string]int{
	"Alice":   25,
	"Bob":     30,
	"Charlie": 35,
}

key, found := FindKey(users, 40)

fmt.Printf("%s %t", key, found)
Output:

false

func FindKeyBy added in v1.23.0

func FindKeyBy[K comparable, V any](object map[K]V, predicate func(key K, value V) bool) (K, bool)

FindKeyBy returns the key of the first element predicate returns true for. Play: https://go.dev/play/p/9IbiPElcyo8

Example
users := map[string]int{
	"Alice":   25,
	"Bob":     30,
	"Charlie": 35,
}

key, found := FindKeyBy(users, func(name string, age int) bool {
	return age > 30
})

fmt.Printf("%s %t", key, found)
Output:

Charlie true
Example (NotFound)
users := map[string]int{
	"Alice":   25,
	"Bob":     30,
	"Charlie": 35,
}

key, found := FindKeyBy(users, func(name string, age int) bool {
	return age > 40
})

fmt.Printf("%s %t", key, found)
Output:

false

func FindLastIndexOf added in v1.12.0

func FindLastIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool)

FindLastIndexOf searches for the last element in a slice based on a predicate and returns the index and true. Returns -1 and false if the element is not found. Play: https://go.dev/play/p/dPiMRtJ6cUx

Example
list := []int{1, 2, 3, 4, 3, 5}

result, index, found := FindLastIndexOf(list, func(n int) bool {
	return n == 3
})

fmt.Printf("%d %d %t", result, index, found)
Output:

3 4 true
Example (NotFound)
list := []int{1, 2, 3, 4, 5}

result, index, found := FindLastIndexOf(list, func(n int) bool {
	return n > 10
})

fmt.Printf("%d %d %t", result, index, found)
Output:

0 -1 false

func FindOrElse added in v1.12.0

func FindOrElse[T any](collection []T, fallback T, predicate func(item T) bool) T

FindOrElse searches for an element in a slice based on a predicate. Returns the element if found or a given fallback value otherwise. Play: https://go.dev/play/p/Eo7W0lvKTky

Example
list := []int{1, 2, 3, 4, 5}

result := FindOrElse(list, -1, func(n int) bool {
	return n > 10
})

fmt.Printf("%d", result)
Output:

-1
Example (Found)
list := []int{1, 2, 3, 4, 5}

result := FindOrElse(list, -1, func(n int) bool {
	return n > 3
})

fmt.Printf("%d", result)
Output:

4

func FindUniques added in v1.25.0

func FindUniques[T comparable, Slice ~[]T](collection Slice) Slice

FindUniques returns a slice with all the elements that appear in the collection only once. The order of result values is determined by the order they occur in the collection.

Example
list := []int{1, 2, 2, 3, 3, 3, 4, 5}

result := FindUniques(list)

fmt.Printf("%v", result)
Output:

[1 4 5]

func FindUniquesBy added in v1.25.0

func FindUniquesBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice

FindUniquesBy returns a slice with all the elements that appear in the collection only once. The order of result values is determined by the order they occur in the slice. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed.

Example
type User struct {
	Name string
	Age  int
}

users := []User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 25},
	{Name: "David", Age: 30},
	{Name: "Eve", Age: 35},
}

result := FindUniquesBy(users, func(user User) int {
	return user.Age
})

fmt.Printf("%d", len(result))
Output:

1

func First added in v1.42.0

func First[T any](collection []T) (T, bool)

First returns the first element of a collection and check for availability of the first element. Play: https://go.dev/play/p/ul45Z0y2EFO

Example
list := []int{1, 2, 3, 4, 5}

result, found := First(list)

fmt.Printf("%d %t", result, found)
Output:

1 true
Example (Empty)
list := []int{}

result, found := First(list)

fmt.Printf("%d %t", result, found)
Output:

0 false

func FirstOr added in v1.42.0

func FirstOr[T any](collection []T, fallback T) T

FirstOr returns the first element of a collection or the fallback value if empty. Play: https://go.dev/play/p/ul45Z0y2EFO

Example
list := []int{1, 2, 3, 4, 5}

result := FirstOr(list, -1)

fmt.Printf("%d", result)
Output:

1
Example (Empty)
list := []int{}

result := FirstOr(list, -1)

fmt.Printf("%d", result)
Output:

-1

func FirstOrEmpty added in v1.42.0

func FirstOrEmpty[T any](collection []T) T

FirstOrEmpty returns the first element of a collection or zero value if empty. Play: https://go.dev/play/p/ul45Z0y2EFO

Example
list := []int{1, 2, 3, 4, 5}

result := FirstOrEmpty(list)

fmt.Printf("%d", result)
Output:

1
Example (Empty)
list := []int{}

result := FirstOrEmpty(list)

fmt.Printf("%d", result)
Output:

0

func FlatMap added in v1.7.0

func FlatMap[T, R any](collection []T, iteratee func(item T, index int) []R) []R

FlatMap manipulates a slice and transforms and flattens it to a slice of another type. The transform function can either return a slice or a `nil`, and in the `nil` case no value is added to the final slice. Play: https://go.dev/play/p/pFCF5WVB225

Example
list := []int64{1, 2, 3, 4}

result := FlatMap(list, func(nbr int64, index int) []string {
	return []string{
		strconv.FormatInt(nbr, 10), // base 10
		strconv.FormatInt(nbr, 2),  // base 2
	}
})

fmt.Printf("%v", result)
Output:

[1 1 2 10 3 11 4 100]

func Flatten

func Flatten[T any, Slice ~[]T](collection []Slice) Slice

Flatten returns a slice a single level deep. Play: https://go.dev/play/p/rbp9ORaMpjw

Example
list := [][]int{{0, 1, 2}, {3, 4, 5}}

result := Flatten(list)

fmt.Printf("%v", result)
Output:

[0 1 2 3 4 5]

func ForEach

func ForEach[T any](collection []T, iteratee func(item T, index int))

ForEach iterates over elements of collection and invokes iteratee for each element. Play: https://go.dev/play/p/oofyiUPRf8t

Example
list := []int64{1, 2, 3, 4}

ForEach(list, func(x int64, _ int) {
	fmt.Println(x)
})
Output:

1
2
3
4

func ForEachWhile added in v1.46.0

func ForEachWhile[T any](collection []T, iteratee func(item T, index int) bool)

ForEachWhile iterates over elements of collection and invokes iteratee for each element collection return value decide to continue or break, like do while(). Play: https://go.dev/play/p/QnLGt35tnow

Example
list := []int64{1, 2, -math.MaxInt, 4}

ForEachWhile(list, func(x int64, _ int) bool {
	if x < 0 {
		return false
	}
	fmt.Println(x)
	return true
})
Output:

1
2

func FromAnySlice added in v1.21.0

func FromAnySlice[T any](in []any) ([]T, bool)

FromAnySlice returns a slice with all elements mapped to a type. Returns false in case of type conversion failure. Play: https://go.dev/play/p/P2sD0PMXw4F

func FromEntries

func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V

FromEntries transforms a slice of key/value pairs into a map. Play: https://go.dev/play/p/oIr5KHFGCEN

Example
result := FromEntries([]Entry[string, int]{
	{
		Key:   "foo",
		Value: 1,
	},
	{
		Key:   "bar",
		Value: 2,
	},
	{
		Key:   "baz",
		Value: 3,
	},
})

fmt.Printf("%v %v %v %v", len(result), result["foo"], result["bar"], result["baz"])
Output:

3 1 2 3

func FromPairs added in v1.22.0

func FromPairs[K comparable, V any](entries []Entry[K, V]) map[K]V

FromPairs transforms a slice of key/value pairs into a map. Alias of FromEntries(). Play: https://go.dev/play/p/oIr5KHFGCEN

func FromPtr added in v1.22.0

func FromPtr[T any](x *T) T

FromPtr returns the pointer value or empty. Play: https://go.dev/play/p/mhD9CwO3X0m

func FromPtrOr added in v1.26.0

func FromPtrOr[T any](x *T, fallback T) T

FromPtrOr returns the pointer value or the fallback value. Play: https://go.dev/play/p/mhD9CwO3X0m

func FromSlicePtr added in v1.47.0

func FromSlicePtr[T any](collection []*T) []T

FromSlicePtr returns a slice with the pointer values. Returns a zero value in case of a nil pointer element. Play: https://go.dev/play/p/lbunFvzlUDX

func FromSlicePtrOr added in v1.47.0

func FromSlicePtrOr[T any](collection []*T, fallback T) []T

FromSlicePtrOr returns a slice with the pointer values or the fallback value. Play: https://go.dev/play/p/lbunFvzlUDX

func Generator deprecated added in v1.31.0

func Generator[T any](bufferSize int, generator func(yield func(T))) <-chan T

Generator implements the generator design pattern. Play: https://go.dev/play/p/lIbSY3QmiEg

Deprecated: use "iter" package instead (Go >= 1.23).

func GroupBy

func GroupBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) map[U]Slice

GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee. Play: https://go.dev/play/p/XnQBd_v6brd

Example
list := []int{0, 1, 2, 3, 4, 5}

result := GroupBy(list, func(i int) int {
	return i % 3
})

fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
Output:

[0 3]
[1 4]
[2 5]

func GroupByMap added in v1.50.0

func GroupByMap[T any, K comparable, V any](collection []T, iteratee func(item T) (K, V)) map[K][]V

GroupByMap returns an object composed of keys generated from the results of running each element of collection through iteratee. Play: https://go.dev/play/p/iMeruQ3_W80

Example
list := []int{0, 1, 2, 3, 4, 5}

result := GroupByMap(list, func(i int) (int, int) {
	return i % 3, i * 2
})

fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
Output:

[0 6]
[2 8]
[4 10]

func HasKey added in v1.43.0

func HasKey[K comparable, V any](in map[K]V, key K) bool

HasKey returns whether the given key exists. Play: https://go.dev/play/p/aVwubIvECqS

func HasPrefix added in v1.52.0

func HasPrefix[T comparable](collection, prefix []T) bool

HasPrefix returns true if the collection has the prefix. Play: https://go.dev/play/p/SrljzVDpMQM

func HasSuffix added in v1.52.0

func HasSuffix[T comparable](collection, suffix []T) bool

HasSuffix returns true if the collection has the suffix. Play: https://go.dev/play/p/bJeLetQNAON

func If

func If[T any](condition bool, result T) *ifElse[T]

If is a single line if/else statement. Play: https://go.dev/play/p/WSw3ApMxhyW

Example
result1 := If(true, 1).
	ElseIf(false, 2).
	Else(3)

result2 := If(false, 1).
	ElseIf(true, 2).
	Else(3)

result3 := If(false, 1).
	ElseIf(false, 2).
	Else(3)

result4 := IfF(true, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

result5 := IfF(false, func() int { return 1 }).
	ElseIfF(true, func() int { return 2 }).
	ElseF(func() int { return 3 })

result6 := IfF(false, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3

func IfF added in v1.13.0

func IfF[T any](condition bool, resultF func() T) *ifElse[T]

IfF is a single line if/else statement whose options are functions. Play: https://go.dev/play/p/WSw3ApMxhyW

Example
result1 := If(true, 1).
	ElseIf(false, 2).
	Else(3)

result2 := If(false, 1).
	ElseIf(true, 2).
	Else(3)

result3 := If(false, 1).
	ElseIf(false, 2).
	Else(3)

result4 := IfF(true, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

result5 := IfF(false, func() int { return 1 }).
	ElseIfF(true, func() int { return 2 }).
	ElseF(func() int { return 3 })

result6 := IfF(false, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3

func IndexOf

func IndexOf[T comparable](collection []T, element T) int

IndexOf returns the index at which the first occurrence of a value is found in a slice or -1 if the value cannot be found. Play: https://go.dev/play/p/Eo7W0lvKTky

Example
list := []string{"foo", "bar", "baz"}

result := IndexOf(list, "bar")

fmt.Printf("%d", result)
Output:

1
Example (NotFound)
list := []string{"foo", "bar", "baz"}

result := IndexOf(list, "qux")

fmt.Printf("%d", result)
Output:

-1

func Interleave added in v1.32.0

func Interleave[T any, Slice ~[]T](collections ...Slice) Slice

Interleave round-robin alternating input slices and sequentially appending value at index into result. Play: https://go.dev/play/p/-RJkTLQEDVt

Example
list1 := [][]int{{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}
list2 := [][]int{{1}, {2, 5, 8}, {3, 6}, {4, 7, 9, 10}}

result1 := Interleave(list1...)
result2 := Interleave(list2...)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
Output:

[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9 10]

func Intersect

func Intersect[T comparable, Slice ~[]T](list1, list2 Slice) Slice

Intersect returns the intersection between two collections. Play: https://go.dev/play/p/uuElL9X9e58

func Invert added in v1.13.0

func Invert[K, V comparable](in map[K]V) map[V]K

Invert creates a map composed of the inverted keys and values. If map contains duplicate values, subsequent values overwrite property assignments of previous values. Play: https://go.dev/play/p/rFQ4rak6iA1

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := Invert(kv)

fmt.Printf("%v %v %v %v", len(result), result[1], result[2], result[3])
Output:

3 foo bar baz

func IsEmpty added in v1.22.0

func IsEmpty[T comparable](v T) bool

IsEmpty returns true if argument is a zero value. Play: https://go.dev/play/p/P2sD0PMXw4F

func IsNil added in v1.39.0

func IsNil(x any) bool

IsNil checks if a value is nil or if it's a reference type with a nil underlying value. Play: https://go.dev/play/p/P2sD0PMXw4F

func IsNotEmpty added in v1.25.0

func IsNotEmpty[T comparable](v T) bool

IsNotEmpty returns true if argument is not a zero value. Play: https://go.dev/play/p/P2sD0PMXw4F

func IsNotNil added in v1.49.0

func IsNotNil(x any) bool

IsNotNil checks if a value is not nil or if it's not a reference type with a nil underlying value. Play: https://go.dev/play/p/P2sD0PMXw4F

func IsSorted added in v1.26.0

func IsSorted[T constraints.Ordered](collection []T) bool

IsSorted checks if a slice is sorted. Play: https://go.dev/play/p/mc3qR-t4mcx

Example
list := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

result := IsSorted(list)

fmt.Printf("%v", result)
Output:

true

func IsSortedByKey added in v1.26.0

func IsSortedByKey[T any, K constraints.Ordered](collection []T, iteratee func(item T) K) bool

IsSortedByKey checks if a slice is sorted by iteratee.

Example
list := []string{"a", "bb", "ccc"}

result := IsSortedByKey(list, func(s string) int {
	return len(s)
})

fmt.Printf("%v", result)
Output:

true

func KebabCase added in v1.40.0

func KebabCase(str string) string

KebabCase converts string to kebab case. Play: https://go.dev/play/p/96gT_WZnTVP

func KeyBy added in v1.10.1

func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V

KeyBy transforms a slice or a slice of structs to a map based on a pivot callback. Play: https://go.dev/play/p/ccUiUL_Lnel

Example
list := []string{"a", "aa", "aaa"}

result := KeyBy(list, func(str string) int {
	return len(str)
})

fmt.Printf("%v", result)
Output:

map[1:a 2:aa 3:aaa]

func Keyify added in v1.48.0

func Keyify[T comparable, Slice ~[]T](collection Slice) map[T]struct{}

Keyify returns a map with each unique element of the slice as a key. Play: https://go.dev/play/p/RYhhM_csqIG

Example
list := []string{"a", "a", "b", "b", "d"}

set := Keyify(list)
_, ok1 := set["a"]
_, ok2 := set["c"]
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", set)
Output:

true
false
map[a:{} b:{} d:{}]

func Keys

func Keys[K comparable, V any](in ...map[K]V) []K

Keys creates a slice of the map keys. Play: https://go.dev/play/p/Uu11fHASqrU

Example
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 3}

result := Keys(kv, kv2)
sort.Strings(result)
fmt.Printf("%v", result)
Output:

[bar baz foo]

func Last

func Last[T any](collection []T) (T, bool)

Last returns the last element of a collection or error if empty. Play: https://go.dev/play/p/ul45Z0y2EFO

Example
list := []int{1, 2, 3, 4, 5}

result, found := Last(list)

fmt.Printf("%d %t", result, found)
Output:

5 true
Example (Empty)
list := []int{}

result, found := Last(list)

fmt.Printf("%d %t", result, found)
Output:

0 false

func LastIndexOf

func LastIndexOf[T comparable](collection []T, element T) int

LastIndexOf returns the index at which the last occurrence of a value is found in a slice or -1 if the value cannot be found. Play: https://go.dev/play/p/Eo7W0lvKTky

Example
list := []string{"foo", "bar", "baz", "bar"}

result := LastIndexOf(list, "bar")

fmt.Printf("%d", result)
Output:

3
Example (NotFound)
list := []string{"foo", "bar", "baz"}

result := LastIndexOf(list, "qux")

fmt.Printf("%d", result)
Output:

-1

func LastOr added in v1.42.0

func LastOr[T any](collection []T, fallback T) T

LastOr returns the last element of a collection or the fallback value if empty. Play: https://go.dev/play/p/ul45Z0y2EFO

Example
list := []int{1, 2, 3, 4, 5}

result := LastOr(list, -1)

fmt.Printf("%d", result)
Output:

5
Example (Empty)
list := []int{}

result := LastOr(list, -1)

fmt.Printf("%d", result)
Output:

-1

func LastOrEmpty added in v1.42.0

func LastOrEmpty[T any](collection []T) T

LastOrEmpty returns the last element of a collection or zero value if empty. Play: https://go.dev/play/p/ul45Z0y2EFO

Example
list := []int{1, 2, 3, 4, 5}

result := LastOrEmpty(list)

fmt.Printf("%d", result)
Output:

5
Example (Empty)
list := []int{}

result := LastOrEmpty(list)

fmt.Printf("%d", result)
Output:

0

func Latest added in v1.41.0

func Latest(times ...time.Time) time.Time

Latest search the maximum time.Time of a collection. Returns zero value when the collection is empty.

Example
now := time.Now()
past := now.Add(-time.Hour)
future := now.Add(time.Hour)

result := Latest(future, now, past)

fmt.Printf("%t", result.Equal(future))
Output:

true

func LatestBy added in v1.45.0

func LatestBy[T any](collection []T, iteratee func(item T) time.Time) T

LatestBy search the maximum time.Time of a collection using the given iteratee function. Returns zero value when the collection is empty.

Example
type Event struct {
	Name string
	Time time.Time
}

now := time.Now()
events := []Event{
	{Name: "Event A", Time: now.Add(time.Hour)},
	{Name: "Event B", Time: now},
	{Name: "Event C", Time: now.Add(-time.Hour)},
}

result := LatestBy(events, func(event Event) time.Time {
	return event.Time
})

fmt.Printf("%s", result.Name)
Output:

Event A

func Map

func Map[T, R any](collection []T, iteratee func(item T, index int) R) []R

Map manipulates a slice and transforms it to a slice of another type. Play: https://go.dev/play/p/OkPcYAhBo0D

Example
list := []int64{1, 2, 3, 4}

result := Map(list, func(nbr int64, index int) string {
	return strconv.FormatInt(nbr*2, 10)
})

fmt.Printf("%v", result)
Output:

[2 4 6 8]

func MapEntries added in v1.32.0

func MapEntries[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2

MapEntries manipulates map entries and transforms it to a map of another type. Play: https://go.dev/play/p/VuvNQzxKimT

Example
kv := map[string]int{"foo": 1, "bar": 2}

result := MapEntries(kv, func(k string, v int) (int, string) {
	return v, k
})

fmt.Printf("%v", result)
Output:

map[1:foo 2:bar]

func MapKeys added in v1.13.0

func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) R) map[R]V

MapKeys manipulates map keys and transforms it to a map of another type. Play: https://go.dev/play/p/9_4WPIqOetJ

Example
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}

result := MapKeys(kv, func(_, k int) string {
	return strconv.FormatInt(int64(k), 10)
})

fmt.Printf("%v %v %v %v %v", len(result), result["1"], result["2"], result["3"], result["4"])
Output:

4 1 2 3 4

func MapToSlice added in v1.26.0

func MapToSlice[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) R) []R

MapToSlice transforms a map into a slice based on specified iteratee. Play: https://go.dev/play/p/ZuiCZpDt6LD

Example
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}

result := MapToSlice(kv, func(k int, v int64) string {
	return fmt.Sprintf("%d_%d", k, v)
})

sort.Strings(result)
fmt.Printf("%v", result)
Output:

[1_1 2_2 3_3 4_4]

func MapValues added in v1.7.0

func MapValues[K comparable, V, R any](in map[K]V, iteratee func(value V, key K) R) map[K]R

MapValues manipulates map values and transforms it to a map of another type. Play: https://go.dev/play/p/T_8xAfvcf0W

Example
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}

result := MapValues(kv, func(v, _ int) string {
	return strconv.FormatInt(int64(v), 10)
})

fmt.Printf("%v %q %q %q %q", len(result), result[1], result[2], result[3], result[4])
Output:

4 "1" "2" "3" "4"

func Max

func Max[T constraints.Ordered](collection []T) T

Max searches the maximum value of a collection. Returns zero value when the collection is empty. Play: https://go.dev/play/p/r6e-Z8JozS8

Example
list := []int{3, 1, 4, 1, 5, 9, 2, 6}

result := Max(list)

fmt.Printf("%d", result)
Output:

9
Example (Empty)
list := []int{}

result := Max(list)

fmt.Printf("%d", result)
Output:

0

func MaxBy added in v1.12.0

func MaxBy[T any](collection []T, comparison func(a, b T) bool) T

MaxBy search the maximum value of a collection using the given comparison function. If several values of the collection are equal to the greatest value, returns the first such value. Returns zero value when the collection is empty.

Example
type User struct {
	Name string
	Age  int
}

users := []User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
}

result := MaxBy(users, func(a, b User) bool {
	return a.Age > b.Age
})

fmt.Printf("%s", result.Name)
Output:

Charlie

func MaxIndex added in v1.48.0

func MaxIndex[T constraints.Ordered](collection []T) (T, int)

MaxIndex searches the maximum value of a collection and the index of the maximum value. Returns (zero value, -1) when the collection is empty.

Example
list := []int{3, 1, 4, 1, 5, 9, 2, 6}

result, index := MaxIndex(list)

fmt.Printf("%d %d", result, index)
Output:

9 5
Example (Empty)
list := []int{}

result, index := MaxIndex(list)

fmt.Printf("%d %d", result, index)
Output:

0 -1

func MaxIndexBy added in v1.48.0

func MaxIndexBy[T any](collection []T, comparison func(a, b T) bool) (T, int)

MaxIndexBy search the maximum value of a collection using the given comparison function and the index of the maximum value. If several values of the collection are equal to the greatest value, returns the first such value. Returns (zero value, -1) when the collection is empty.

Example
type User struct {
	Name string
	Age  int
}

users := []User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
}

result, index := MaxIndexBy(users, func(a, b User) bool {
	return a.Age > b.Age
})

fmt.Printf("%s %d", result.Name, index)
Output:

Charlie 2

func Mean added in v1.40.0

func Mean[T constraints.Float | constraints.Integer](collection []T) T

Mean calculates the mean of a collection of numbers. Play: https://go.dev/play/p/tPURSuteUsP

Example
list := []int{1, 2, 3, 4, 5}

result := Mean(list)

fmt.Printf("%v", result)
Output:

3

func MeanBy added in v1.40.0

func MeanBy[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(item T) R) R

MeanBy calculates the mean of a collection of numbers using the given return value from the iteration function. Play: https://go.dev/play/p/j7TsVwBOZ7P

Example
list := []string{"foo", "bar"}

result := MeanBy(list, func(item string) int {
	return len(item)
})

fmt.Printf("%v", result)
Output:

3

func Min

func Min[T constraints.Ordered](collection []T) T

Min search the minimum value of a collection. Returns zero value when the collection is empty. Play: https://go.dev/play/p/r6e-Z8JozS8

Example
list := []int{3, 1, 4, 1, 5, 9, 2, 6}

result := Min(list)

fmt.Printf("%d", result)
Output:

1
Example (Empty)
list := []int{}

result := Min(list)

fmt.Printf("%d", result)
Output:

0

func MinBy added in v1.12.0

func MinBy[T any](collection []T, comparison func(a, b T) bool) T

MinBy search the minimum value of a collection using the given comparison function. If several values of the collection are equal to the smallest value, returns the first such value. Returns zero value when the collection is empty.

Example
type User struct {
	Name string
	Age  int
}

users := []User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
}

result := MinBy(users, func(a, b User) bool {
	return a.Age < b.Age
})

fmt.Printf("%s", result.Name)
Output:

Alice

func MinIndex added in v1.48.0

func MinIndex[T constraints.Ordered](collection []T) (T, int)

MinIndex search the minimum value of a collection and the index of the minimum value. Returns (zero value, -1) when the collection is empty.

Example
list := []int{3, 1, 4, 1, 5, 9, 2, 6}

result, index := MinIndex(list)

fmt.Printf("%d %d", result, index)
Output:

1 1
Example (Empty)
list := []int{}

result, index := MinIndex(list)

fmt.Printf("%d %d", result, index)
Output:

0 -1

func MinIndexBy added in v1.48.0

func MinIndexBy[T any](collection []T, comparison func(a, b T) bool) (T, int)

MinIndexBy search the minimum value of a collection using the given comparison function and the index of the minimum value. If several values of the collection are equal to the smallest value, returns the first such value. Returns (zero value, -1) when the collection is empty.

Example
type User struct {
	Name string
	Age  int
}

users := []User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
}

result, index := MinIndexBy(users, func(a, b User) bool {
	return a.Age < b.Age
})

fmt.Printf("%s %d", result.Name, index)
Output:

Alice 0

func Mode added in v1.52.0

func Mode[T constraints.Integer | constraints.Float](collection []T) []T

Mode returns the mode (most frequent value) of a collection. If multiple values have the same highest frequency, then multiple values are returned. If the collection is empty, then the zero value of T is returned.

func Must added in v1.12.0

func Must[T any](val T, err any, messageArgs ...any) T

Must is a helper that wraps a call to a function returning a value and an error and panics if err is error or false. Play: https://go.dev/play/p/fOqtX5HudtN

Example
defer func() {
	_ = recover()
}()

// won't panic
Must(42, nil)

// won't panic
cb := func() (int, error) {
	return 42, nil
}
Must(cb())

// will panic
Must(42, errors.New("my error"))

// will panic with error message
Must(42, errors.New("world"), "hello")

func Must0 added in v1.12.0

func Must0(err any, messageArgs ...any)

Must0 has the same behavior as Must, but callback returns no variable. Play: https://go.dev/play/p/TMoWrRp3DyC

Example
defer func() {
	_ = recover()
}()

// won't panic
Must0(nil)

// will panic
Must0(errors.New("my error"))

// will panic with error message
Must0(errors.New("world"), "hello")

func Must1 added in v1.12.0

func Must1[T any](val T, err any, messageArgs ...any) T

Must1 is an alias to Must. Play: https://go.dev/play/p/TMoWrRp3DyC

Example
defer func() {
	_ = recover()
}()

// won't panic
Must1(42, nil)

// won't panic
cb := func() (int, error) {
	return 42, nil
}
Must1(cb())

// will panic
Must1(42, errors.New("my error"))

// will panic with error message
Must1(42, errors.New("world"), "hello")

func Must2 added in v1.12.0

func Must2[T1, T2 any](val1 T1, val2 T2, err any, messageArgs ...any) (T1, T2)

Must2 has the same behavior as Must, but callback returns 2 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example
defer func() {
	_ = recover()
}()

// won't panic
Must2(42, "hello", nil)

// will panic
Must2(42, "hello", errors.New("my error"))

// will panic with error message
Must2(42, "hello", errors.New("world"), "hello")

func Must3 added in v1.12.0

func Must3[T1, T2, T3 any](val1 T1, val2 T2, val3 T3, err any, messageArgs ...any) (T1, T2, T3)

Must3 has the same behavior as Must, but callback returns 3 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example
defer func() {
	_ = recover()
}()

// won't panic
Must3(42, "hello", 4.2, nil)

// will panic
Must3(42, "hello", 4.2, errors.New("my error"))

// will panic with error message
Must3(42, "hello", 4.2, errors.New("world"), "hello")

func Must4 added in v1.12.0

func Must4[T1, T2, T3, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...any) (T1, T2, T3, T4)

Must4 has the same behavior as Must, but callback returns 4 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example
defer func() {
	_ = recover()
}()

// won't panic
Must4(42, "hello", 4.2, true, nil)

// will panic
Must4(42, "hello", 4.2, true, errors.New("my error"))

// will panic with error message
Must4(42, "hello", 4.2, true, errors.New("world"), "hello")

func Must5 added in v1.12.0

func Must5[T1, T2, T3, T4, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, messageArgs ...any) (T1, T2, T3, T4, T5)

Must5 has the same behavior as Must, but callback returns 5 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example
defer func() {
	_ = recover()
}()

// won't panic
Must5(42, "hello", 4.2, true, foo{}, nil)

// will panic
Must5(42, "hello", 4.2, true, foo{}, errors.New("my error"))

// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, errors.New("world"), "hello")

func Must6 added in v1.12.0

func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any, messageArgs ...any) (T1, T2, T3, T4, T5, T6)

Must6 has the same behavior as Must, but callback returns 6 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example
defer func() {
	_ = recover()
}()

// won't panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", nil)

// will panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", errors.New("my error"))

// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, "foobar", errors.New("world"), "hello")

func NewDebounce added in v1.11.0

func NewDebounce(duration time.Duration, f ...func()) (func(), func())

NewDebounce creates a debounced instance that delays invoking functions given until after wait milliseconds have elapsed. Play: https://go.dev/play/p/mz32VMK2nqe

Example
i := int32(0)
calls := []int32{}
mu := sync.Mutex{}

debounce, cancel := NewDebounce(time.Millisecond, func() {
	mu.Lock()
	defer mu.Unlock()
	calls = append(calls, atomic.LoadInt32(&i))
})

debounce()
atomic.AddInt32(&i, 1)

time.Sleep(5 * time.Millisecond)

debounce()
atomic.AddInt32(&i, 1)
debounce()
atomic.AddInt32(&i, 1)
debounce()
atomic.AddInt32(&i, 1)

time.Sleep(5 * time.Millisecond)

cancel()

mu.Lock()
fmt.Printf("%v", calls)
mu.Unlock()
Output:

[1 4]

func NewDebounceBy added in v1.38.0

func NewDebounceBy[T comparable](duration time.Duration, f ...func(key T, count int)) (func(key T), func(key T))

NewDebounceBy creates a debounced instance for each distinct key, that delays invoking functions given until after wait milliseconds have elapsed. Play: https://go.dev/play/p/d3Vpt6pxhY8

Example
calls := map[string][]int{}
mu := sync.Mutex{}

debounce, cancel := NewDebounceBy(time.Millisecond, func(userID string, count int) {
	mu.Lock()
	defer mu.Unlock()

	if _, ok := calls[userID]; !ok {
		calls[userID] = []int{}
	}

	calls[userID] = append(calls[userID], count)
})

debounce("samuel")
debounce("john")

time.Sleep(5 * time.Millisecond)

debounce("john")
debounce("john")
debounce("samuel")
debounce("john")

time.Sleep(5 * time.Millisecond)

cancel("samuel")
cancel("john")

mu.Lock()
fmt.Printf("samuel: %v\n", calls["samuel"])
fmt.Printf("john: %v\n", calls["john"])
mu.Unlock()
Output:

samuel: [1 1]
john: [1 3]

func NewThrottle added in v1.49.0

func NewThrottle(interval time.Duration, f ...func()) (throttle, reset func())

NewThrottle creates a throttled instance that invokes given functions only once in every interval. This returns 2 functions, First one is throttled function and Second one is a function to reset interval. Play: https://go.dev/play/p/qQn3fm8Z7jS

Example
throttle, reset := NewThrottle(100*time.Millisecond, func() {
	fmt.Println("Called once in every 100ms")
})

for j := 0; j < 10; j++ {
	throttle()
	time.Sleep(30 * time.Millisecond)
}

reset()
Output:

Called once in every 100ms
Called once in every 100ms
Called once in every 100ms

func NewThrottleBy added in v1.49.0

func NewThrottleBy[T comparable](interval time.Duration, f ...func(key T)) (throttle func(key T), reset func())

NewThrottleBy creates a throttled instance that invokes given functions only once in every interval. This returns 2 functions, First one is throttled function and Second one is a function to reset interval. Play: https://go.dev/play/p/0Wv6oX7dHdC

Example
throttle, reset := NewThrottleBy(100*time.Millisecond, func(key string) {
	fmt.Println(key, "Called once in every 100ms")
})

for j := 0; j < 10; j++ {
	throttle("foo")
	throttle("bar")
	time.Sleep(30 * time.Millisecond)
}

reset()
Output:

foo Called once in every 100ms
bar Called once in every 100ms
foo Called once in every 100ms
bar Called once in every 100ms
foo Called once in every 100ms
bar Called once in every 100ms

func NewThrottleByWithCount added in v1.49.0

func NewThrottleByWithCount[T comparable](interval time.Duration, count int, f ...func(key T)) (throttle func(key T), reset func())

NewThrottleByWithCount is NewThrottleBy with count limit, throttled function will be invoked count times in every interval. Play: https://go.dev/play/p/vQk3ECH7_EW

Example
throttle, reset := NewThrottleByWithCount(100*time.Millisecond, 2, func(key string) {
	fmt.Println(key, "Called once in every 100ms")
})

for j := 0; j < 10; j++ {
	throttle("foo")
	throttle("bar")
	time.Sleep(30 * time.Millisecond)
}

reset()
Output:

foo Called once in every 100ms
bar Called once in every 100ms
foo Called once in every 100ms
bar Called once in every 100ms
foo Called once in every 100ms
bar Called once in every 100ms
foo Called once in every 100ms
bar Called once in every 100ms
foo Called once in every 100ms
bar Called once in every 100ms
foo Called once in every 100ms
bar Called once in every 100ms

func NewThrottleWithCount added in v1.49.0

func NewThrottleWithCount(interval time.Duration, count int, f ...func()) (throttle, reset func())

NewThrottleWithCount is NewThrottle with count limit, throttled function will be invoked count times in every interval. Play: https://go.dev/play/p/w5nc0MgWtjC

Example
throttle, reset := NewThrottleWithCount(100*time.Millisecond, 2, func() {
	fmt.Println("Called once in every 100ms")
})

for j := 0; j < 10; j++ {
	throttle()
	time.Sleep(30 * time.Millisecond)
}

reset()
Output:

Called once in every 100ms
Called once in every 100ms
Called once in every 100ms
Called once in every 100ms
Called once in every 100ms
Called once in every 100ms

func Nil added in v1.42.0

func Nil[T any]() *T

Nil returns a nil pointer of type. Play: https://go.dev/play/p/P2sD0PMXw4F

func None added in v1.18.0

func None[T comparable](collection, subset []T) bool

None returns true if no element of a subset is contained in a collection or if the subset is empty. Play: https://go.dev/play/p/fye7JsmxzPV

func NoneBy added in v1.18.0

func NoneBy[T any](collection []T, predicate func(item T) bool) bool

NoneBy returns true if the predicate returns true for none of the elements in the collection or if the collection is empty. Play: https://go.dev/play/p/O64WZ32H58S

func Nth

func Nth[T any, N constraints.Integer](collection []T, nth N) (T, error)

Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds. Play: https://go.dev/play/p/sHoh88KWt6B

Example
list := []int{1, 2, 3, 4, 5}

result, err := Nth(list, 2)

fmt.Printf("%d %v", result, err)
Output:

3 <nil>
Example (Negative)
list := []int{1, 2, 3, 4, 5}

result, err := Nth(list, -2)

fmt.Printf("%d %v", result, err)
Output:

4 <nil>
Example (OutOfBounds)
list := []int{1, 2, 3, 4, 5}

result, err := Nth(list, 10)

fmt.Printf("%d %v", result, err)
Output:

0 nth: 10 out of slice bounds

func NthOr added in v1.50.0

func NthOr[T any, N constraints.Integer](collection []T, nth N, fallback T) T

NthOr returns the element at index `nth` of collection. If `nth` is negative, it returns the nth element from the end. If `nth` is out of slice bounds, it returns the fallback value instead of an error. Play: https://go.dev/play/p/sHoh88KWt6B

Example
list := []int{1, 2, 3, 4, 5}

result := NthOr(list, 2, -1)

fmt.Printf("%d", result)
Output:

3
Example (OutOfBounds)
list := []int{1, 2, 3, 4, 5}

result := NthOr(list, 10, -1)

fmt.Printf("%d", result)
Output:

-1

func NthOrEmpty added in v1.50.0

func NthOrEmpty[T any, N constraints.Integer](collection []T, nth N) T

NthOrEmpty returns the element at index `nth` of collection. If `nth` is negative, it returns the nth element from the end. If `nth` is out of slice bounds, it returns the zero value (empty value) for that type. Play: https://go.dev/play/p/sHoh88KWt6B

Example
list := []int{1, 2, 3, 4, 5}

result := NthOrEmpty(list, 2)

fmt.Printf("%d", result)
Output:

3
Example (OutOfBounds)
list := []int{1, 2, 3, 4, 5}

result := NthOrEmpty(list, 10)

fmt.Printf("%d", result)
Output:

0

func OmitBy added in v1.13.0

func OmitBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map

OmitBy returns same map type filtered by given predicate. Play: https://go.dev/play/p/EtBsR43bdsd

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := OmitBy(kv, func(key string, value int) bool {
	return value%2 == 1
})

fmt.Printf("%v", result)
Output:

map[bar:2]

func OmitByKeys added in v1.13.0

func OmitByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map

OmitByKeys returns same map type filtered by given keys. Play: https://go.dev/play/p/t1QjCrs-ysk

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := OmitByKeys(kv, []string{"foo", "baz"})

fmt.Printf("%v", result)
Output:

map[bar:2]

func OmitByValues added in v1.13.0

func OmitByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map

OmitByValues returns same map type filtered by given values. Play: https://go.dev/play/p/9UYZi-hrs8j

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := OmitByValues(kv, []int{1, 3})

fmt.Printf("%v", result)
Output:

map[bar:2]

func Partial added in v1.22.0

func Partial[T1, T2, R any](f func(a T1, b T2) R, arg1 T1) func(T2) R

Partial returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/Sy1gAQiQZ3v

func Partial1 added in v1.37.0

func Partial1[T1, T2, R any](f func(T1, T2) R, arg1 T1) func(T2) R

Partial1 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/D-ASTXCLBzw

func Partial2 added in v1.37.0

func Partial2[T1, T2, T3, R any](f func(T1, T2, T3) R, arg1 T1) func(T2, T3) R

Partial2 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/-xiPjy4JChJ

func Partial3 added in v1.37.0

func Partial3[T1, T2, T3, T4, R any](f func(T1, T2, T3, T4) R, arg1 T1) func(T2, T3, T4) R

Partial3 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/zWtSutpI26m

func Partial4 added in v1.37.0

func Partial4[T1, T2, T3, T4, T5, R any](f func(T1, T2, T3, T4, T5) R, arg1 T1) func(T2, T3, T4, T5) R

Partial4 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/kBrnnMTcJm0

func Partial5 added in v1.37.0

func Partial5[T1, T2, T3, T4, T5, T6, R any](f func(T1, T2, T3, T4, T5, T6) R, arg1 T1) func(T2, T3, T4, T5, T6) R

Partial5 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/7Is7K2y_VC3

func PartitionBy added in v1.4.0

func PartitionBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K) []Slice

PartitionBy returns a slice of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee. Play: https://go.dev/play/p/NfQ_nGjkgXW

Example
list := []int{-2, -1, 0, 1, 2, 3, 4}

result := PartitionBy(list, func(x int) string {
	if x < 0 {
		return "negative"
	} else if x%2 == 0 {
		return "even"
	}
	return "odd"
})

for _, item := range result {
	fmt.Printf("%v\n", item)
}
Output:

[-2 -1]
[0 2 4]
[1 3]

func PascalCase added in v1.40.0

func PascalCase(str string) string

PascalCase converts string to pascal case. Play: https://go.dev/play/p/Dy_V_6DUYhe

func PickBy added in v1.13.0

func PickBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map

PickBy returns same map type filtered by given predicate. Play: https://go.dev/play/p/kdg8GR_QMmf

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := PickBy(kv, func(key string, value int) bool {
	return value%2 == 1
})

fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output:

2 1 3

func PickByKeys added in v1.13.0

func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map

PickByKeys returns same map type filtered by given keys. Play: https://go.dev/play/p/R1imbuci9qU

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := PickByKeys(kv, []string{"foo", "baz"})

fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output:

2 1 3

func PickByValues added in v1.13.0

func PickByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map

PickByValues returns same map type filtered by given values. Play: https://go.dev/play/p/1zdzSvbfsJc

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := PickByValues(kv, []int{1, 3})

fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output:

2 1 3

func Product added in v1.49.0

func Product[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T

Product gets the product of the values in a collection. If collection is empty 1 is returned. Play: https://go.dev/play/p/2_kjM_smtAH

Example
list := []int{1, 2, 3, 4, 5}

result := Product(list)

fmt.Printf("%v", result)
Output:

120

func ProductBy added in v1.49.0

func ProductBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R

ProductBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 1 is returned. Play: https://go.dev/play/p/wadzrWr9Aer

Example
list := []string{"foo", "bar"}

result := ProductBy(list, func(item string) int {
	return len(item)
})

fmt.Printf("%v", result)
Output:

9

func RandomString added in v1.35.0

func RandomString(size int, charset []rune) string

RandomString return a random string. Play: https://go.dev/play/p/rRseOQVVum4

func Range added in v1.10.0

func Range(elementNum int) []int

Range creates a slice of numbers (positive and/or negative) with given length. Play: https://go.dev/play/p/0r6VimXAi9H

Example
result1 := Range(4)
result2 := Range(-4)
result3 := RangeFrom(1, 5)
result4 := RangeFrom(1.0, 5)
result5 := RangeWithSteps(0, 20, 5)
result6 := RangeWithSteps[float32](-1.0, -4.0, -1.0)
result7 := RangeWithSteps(1, 4, -1)
result8 := Range(0)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
fmt.Printf("%v\n", result7)
fmt.Printf("%v\n", result8)
Output:

[0 1 2 3]
[0 -1 -2 -3]
[1 2 3 4 5]
[1 2 3 4 5]
[0 5 10 15]
[-1 -2 -3]
[]
[]

func RangeFrom added in v1.10.0

func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) []T

RangeFrom creates a slice of numbers from start with specified length. Play: https://go.dev/play/p/0r6VimXAi9H

func RangeWithSteps added in v1.10.0

func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) []T

RangeWithSteps creates a slice of numbers (positive and/or negative) progressing from start up to, but not including end. step set to zero will return an empty slice. Play: https://go.dev/play/p/0r6VimXAi9H

func Reduce

func Reduce[T, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R

Reduce reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous. Play: https://go.dev/play/p/CgHYNUpOd1I

Example
list := []int64{1, 2, 3, 4}

result := Reduce(list, func(agg, item int64, index int) int64 {
	return agg + item
}, 0)

fmt.Printf("%v", result)
Output:

10

func ReduceRight added in v1.26.0

func ReduceRight[T, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R

ReduceRight is like Reduce except that it iterates over elements of collection from right to left. Play: https://go.dev/play/p/Fq3W70l7wXF

Example
list := [][]int{{0, 1}, {2, 3}, {4, 5}}

result := ReduceRight(list, func(agg, item []int, index int) []int {
	return append(agg, item...)
}, []int{})

fmt.Printf("%v", result)
Output:

[4 5 2 3 0 1]

func Reject added in v1.11.0

func Reject[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice

Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return true for. Play: https://go.dev/play/p/pFCF5WVB225

Example
list := []int{0, 1, 2, 3, 4, 5}

result := Reject(list, func(x, _ int) bool {
	return x%2 == 0
})

fmt.Printf("%v", result)
Output:

[1 3 5]

func RejectMap added in v1.41.0

func RejectMap[T, R any](collection []T, callback func(item T, index int) (R, bool)) []R

RejectMap is the opposite of FilterMap, this method returns a slice obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/W9Ug9r0QFkL

func Repeat added in v1.5.0

func Repeat[T Clonable[T]](count int, initial T) []T

Repeat builds a slice with N copies of initial value. Play: https://go.dev/play/p/g3uHXbmc3b6

Example
result := Repeat(2, foo{"a"})

fmt.Printf("%v", result)
Output:

[{a} {a}]

func RepeatBy added in v1.19.0

func RepeatBy[T any](count int, predicate func(index int) T) []T

RepeatBy builds a slice with values returned by N calls of callback. Play: https://go.dev/play/p/ozZLCtX_hNU

Example
result := RepeatBy(5, func(i int) string {
	return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})

fmt.Printf("%v", result)
Output:

[0 1 4 9 16]

func Replace added in v1.19.0

func Replace[T comparable, Slice ~[]T](collection Slice, old, nEw T, n int) Slice

Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new. Play: https://go.dev/play/p/XfPzmf9gql6

Example
list := []int{0, 1, 0, 1, 2, 3, 0}

result := Replace(list, 0, 42, 1)
fmt.Printf("%v\n", result)

result = Replace(list, -1, 42, 1)
fmt.Printf("%v\n", result)

result = Replace(list, 0, 42, 2)
fmt.Printf("%v\n", result)

result = Replace(list, 0, 42, -1)
fmt.Printf("%v\n", result)
Output:

[42 1 0 1 2 3 0]
[0 1 0 1 2 3 0]
[42 1 42 1 2 3 0]
[42 1 42 1 2 3 42]

func ReplaceAll added in v1.19.0

func ReplaceAll[T comparable, Slice ~[]T](collection Slice, old, nEw T) Slice

ReplaceAll returns a copy of the slice with all non-overlapping instances of old replaced by new. Play: https://go.dev/play/p/a9xZFUHfYcV

func Reverse deprecated

func Reverse[T any, Slice ~[]T](collection Slice) Slice

Reverse reverses a slice so that the first element becomes the last, the second element becomes the second to last, and so on. Play: https://go.dev/play/p/iv2e9jslfBM

Deprecated: use mutable.Reverse() instead.

Example
list := []int{0, 1, 2, 3, 4, 5}

result := Reverse(list)

fmt.Printf("%v", result)
Output:

[5 4 3 2 1 0]

func RuneLength added in v1.19.0

func RuneLength(str string) int

RuneLength is an alias to utf8.RuneCountInString which returns the number of runes in string. Play: https://go.dev/play/p/tuhgW_lWY8l

Example
result1, chars1 := RuneLength("hellô"), len("hellô")
result2, chars2 := RuneLength("🤘"), len("🤘")

fmt.Printf("%v %v\n", result1, chars1)
fmt.Printf("%v %v\n", result2, chars2)
Output:

5 6
1 4

func Sample added in v1.6.0

func Sample[T any](collection []T) T

Sample returns a random item from collection. Play: https://go.dev/play/p/vCcSJbh5s6l

func SampleBy added in v1.49.0

func SampleBy[T any](collection []T, randomIntGenerator randomIntGenerator) T

SampleBy returns a random item from collection, using randomIntGenerator as the random index generator. Play: https://go.dev/play/p/HDmKmMgq0XN

func Samples added in v1.6.0

func Samples[T any, Slice ~[]T](collection Slice, count int) Slice

Samples returns N random unique items from collection. Play: https://go.dev/play/p/vCcSJbh5s6l

func SamplesBy added in v1.49.0

func SamplesBy[T any, Slice ~[]T](collection Slice, count int, randomIntGenerator randomIntGenerator) Slice

SamplesBy returns N random unique items from collection, using randomIntGenerator as the random index generator. Play: https://go.dev/play/p/HDmKmMgq0XN

func Shuffle deprecated

func Shuffle[T any, Slice ~[]T](collection Slice) Slice

Shuffle returns a slice of shuffled values. Uses the Fisher-Yates shuffle algorithm. Play: https://go.dev/play/p/ZTGG7OUCdnp

Deprecated: use mutable.Shuffle() instead.

Example
list := []int{0, 1, 2, 3, 4, 5}

result := Shuffle(list)

fmt.Printf("%v", result)

func Slice added in v1.22.0

func Slice[T any, Slice ~[]T](collection Slice, start, end int) Slice

Slice returns a copy of a slice from `start` up to, but not including `end`. Like `slice[start:end]`, but does not panic on overflow. Play: https://go.dev/play/p/8XWYhfMMA1h

Example
list := []int{0, 1, 2, 3, 4, 5}

result := Slice(list, 1, 4)
fmt.Printf("%v\n", result)

result = Slice(list, 4, 1)
fmt.Printf("%v\n", result)

result = Slice(list, 4, 5)
fmt.Printf("%v\n", result)
Output:

[1 2 3]
[]
[4]

func SliceToChannel added in v1.31.0

func SliceToChannel[T any](bufferSize int, collection []T) <-chan T

SliceToChannel returns a read-only channel of collection elements. Play: https://go.dev/play/p/lIbSY3QmiEg

func SliceToMap added in v1.27.0

func SliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V

SliceToMap returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original slice. Alias of Associate(). Play: https://go.dev/play/p/WHa2CfMO3Lr

Example
list := []string{"a", "aa", "aaa"}

result := SliceToMap(list, func(str string) (string, int) {
	return str, len(str)
})

fmt.Printf("%v", result)
Output:

map[a:1 aa:2 aaa:3]

func SliceToMapI added in v1.52.0

func SliceToMapI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V)) map[K]V

SliceToMapI returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original slice. Alias of AssociateI(). Play: https://go.dev/play/p/mMBm5GV3_eq

func SnakeCase added in v1.40.0

func SnakeCase(str string) string

SnakeCase converts string to snake case. Play: https://go.dev/play/p/ziB0V89IeVH

func Some

func Some[T comparable](collection, subset []T) bool

Some returns true if at least 1 element of a subset is contained in a collection. If the subset is empty Some returns false. Play: https://go.dev/play/p/Lj4ceFkeT9V

func SomeBy added in v1.18.0

func SomeBy[T any](collection []T, predicate func(item T) bool) bool

SomeBy returns true if the predicate returns true for any of the elements in the collection. If the collection is empty SomeBy returns false. Play: https://go.dev/play/p/DXF-TORBudx

func Splice added in v1.44.0

func Splice[T any, Slice ~[]T](collection Slice, i int, elements ...T) Slice

Splice inserts multiple elements at index i. A negative index counts back from the end of the slice. The helper is protected against overflow errors. Play: https://go.dev/play/p/G5_GhkeSUBA

func Subset added in v1.19.0

func Subset[T any, Slice ~[]T](collection Slice, offset int, length uint) Slice

Subset returns a copy of a slice from `offset` up to `length` elements. Like `slice[start:start+length]`, but does not panic on overflow. Play: https://go.dev/play/p/tOQu1GhFcog

Example
list := []int{0, 1, 2, 3, 4, 5}

result := Subset(list, 2, 3)

fmt.Printf("%v", result)
Output:

[2 3 4]

func Substring added in v1.19.0

func Substring[T ~string](str T, offset int, length uint) T

Substring return part of a string. Play: https://go.dev/play/p/TQlxQi82Lu1

Example
result1 := Substring("hello", 2, 3)
result2 := Substring("hello", -4, 3)
result3 := Substring("hello", -2, math.MaxUint)
result4 := Substring("🏠🐶🐱", 0, 2)
result5 := Substring("你好,世界", 0, 3)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output:

llo
ell
lo
🏠🐶
你好,

func Sum added in v1.32.0

func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T

Sum sums the values in a collection. If collection is empty 0 is returned. Play: https://go.dev/play/p/upfeJVqs4Bt

Example
list := []int{1, 2, 3, 4, 5}

sum := Sum(list)

fmt.Printf("%v", sum)
Output:

15

func SumBy added in v1.20.0

func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R

SumBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 0 is returned. Play: https://go.dev/play/p/Dz_a_7jN_ca

Example
list := []string{"foo", "bar"}

result := SumBy(list, func(item string) int {
	return len(item)
})

fmt.Printf("%v", result)
Output:

6

func Switch

func Switch[T comparable, R any](predicate T) *switchCase[T, R]

Switch is a pure functional switch/case/default statement. Play: https://go.dev/play/p/TGbKUMAeRUd

Example
result1 := Switch[int, string](1).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result2 := Switch[int, string](2).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result3 := Switch[int, string](42).
	Case(1, "1").
	Case(2, "2").
	Default("3")

result4 := Switch[int, string](1).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result5 := Switch[int, string](2).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

result6 := Switch[int, string](42).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3

func Synchronize added in v1.20.0

func Synchronize(opt ...sync.Locker) *synchronize

Synchronize wraps the underlying callback in a mutex. It receives an optional mutex. Play: https://go.dev/play/p/X3cqROSpQmu

func Ternary

func Ternary[T any](condition bool, ifOutput, elseOutput T) T

Ternary is a single line if/else statement. Take care to avoid dereferencing potentially nil pointers in your A/B expressions, because they are both evaluated. See TernaryF to avoid this problem. Play: https://go.dev/play/p/t-D7WBL44h2

Example
result := Ternary(true, "a", "b")

fmt.Printf("%v", result)
Output:

a

func TernaryF added in v1.30.0

func TernaryF[T any](condition bool, ifFunc, elseFunc func() T) T

TernaryF is a single line if/else statement whose options are functions. Play: https://go.dev/play/p/AO4VW20JoqM

Example
result := TernaryF(true, func() string { return "a" }, func() string { return "b" })

fmt.Printf("%v", result)
Output:

a

func Times added in v1.5.0

func Times[T any](count int, iteratee func(index int) T) []T

Times invokes the iteratee n times, returning a slice of the results of each invocation. The iteratee is invoked with index as argument. Play: https://go.dev/play/p/vgQj3Glr6lT

Example
result := Times(3, func(i int) string {
	return strconv.FormatInt(int64(i), 10)
})

fmt.Printf("%v", result)
Output:

[0 1 2]

func ToAnySlice added in v1.21.0

func ToAnySlice[T any](collection []T) []any

ToAnySlice returns a slice with all elements mapped to `any` type. Play: https://go.dev/play/p/P2sD0PMXw4F

func ToPtr

func ToPtr[T any](x T) *T

ToPtr returns a pointer copy of value. Play: https://go.dev/play/p/P2sD0PMXw4F

func ToSlicePtr

func ToSlicePtr[T any](collection []T) []*T

ToSlicePtr returns a slice of pointers to each value. Play: https://go.dev/play/p/P2sD0PMXw4F

func Trim added in v1.52.0

func Trim[T comparable, Slice ~[]T](collection, cutset Slice) Slice

Trim removes all the leading and trailing cutset from the collection. Play: https://go.dev/play/p/1an9mxLdRG5

Example
collection := []int{0, 1, 2, 0, 3, 0}

// Test with valid cutset
result := Trim(collection, []int{0})
fmt.Printf("Trim with cutset {0}: %v\n", result)

// Test with string collection
words := []string{"  hello  ", "world", "  "}
result2 := Trim(words, []string{" "})
fmt.Printf("Trim with string cutset: %v\n", result2)

// Test with no cutset elements
result3 := Trim(collection, []int{5})
fmt.Printf("Trim with cutset {5} (not present): %v\n", result3)
Output:

Trim with cutset {0}: [1 2 0 3]
Trim with string cutset: [  hello   world   ]
Trim with cutset {5} (not present): [0 1 2 0 3 0]

func TrimLeft added in v1.52.0

func TrimLeft[T comparable, Slice ~[]T](collection, cutset Slice) Slice

TrimLeft removes all the leading cutset from the collection. Play: https://go.dev/play/p/74aqfAYLmyi

Example
collection := []int{0, 1, 2, 0, 3, 0}

// Test with valid cutset
result := TrimLeft(collection, []int{0})
fmt.Printf("TrimLeft with cutset {0}: %v\n", result)

// Test with string collection
words := []string{"  hello  ", "world", "  "}
result2 := TrimLeft(words, []string{" "})
fmt.Printf("TrimLeft with string cutset: %v\n", result2)

// Test with no cutset elements
result3 := TrimLeft(collection, []int{5})
fmt.Printf("TrimLeft with cutset {5} (not present): %v\n", result3)
Output:

TrimLeft with cutset {0}: [1 2 0 3 0]
TrimLeft with string cutset: [  hello   world   ]
TrimLeft with cutset {5} (not present): [0 1 2 0 3 0]

func TrimPrefix added in v1.52.0

func TrimPrefix[T comparable, Slice ~[]T](collection, prefix Slice) Slice

TrimPrefix removes all the leading prefix from the collection. Play: https://go.dev/play/p/SHO6X-YegPg

Example
collection := []int{1, 2, 1, 2, 3}

// Test with valid prefix
result := TrimPrefix(collection, []int{1, 2})
fmt.Printf("TrimPrefix with prefix {1,2}: %v\n", result)

// Test with string collection
words := []string{"hello", "hello", "world"}
result2 := TrimPrefix(words, []string{"hello"})
fmt.Printf("TrimPrefix with string prefix: %v\n", result2)

// Test with prefix not present
result3 := TrimPrefix(collection, []int{5, 6})
fmt.Printf("TrimPrefix with prefix {5,6} (not present): %v\n", result3)
Output:

TrimPrefix with prefix {1,2}: [3]
TrimPrefix with string prefix: [world]
TrimPrefix with prefix {5,6} (not present): [1 2 1 2 3]

func TrimRight added in v1.52.0

func TrimRight[T comparable, Slice ~[]T](collection, cutset Slice) Slice

TrimRight removes all the trailing cutset from the collection. Play: https://go.dev/play/p/MRpAfR6sf0g

Example
collection := []int{0, 1, 2, 0, 3, 0}

// Test with valid cutset
result := TrimRight(collection, []int{0})
fmt.Printf("TrimRight with cutset {0}: %v\n", result)

// Test with string collection
words := []string{"  hello  ", "world", "  "}
result2 := TrimRight(words, []string{" "})
fmt.Printf("TrimRight with string cutset: %v\n", result2)

// Test with no cutset elements
result3 := TrimRight(collection, []int{5})
fmt.Printf("TrimRight with cutset {5} (not present): %v\n", result3)
Output:

TrimRight with cutset {0}: [0 1 2 0 3]
TrimRight with string cutset: [  hello   world   ]
TrimRight with cutset {5} (not present): [0 1 2 0 3 0]

func TrimSuffix added in v1.52.0

func TrimSuffix[T comparable, Slice ~[]T](collection, suffix Slice) Slice

TrimSuffix removes all the trailing suffix from the collection. Play: https://go.dev/play/p/IjEUrV0iofq

Example
collection := []int{1, 2, 1, 2, 3}

// Test with valid suffix
result := TrimSuffix(collection, []int{1, 2})
fmt.Printf("TrimSuffix with suffix {1,2}: %v\n", result)

// Test with string collection
words := []string{"hello", "world", "test"}
result2 := TrimSuffix(words, []string{"test"})
fmt.Printf("TrimSuffix with string suffix: %v\n", result2)

// Test with suffix not present
result3 := TrimSuffix(collection, []int{5, 6})
fmt.Printf("TrimSuffix with suffix {5,6} (not present): %v\n", result3)
Output:

TrimSuffix with suffix {1,2}: [1 2 1 2 3]
TrimSuffix with string suffix: [hello world]
TrimSuffix with suffix {5,6} (not present): [1 2 1 2 3]

func Try added in v1.11.0

func Try(callback func() error) (ok bool)

Try calls the function and return false in case of error.

Example
ok1 := Try(func() error {
	return nil
})
ok2 := Try(func() error {
	return errors.New("my error")
})
ok3 := Try(func() error {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try0 added in v1.11.0

func Try0(callback func()) bool

Try0 has the same behavior as Try, but callback returns no variable. Play: https://go.dev/play/p/mTyyWUvn9u4

func Try1 added in v1.11.0

func Try1(callback func() error) bool

Try1 is an alias to Try. Play: https://go.dev/play/p/mTyyWUvn9u4

Example
ok1 := Try1(func() error {
	return nil
})
ok2 := Try1(func() error {
	return errors.New("my error")
})
ok3 := Try1(func() error {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try2 added in v1.11.0

func Try2[T any](callback func() (T, error)) bool

Try2 has the same behavior as Try, but callback returns 2 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example
ok1 := Try2(func() (int, error) {
	return 42, nil
})
ok2 := Try2(func() (int, error) {
	return 42, errors.New("my error")
})
ok3 := Try2(func() (int, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try3 added in v1.11.0

func Try3[T, R any](callback func() (T, R, error)) bool

Try3 has the same behavior as Try, but callback returns 3 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example
ok1 := Try3(func() (int, string, error) {
	return 42, "foobar", nil
})
ok2 := Try3(func() (int, string, error) {
	return 42, "foobar", errors.New("my error")
})
ok3 := Try3(func() (int, string, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try4 added in v1.11.0

func Try4[T, R, S any](callback func() (T, R, S, error)) bool

Try4 has the same behavior as Try, but callback returns 4 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example
ok1 := Try4(func() (int, string, float64, error) {
	return 42, "foobar", 4.2, nil
})
ok2 := Try4(func() (int, string, float64, error) {
	return 42, "foobar", 4.2, errors.New("my error")
})
ok3 := Try4(func() (int, string, float64, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try5 added in v1.11.0

func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool

Try5 has the same behavior as Try, but callback returns 5 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example
ok1 := Try5(func() (int, string, float64, bool, error) {
	return 42, "foobar", 4.2, true, nil
})
ok2 := Try5(func() (int, string, float64, bool, error) {
	return 42, "foobar", 4.2, true, errors.New("my error")
})
ok3 := Try5(func() (int, string, float64, bool, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try6 added in v1.11.0

func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool

Try6 has the same behavior as Try, but callback returns 6 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example
ok1 := Try6(func() (int, string, float64, bool, foo, error) {
	return 42, "foobar", 4.2, true, foo{}, nil
})
ok2 := Try6(func() (int, string, float64, bool, foo, error) {
	return 42, "foobar", 4.2, true, foo{}, errors.New("my error")
})
ok3 := Try6(func() (int, string, float64, bool, foo, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func TryCatch added in v1.11.0

func TryCatch(callback func() error, catch func())

TryCatch has the same behavior as Try, but calls the catch function in case of error. Play: https://go.dev/play/p/PnOON-EqBiU

func TryCatchWithErrorValue added in v1.11.0

func TryCatchWithErrorValue(callback func() error, catch func(any))

TryCatchWithErrorValue has the same behavior as TryWithErrorValue, but calls the catch function in case of error. Play: https://go.dev/play/p/8Pc9gwX_GZO

Example
TryCatchWithErrorValue(
	func() error {
		panic("trigger an error")
	},
	func(err any) {
		fmt.Printf("catch: %s", err)
	},
)
Output:

catch: trigger an error

func TryOr added in v1.29.0

func TryOr[A any](callback func() (A, error), fallbackA A) (A, bool)

TryOr has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example
value1, ok1 := TryOr(func() (int, error) {
	return 42, nil
}, 21)
value2, ok2 := TryOr(func() (int, error) {
	return 42, errors.New("my error")
}, 21)
value3, ok3 := TryOr(func() (int, error) {
	panic("my error")
}, 21)

fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
Output:

42 true
21 false
21 false

func TryOr1 added in v1.29.0

func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool)

TryOr1 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example
value1, ok1 := TryOr1(func() (int, error) {
	return 42, nil
}, 21)
value2, ok2 := TryOr1(func() (int, error) {
	return 42, errors.New("my error")
}, 21)
value3, ok3 := TryOr1(func() (int, error) {
	panic("my error")
}, 21)

fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
Output:

42 true
21 false
21 false

func TryOr2 added in v1.29.0

func TryOr2[A, B any](callback func() (A, B, error), fallbackA A, fallbackB B) (A, B, bool)

TryOr2 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example
value1, value2, ok3 := TryOr2(func() (int, string, error) {
	panic("my error")
}, 21, "hello")

fmt.Printf("%v %v %v\n", value1, value2, ok3)
Output:

21 hello false

func TryOr3 added in v1.29.0

func TryOr3[A, B, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool)

TryOr3 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example
value1, value2, value3, ok3 := TryOr3(func() (int, string, bool, error) {
	panic("my error")
}, 21, "hello", false)

fmt.Printf("%v %v %v %v\n", value1, value2, value3, ok3)
Output:

21 hello false false

func TryOr4 added in v1.29.0

func TryOr4[A, B, C, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D) (A, B, C, D, bool)

TryOr4 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example
value1, value2, value3, value4, ok3 := TryOr4(func() (int, string, bool, foo, error) {
	panic("my error")
}, 21, "hello", false, foo{bar: "bar"})

fmt.Printf("%v %v %v %v %v\n", value1, value2, value3, value4, ok3)
Output:

21 hello false {bar} false

func TryOr5 added in v1.29.0

func TryOr5[A, B, C, D, E any](callback func() (A, B, C, D, E, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E) (A, B, C, D, E, bool)

TryOr5 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example
value1, value2, value3, value4, value5, ok3 := TryOr5(func() (int, string, bool, foo, float64, error) {
	panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2)

fmt.Printf("%v %v %v %v %v %v\n", value1, value2, value3, value4, value5, ok3)
Output:

21 hello false {bar} 4.2 false

func TryOr6 added in v1.29.0

func TryOr6[A, B, C, D, E, F any](callback func() (A, B, C, D, E, F, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E, fallbackF F) (A, B, C, D, E, F, bool)

TryOr6 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example
value1, value2, value3, value4, value5, value6, ok3 := TryOr6(func() (int, string, bool, foo, float64, string, error) {
	panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2, "world")

fmt.Printf("%v %v %v %v %v %v %v\n", value1, value2, value3, value4, value5, value6, ok3)
Output:

21 hello false {bar} 4.2 world false

func TryWithErrorValue added in v1.11.0

func TryWithErrorValue(callback func() error) (errorValue any, ok bool)

TryWithErrorValue has the same behavior as Try, but also returns value passed to panic. Play: https://go.dev/play/p/Kc7afQIT2Fs

Example
err1, ok1 := TryWithErrorValue(func() error {
	return nil
})
err2, ok2 := TryWithErrorValue(func() error {
	return errors.New("my error")
})
err3, ok3 := TryWithErrorValue(func() error {
	panic("my error")
})

fmt.Printf("%v %v\n", err1, ok1)
fmt.Printf("%v %v\n", err2, ok2)
fmt.Printf("%v %v\n", err3, ok3)
Output:

<nil> true
my error false
my error false

func Union added in v1.8.0

func Union[T comparable, Slice ~[]T](lists ...Slice) Slice

Union returns all distinct elements from given collections. result returns will not change the order of elements relatively. Play: https://go.dev/play/p/DI9RVEB_qMK

func Uniq

func Uniq[T comparable, Slice ~[]T](collection Slice) Slice

Uniq returns a duplicate-free version of a slice, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the slice. Play: https://go.dev/play/p/DTzbeXZ6iEN

Example
list := []int{1, 2, 2, 1}

result := Uniq(list)

fmt.Printf("%v", result)
Output:

[1 2]

func UniqBy

func UniqBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice

UniqBy returns a duplicate-free version of a slice, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the slice. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed. Play: https://go.dev/play/p/g42Z3QSb53u

Example
list := []int{0, 1, 2, 3, 4, 5}

result := UniqBy(list, func(i int) int {
	return i % 3
})

fmt.Printf("%v", result)
Output:

[0 1 2]

func UniqKeys added in v1.47.0

func UniqKeys[K comparable, V any](in ...map[K]V) []K

UniqKeys creates a slice of unique keys in the map. Play: https://go.dev/play/p/TPKAb6ILdHk

Example
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"bar": 3}

result := UniqKeys(kv, kv2)
sort.Strings(result)
fmt.Printf("%v", result)
Output:

[bar foo]

func UniqMap added in v1.48.0

func UniqMap[T any, R comparable](collection []T, iteratee func(item T, index int) R) []R

UniqMap manipulates a slice and transforms it to a slice of another type with unique values. Play: https://go.dev/play/p/fygzLBhvUdB

Example
type User struct {
	Name string
	Age  int
}
users := []User{{Name: "Alex", Age: 10}, {Name: "Alex", Age: 12}, {Name: "Bob", Age: 11}, {Name: "Alice", Age: 20}}

result := UniqMap(users, func(u User, index int) string {
	return u.Name
})

fmt.Printf("%v", result)
Output:

[Alex Bob Alice]

func UniqValues added in v1.47.0

func UniqValues[K, V comparable](in ...map[K]V) []V

UniqValues creates a slice of unique values in the map. Play: https://go.dev/play/p/nf6bXMh7rM3

Example
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 2}

result := UniqValues(kv, kv2)

sort.Ints(result)
fmt.Printf("%v", result)
Output:

[1 2]

func Unpack2 added in v1.17.0

func Unpack2[A, B any](tuple Tuple2[A, B]) (A, B)

Unpack2 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example
a, b := Unpack2(T2("hello", 2))
fmt.Printf("%v %v", a, b)
Output:

hello 2

func Unpack3 added in v1.17.0

func Unpack3[A, B, C any](tuple Tuple3[A, B, C]) (A, B, C)

Unpack3 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example
a, b, c := Unpack3(T3("hello", 2, true))
fmt.Printf("%v %v %v", a, b, c)
Output:

hello 2 true

func Unpack4 added in v1.17.0

func Unpack4[A, B, C, D any](tuple Tuple4[A, B, C, D]) (A, B, C, D)

Unpack4 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example
a, b, c, d := Unpack4(T4("hello", 2, true, foo{bar: "bar"}))
fmt.Printf("%v %v %v %v", a, b, c, d)
Output:

hello 2 true {bar}

func Unpack5 added in v1.17.0

func Unpack5[A, B, C, D, E any](tuple Tuple5[A, B, C, D, E]) (A, B, C, D, E)

Unpack5 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example
a, b, c, d, e := Unpack5(T5("hello", 2, true, foo{bar: "bar"}, 4.2))
fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
Output:

hello 2 true {bar} 4.2

func Unpack6 added in v1.17.0

func Unpack6[A, B, C, D, E, F any](tuple Tuple6[A, B, C, D, E, F]) (A, B, C, D, E, F)

Unpack6 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example
a, b, c, d, e, f := Unpack6(T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop"))
fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
Output:

hello 2 true {bar} 4.2 plop

func Unpack7 added in v1.17.0

func Unpack7[A, B, C, D, E, F, G any](tuple Tuple7[A, B, C, D, E, F, G]) (A, B, C, D, E, F, G)

Unpack7 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example
a, b, c, d, e, f, g := Unpack7(T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false))
fmt.Printf("%v %v %v %v %v %v %v", a, b, c, d, e, f, g)
Output:

hello 2 true {bar} 4.2 plop false

func Unpack8 added in v1.17.0

func Unpack8[A, B, C, D, E, F, G, H any](tuple Tuple8[A, B, C, D, E, F, G, H]) (A, B, C, D, E, F, G, H)

Unpack8 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example
a, b, c, d, e, f, g, h := Unpack8(T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42))
fmt.Printf("%v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h)
Output:

hello 2 true {bar} 4.2 plop false 42

func Unpack9 added in v1.17.0

func Unpack9[A, B, C, D, E, F, G, H, I any](tuple Tuple9[A, B, C, D, E, F, G, H, I]) (A, B, C, D, E, F, G, H, I)

Unpack9 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example
a, b, c, d, e, f, g, h, i := Unpack9(T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world"))
fmt.Printf("%v %v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h, i)
Output:

hello 2 true {bar} 4.2 plop false 42 hello world

func Unzip2 added in v1.4.0

func Unzip2[A, B any](tuples []Tuple2[A, B]) ([]A, []B)

Unzip2 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW

Example
a, b := Unzip2([]Tuple2[string, int]{T2("hello", 2)})
fmt.Printf("%v %v", a, b)
Output:

[hello] [2]

func Unzip3 added in v1.4.0

func Unzip3[A, B, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C)

Unzip3 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW

Example
a, b, c := Unzip3([]Tuple3[string, int, bool]{T3("hello", 2, true)})
fmt.Printf("%v %v %v", a, b, c)
Output:

[hello] [2] [true]

func Unzip4 added in v1.4.0

func Unzip4[A, B, C, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D)

Unzip4 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW

Example
a, b, c, d := Unzip4([]Tuple4[string, int, bool, foo]{T4("hello", 2, true, foo{bar: "bar"})})
fmt.Printf("%v %v %v %v", a, b, c, d)
Output:

[hello] [2] [true] [{bar}]

func Unzip5 added in v1.4.0

func Unzip5[A, B, C, D, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, []D, []E)

Unzip5 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW

Example
a, b, c, d, e := Unzip5([]Tuple5[string, int, bool, foo, float64]{T5("hello", 2, true, foo{bar: "bar"}, 4.2)})
fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
Output:

[hello] [2] [true] [{bar}] [4.2]

func Unzip6 added in v1.4.0

func Unzip6[A, B, C, D, E, F any](tuples []Tuple6[A, B, C, D, E, F]) ([]A, []B, []C, []D, []E, []F)

Unzip6 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW

Example
a, b, c, d, e, f := Unzip6([]Tuple6[string, int, bool, foo, float64, string]{T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop")})
fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
Output:

[hello] [2] [true] [{bar}] [4.2] [plop]

func Unzip7 added in v1.4.0

func Unzip7[A, B, C, D, E, F, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, []B, []C, []D, []E, []F, []G)

Unzip7 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW

Example
a, b, c, d, e, f, g := Unzip7([]Tuple7[string, int, bool, foo, float64, string, bool]{T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false)})
fmt.Printf("%v %v %v %v %v %v %v", a, b, c, d, e, f, g)
Output:

[hello] [2] [true] [{bar}] [4.2] [plop] [false]

func Unzip8 added in v1.4.0

func Unzip8[A, B, C, D, E, F, G, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) ([]A, []B, []C, []D, []E, []F, []G, []H)

Unzip8 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW

Example
a, b, c, d, e, f, g, h := Unzip8([]Tuple8[string, int, bool, foo, float64, string, bool, int]{T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42)})
fmt.Printf("%v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h)
Output:

[hello] [2] [true] [{bar}] [4.2] [plop] [false] [42]

func Unzip9 added in v1.4.0

func Unzip9[A, B, C, D, E, F, G, H, I any](tuples []Tuple9[A, B, C, D, E, F, G, H, I]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I)

Unzip9 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW

Example
a, b, c, d, e, f, g, h, i := Unzip9([]Tuple9[string, int, bool, foo, float64, string, bool, int, string]{T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world")})
fmt.Printf("%v %v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h, i)
Output:

[hello] [2] [true] [{bar}] [4.2] [plop] [false] [42] [hello world]

func UnzipBy2 added in v1.40.0

func UnzipBy2[In, A, B any](items []In, iteratee func(In) (a A, b B)) ([]A, []B)

UnzipBy2 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/tN8yqaRZz0r

func UnzipBy3 added in v1.40.0

func UnzipBy3[In, A, B, C any](items []In, iteratee func(In) (a A, b B, c C)) ([]A, []B, []C)

UnzipBy3 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/36ITO2DlQq1

func UnzipBy4 added in v1.40.0

func UnzipBy4[In, A, B, C, D any](items []In, iteratee func(In) (a A, b B, c C, d D)) ([]A, []B, []C, []D)

UnzipBy4 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/zJ6qY1dD1rL

func UnzipBy5 added in v1.40.0

func UnzipBy5[In, A, B, C, D, E any](items []In, iteratee func(In) (a A, b B, c C, d D, e E)) ([]A, []B, []C, []D, []E)

UnzipBy5 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/3f7jKkV9xZt

func UnzipBy6 added in v1.40.0

func UnzipBy6[In, A, B, C, D, E, F any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F)) ([]A, []B, []C, []D, []E, []F)

UnzipBy6 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/8Y1b7tKu2pL

func UnzipBy7 added in v1.40.0

func UnzipBy7[In, A, B, C, D, E, F, G any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G)) ([]A, []B, []C, []D, []E, []F, []G)

UnzipBy7 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/7j1kLmVn3pM

func UnzipBy8 added in v1.40.0

func UnzipBy8[In, A, B, C, D, E, F, G, H any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G, h H)) ([]A, []B, []C, []D, []E, []F, []G, []H)

UnzipBy8 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/1n2k3L4m5N6

func UnzipBy9 added in v1.40.0

func UnzipBy9[In, A, B, C, D, E, F, G, H, I any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G, h H, i I)) ([]A, []B, []C, []D, []E, []F, []G, []H, []I)

UnzipBy9 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/7o8p9q0r1s2

func Validate added in v1.30.0

func Validate(ok bool, format string, args ...any) error

Validate is a helper that creates an error when a condition is not met. Play: https://go.dev/play/p/vPyh51XpCBt

Example
i := 42

err1 := Validate(i < 0, "expected %d < 0", i)
err2 := Validate(i > 0, "expected %d > 0", i)

fmt.Printf("%v\n%v", err1, err2)
Output:

expected 42 < 0
<nil>

func ValueOr added in v1.38.0

func ValueOr[K comparable, V any](in map[K]V, key K, fallback V) V

ValueOr returns the value of the given key or the fallback value if the key is not present. Play: https://go.dev/play/p/bAq9mHErB4V

Example
kv := map[string]int{"foo": 1, "bar": 2}

result1 := ValueOr(kv, "foo", 42)
result2 := ValueOr(kv, "baz", 42)

fmt.Printf("%v %v", result1, result2)
Output:

1 42

func Values

func Values[K comparable, V any](in ...map[K]V) []V

Values creates a slice of the map values. Play: https://go.dev/play/p/nnRTQkzQfF6

Example
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 3}

result := Values(kv, kv2)

sort.Ints(result)
fmt.Printf("%v", result)
Output:

[1 2 3]

func WaitFor added in v1.43.0

func WaitFor(condition func(i int) bool, timeout, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool)

WaitFor runs periodically until a condition is validated. Play: https://go.dev/play/p/t_wTDmubbK3

func WaitForWithContext added in v1.46.0

func WaitForWithContext(ctx context.Context, condition func(ctx context.Context, currentIteration int) bool, timeout, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool)

WaitForWithContext runs periodically until a condition is validated or context is canceled. Play: https://go.dev/play/p/t_wTDmubbK3

func Without added in v1.24.0

func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice

Without returns a slice excluding all given values. Play: https://go.dev/play/p/5j30Ux8TaD0

func WithoutBy added in v1.48.0

func WithoutBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K, exclude ...K) Slice

WithoutBy filters a slice by excluding elements whose extracted keys match any in the exclude list. Returns a new slice containing only the elements whose keys are not in the exclude list. Play: https://go.dev/play/p/VgWJOF01NbJ

Example
type User struct {
	ID   int
	Name string
}
// original users
users := []User{
	{ID: 1, Name: "Alice"},
	{ID: 2, Name: "Bob"},
	{ID: 3, Name: "Charlie"},
}

// exclude users with IDs 2 and 3
excludedIDs := []int{2, 3}

// extract function to get the user ID
extractID := func(user User) int {
	return user.ID
}

// filtering users
filteredUsers := WithoutBy(users, extractID, excludedIDs...)

// output the filtered users
fmt.Printf("%v", filteredUsers)
Output:

[{1 Alice}]

func WithoutEmpty deprecated added in v1.24.0

func WithoutEmpty[T comparable, Slice ~[]T](collection Slice) Slice

WithoutEmpty returns a slice excluding zero values.

Deprecated: Use lo.Compact instead.

func WithoutNth added in v1.48.0

func WithoutNth[T comparable, Slice ~[]T](collection Slice, nths ...int) Slice

WithoutNth returns a slice excluding the nth value. Play: https://go.dev/play/p/5g3F9R2H1xL

func Words added in v1.40.0

func Words(str string) []string

Words splits string into a slice of its words. Play: https://go.dev/play/p/-f3VIQqiaVw

func ZipBy2 added in v1.40.0

func ZipBy2[A, B, Out any](a []A, b []B, iteratee func(a A, b B) Out) []Out

ZipBy2 creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/wlHur6yO8rR

func ZipBy3 added in v1.40.0

func ZipBy3[A, B, C, Out any](a []A, b []B, c []C, iteratee func(a A, b B, c C) Out) []Out

ZipBy3 creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/j9maveOnSQX

func ZipBy4 added in v1.40.0

func ZipBy4[A, B, C, D, Out any](a []A, b []B, c []C, d []D, iteratee func(a A, b B, c C, d D) Out) []Out

ZipBy4 creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/Y1eF2Ke0Ayz

func ZipBy5 added in v1.40.0

func ZipBy5[A, B, C, D, E, Out any](a []A, b []B, c []C, d []D, e []E, iteratee func(a A, b B, c C, d D, e E) Out) []Out

ZipBy5 creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/SLynyalh5Oa

func ZipBy6 added in v1.40.0

func ZipBy6[A, B, C, D, E, F, Out any](a []A, b []B, c []C, d []D, e []E, f []F, iteratee func(a A, b B, c C, d D, e E, f F) Out) []Out

ZipBy6 creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/IK6KVgw9e-S

func ZipBy7 added in v1.40.0

func ZipBy7[A, B, C, D, E, F, G, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, iteratee func(a A, b B, c C, d D, e E, f F, g G) Out) []Out

ZipBy7 creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/4uW6a2vXh8w

func ZipBy8 added in v1.40.0

func ZipBy8[A, B, C, D, E, F, G, H, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, iteratee func(a A, b B, c C, d D, e E, f F, g G, h H) Out) []Out

ZipBy8 creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/tk8xW7XzY4v

func ZipBy9 added in v1.40.0

func ZipBy9[A, B, C, D, E, F, G, H, I, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I, iteratee func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) []Out

ZipBy9 creates a slice of transformed elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/VGqjDmQ9YqX

Types

type Clonable

type Clonable[T any] interface {
	Clone() T
}

Clonable defines a constraint of types having Clone() T method.

type DispatchingStrategy added in v1.28.0

type DispatchingStrategy[T any] func(msg T, index uint64, channels []<-chan T) int

DispatchingStrategy is a function that distributes messages to channels.

func DispatchingStrategyWeightedRandom added in v1.28.0

func DispatchingStrategyWeightedRandom[T any](weights []int) DispatchingStrategy[T]

DispatchingStrategyWeightedRandom distributes messages in a weighted manner. If the channel capacity is exceeded, another random channel will be selected and so on. Play: https://go.dev/play/p/v0eMh8NZG2L

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

Entry defines a key/value pairs.

func Entries

func Entries[K comparable, V any](in map[K]V) []Entry[K, V]

Entries transforms a map into a slice of key/value pairs. Play: https://go.dev/play/p/_t4Xe34-Nl5

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := Entries(kv)

sort.Slice(result, func(i, j int) bool {
	return strings.Compare(result[i].Key, result[j].Key) < 0
})
fmt.Printf("%v", result)
Output:

[{bar 2} {baz 3} {foo 1}]

func ToPairs added in v1.22.0

func ToPairs[K comparable, V any](in map[K]V) []Entry[K, V]

ToPairs transforms a map into a slice of key/value pairs. Alias of Entries(). Play: https://go.dev/play/p/3Dhgx46gawJ

type Transaction added in v1.37.0

type Transaction[T any] struct {
	// contains filtered or unexported fields
}

Transaction implements a Saga pattern.

Example
transaction := NewTransaction[int]().
	Then(
		func(state int) (int, error) {
			fmt.Println("step 1")
			return state + 10, nil
		},
		func(state int) int {
			fmt.Println("rollback 1")
			return state - 10
		},
	).
	Then(
		func(state int) (int, error) {
			fmt.Println("step 2")
			return state + 15, nil
		},
		func(state int) int {
			fmt.Println("rollback 2")
			return state - 15
		},
	).
	Then(
		func(state int) (int, error) {
			fmt.Println("step 3")

			if true {
				return state, errors.New("error")
			}

			return state + 42, nil
		},
		func(state int) int {
			fmt.Println("rollback 3")
			return state - 42
		},
	)

_, _ = transaction.Process(-5)
Output:

step 1
step 2
step 3
rollback 2
rollback 1
Example (Error)
transaction := NewTransaction[int]().
	Then(
		func(state int) (int, error) {
			return state + 10, nil
		},
		func(state int) int {
			return state - 10
		},
	).
	Then(
		func(state int) (int, error) {
			return state, errors.New("error")
		},
		func(state int) int {
			return state - 15
		},
	).
	Then(
		func(state int) (int, error) {
			return state + 42, nil
		},
		func(state int) int {
			return state - 42
		},
	)

state, err := transaction.Process(-5)

fmt.Println(state)
fmt.Println(err)
Output:

-5
error
Example (Ok)
transaction := NewTransaction[int]().
	Then(
		func(state int) (int, error) {
			return state + 10, nil
		},
		func(state int) int {
			return state - 10
		},
	).
	Then(
		func(state int) (int, error) {
			return state + 15, nil
		},
		func(state int) int {
			return state - 15
		},
	).
	Then(
		func(state int) (int, error) {
			return state + 42, nil
		},
		func(state int) int {
			return state - 42
		},
	)

state, err := transaction.Process(-5)

fmt.Println(state)
fmt.Println(err)
Output:

62
<nil>

func NewTransaction added in v1.37.0

func NewTransaction[T any]() *Transaction[T]

NewTransaction instantiate a new transaction. Play: https://go.dev/play/p/Qxrd7MGQGh1

func (*Transaction[T]) Process added in v1.37.0

func (t *Transaction[T]) Process(state T) (T, error)

Process runs the Transaction steps and rollbacks in case of errors. Play: https://go.dev/play/p/Qxrd7MGQGh1 https://go.dev/play/p/xrHb2_kMvTY

func (*Transaction[T]) Then added in v1.37.0

func (t *Transaction[T]) Then(exec func(T) (T, error), onRollback func(T) T) *Transaction[T]

Then adds a step to the chain of callbacks. Returns the same Transaction. Play: https://go.dev/play/p/Qxrd7MGQGh1 https://go.dev/play/p/xrHb2_kMvTY

type Tuple2 added in v1.4.0

type Tuple2[A, B any] struct {
	A A
	B B
}

Tuple2 is a group of 2 elements (pair).

func CrossJoin2 added in v1.48.0

func CrossJoin2[A, B any](listA []A, listB []B) []Tuple2[A, B]

CrossJoin2 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/3VFppyL9FDU

Example
result := CrossJoin2([]string{"a", "b"}, []int{1, 2, 3, 4})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1}
{a 2}
{a 3}
{a 4}
{b 1}
{b 2}
{b 3}
{b 4}

func T2 added in v1.13.0

func T2[A, B any](a A, b B) Tuple2[A, B]

T2 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example
result := T2("hello", 2)
fmt.Printf("%v %v", result.A, result.B)
Output:

hello 2

func Zip2 added in v1.4.0

func Zip2[A, B any](a []A, b []B) []Tuple2[A, B]

Zip2 creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/jujaA6GaJTp

Example
result := Zip2([]string{"hello"}, []int{2})
fmt.Printf("%v", result)
Output:

[{hello 2}]

func (Tuple2[A, B]) Unpack added in v1.32.0

func (t Tuple2[A, B]) Unpack() (A, B)

Unpack returns values contained in a tuple. Play: https://go.dev/play/p/yrtn7QJTmL_E

type Tuple3 added in v1.4.0

type Tuple3[A, B, C any] struct {
	A A
	B B
	C C
}

Tuple3 is a group of 3 elements.

func CrossJoin3 added in v1.48.0

func CrossJoin3[A, B, C any](listA []A, listB []B, listC []C) []Tuple3[A, B, C]

CrossJoin3 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/2WGeHyJj4fK

Example
result := CrossJoin3([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true}
{a 1 false}
{a 2 true}
{a 2 false}
{a 3 true}
{a 3 false}
{a 4 true}
{a 4 false}
{b 1 true}
{b 1 false}
{b 2 true}
{b 2 false}
{b 3 true}
{b 3 false}
{b 4 true}
{b 4 false}

func T3 added in v1.13.0

func T3[A, B, C any](a A, b B, c C) Tuple3[A, B, C]

T3 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example
result := T3("hello", 2, true)
fmt.Printf("%v %v %v", result.A, result.B, result.C)
Output:

hello 2 true

func Zip3 added in v1.4.0

func Zip3[A, B, C any](a []A, b []B, c []C) []Tuple3[A, B, C]

Zip3 creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/jujaA6GaJTp

Example
result := Zip3([]string{"hello"}, []int{2}, []bool{true})
fmt.Printf("%v", result)
Output:

[{hello 2 true}]

func (Tuple3[A, B, C]) Unpack added in v1.32.0

func (t Tuple3[A, B, C]) Unpack() (A, B, C)

Unpack returns values contained in a tuple. Play: https://go.dev/play/p/yrtn7QJTmL_E

type Tuple4 added in v1.4.0

type Tuple4[A, B, C, D any] struct {
	A A
	B B
	C C
	D D
}

Tuple4 is a group of 4 elements.

func CrossJoin4 added in v1.48.0

func CrossJoin4[A, B, C, D any](listA []A, listB []B, listC []C, listD []D) []Tuple4[A, B, C, D]

CrossJoin4 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/6XhKjLmMnNp

Example
result := CrossJoin4([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar}}
{a 1 false {bar}}
{a 2 true {bar}}
{a 2 false {bar}}
{a 3 true {bar}}
{a 3 false {bar}}
{a 4 true {bar}}
{a 4 false {bar}}
{b 1 true {bar}}
{b 1 false {bar}}
{b 2 true {bar}}
{b 2 false {bar}}
{b 3 true {bar}}
{b 3 false {bar}}
{b 4 true {bar}}
{b 4 false {bar}}

func T4 added in v1.13.0

func T4[A, B, C, D any](a A, b B, c C, d D) Tuple4[A, B, C, D]

T4 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example
result := T4("hello", 2, true, foo{bar: "bar"})
fmt.Printf("%v %v %v %v", result.A, result.B, result.C, result.D)
Output:

hello 2 true {bar}

func Zip4 added in v1.4.0

func Zip4[A, B, C, D any](a []A, b []B, c []C, d []D) []Tuple4[A, B, C, D]

Zip4 creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/jujaA6GaJTp

Example
result := Zip4([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar}}]

func (Tuple4[A, B, C, D]) Unpack added in v1.32.0

func (t Tuple4[A, B, C, D]) Unpack() (A, B, C, D)

Unpack returns values contained in a tuple. Play: https://go.dev/play/p/yrtn7QJTmL_E

type Tuple5 added in v1.4.0

type Tuple5[A, B, C, D, E any] struct {
	A A
	B B
	C C
	D D
	E E
}

Tuple5 is a group of 5 elements.

func CrossJoin5 added in v1.48.0

func CrossJoin5[A, B, C, D, E any](listA []A, listB []B, listC []C, listD []D, listE []E) []Tuple5[A, B, C, D, E]

CrossJoin5 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/7oPqRsTuVwX

Example
result := CrossJoin5([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2}
{a 1 false {bar} 4.2}
{a 2 true {bar} 4.2}
{a 2 false {bar} 4.2}
{a 3 true {bar} 4.2}
{a 3 false {bar} 4.2}
{a 4 true {bar} 4.2}
{a 4 false {bar} 4.2}
{b 1 true {bar} 4.2}
{b 1 false {bar} 4.2}
{b 2 true {bar} 4.2}
{b 2 false {bar} 4.2}
{b 3 true {bar} 4.2}
{b 3 false {bar} 4.2}
{b 4 true {bar} 4.2}
{b 4 false {bar} 4.2}

func T5 added in v1.13.0

func T5[A, B, C, D, E any](a A, b B, c C, d D, e E) Tuple5[A, B, C, D, E]

T5 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example
result := T5("hello", 2, true, foo{bar: "bar"}, 4.2)
fmt.Printf("%v %v %v %v %v", result.A, result.B, result.C, result.D, result.E)
Output:

hello 2 true {bar} 4.2

func Zip5 added in v1.4.0

func Zip5[A, B, C, D, E any](a []A, b []B, c []C, d []D, e []E) []Tuple5[A, B, C, D, E]

Zip5 creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/jujaA6GaJTp

Example
result := Zip5([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2}]

func (Tuple5[A, B, C, D, E]) Unpack added in v1.32.0

func (t Tuple5[A, B, C, D, E]) Unpack() (A, B, C, D, E)

Unpack returns values contained in a tuple. Play: https://go.dev/play/p/7J4KrtgtK3M

type Tuple6 added in v1.4.0

type Tuple6[A, B, C, D, E, F any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
}

Tuple6 is a group of 6 elements.

func CrossJoin6 added in v1.48.0

func CrossJoin6[A, B, C, D, E, F any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F) []Tuple6[A, B, C, D, E, F]

CrossJoin6 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/8yZ1aB2cD3e

Example
result := CrossJoin6([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2 plop}
{a 1 false {bar} 4.2 plop}
{a 2 true {bar} 4.2 plop}
{a 2 false {bar} 4.2 plop}
{a 3 true {bar} 4.2 plop}
{a 3 false {bar} 4.2 plop}
{a 4 true {bar} 4.2 plop}
{a 4 false {bar} 4.2 plop}
{b 1 true {bar} 4.2 plop}
{b 1 false {bar} 4.2 plop}
{b 2 true {bar} 4.2 plop}
{b 2 false {bar} 4.2 plop}
{b 3 true {bar} 4.2 plop}
{b 3 false {bar} 4.2 plop}
{b 4 true {bar} 4.2 plop}
{b 4 false {bar} 4.2 plop}

func T6 added in v1.13.0

func T6[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F) Tuple6[A, B, C, D, E, F]

T6 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example
result := T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop")
fmt.Printf("%v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F)
Output:

hello 2 true {bar} 4.2 plop

func Zip6 added in v1.4.0

func Zip6[A, B, C, D, E, F any](a []A, b []B, c []C, d []D, e []E, f []F) []Tuple6[A, B, C, D, E, F]

Zip6 creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/jujaA6GaJTp

Example
result := Zip6([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2 plop}]

func (Tuple6[A, B, C, D, E, F]) Unpack added in v1.32.0

func (t Tuple6[A, B, C, D, E, F]) Unpack() (A, B, C, D, E, F)

Unpack returns values contained in a tuple. Play: https://go.dev/play/p/7J4KrtgtK3M

type Tuple7 added in v1.4.0

type Tuple7[A, B, C, D, E, F, G any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
}

Tuple7 is a group of 7 elements.

func CrossJoin7 added in v1.48.0

func CrossJoin7[A, B, C, D, E, F, G any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G) []Tuple7[A, B, C, D, E, F, G]

CrossJoin7 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/9f4g5h6i7j8

Example
result := CrossJoin7([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2 plop false}
{a 1 false {bar} 4.2 plop false}
{a 2 true {bar} 4.2 plop false}
{a 2 false {bar} 4.2 plop false}
{a 3 true {bar} 4.2 plop false}
{a 3 false {bar} 4.2 plop false}
{a 4 true {bar} 4.2 plop false}
{a 4 false {bar} 4.2 plop false}
{b 1 true {bar} 4.2 plop false}
{b 1 false {bar} 4.2 plop false}
{b 2 true {bar} 4.2 plop false}
{b 2 false {bar} 4.2 plop false}
{b 3 true {bar} 4.2 plop false}
{b 3 false {bar} 4.2 plop false}
{b 4 true {bar} 4.2 plop false}
{b 4 false {bar} 4.2 plop false}

func T7 added in v1.13.0

func T7[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G) Tuple7[A, B, C, D, E, F, G]

T7 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example
result := T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false)
fmt.Printf("%v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G)
Output:

hello 2 true {bar} 4.2 plop false

func Zip7 added in v1.4.0

func Zip7[A, B, C, D, E, F, G any](a []A, b []B, c []C, d []D, e []E, f []F, g []G) []Tuple7[A, B, C, D, E, F, G]

Zip7 creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/jujaA6GaJTp

Example
result := Zip7([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2 plop false}]

func (Tuple7[A, B, C, D, E, F, G]) Unpack added in v1.32.0

func (t Tuple7[A, B, C, D, E, F, G]) Unpack() (A, B, C, D, E, F, G)

Unpack returns values contained in a tuple. Play: https://go.dev/play/p/Ow9Zgf_zeiA

type Tuple8 added in v1.4.0

type Tuple8[A, B, C, D, E, F, G, H any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
	H H
}

Tuple8 is a group of 8 elements.

func CrossJoin8 added in v1.48.0

func CrossJoin8[A, B, C, D, E, F, G, H any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H) []Tuple8[A, B, C, D, E, F, G, H]

CrossJoin8 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/0k1l2m3n4o5

Example
result := CrossJoin8([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2 plop false 42}
{a 1 false {bar} 4.2 plop false 42}
{a 2 true {bar} 4.2 plop false 42}
{a 2 false {bar} 4.2 plop false 42}
{a 3 true {bar} 4.2 plop false 42}
{a 3 false {bar} 4.2 plop false 42}
{a 4 true {bar} 4.2 plop false 42}
{a 4 false {bar} 4.2 plop false 42}
{b 1 true {bar} 4.2 plop false 42}
{b 1 false {bar} 4.2 plop false 42}
{b 2 true {bar} 4.2 plop false 42}
{b 2 false {bar} 4.2 plop false 42}
{b 3 true {bar} 4.2 plop false 42}
{b 3 false {bar} 4.2 plop false 42}
{b 4 true {bar} 4.2 plop false 42}
{b 4 false {bar} 4.2 plop false 42}

func T8 added in v1.13.0

func T8[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) Tuple8[A, B, C, D, E, F, G, H]

T8 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example
result := T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42)
fmt.Printf("%v %v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G, result.H)
Output:

hello 2 true {bar} 4.2 plop false 42

func Zip8 added in v1.4.0

func Zip8[A, B, C, D, E, F, G, H any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H) []Tuple8[A, B, C, D, E, F, G, H]

Zip8 creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/jujaA6GaJTp

Example
result := Zip8([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2 plop false 42}]

func (Tuple8[A, B, C, D, E, F, G, H]) Unpack added in v1.32.0

func (t Tuple8[A, B, C, D, E, F, G, H]) Unpack() (A, B, C, D, E, F, G, H)

Unpack returns values contained in a tuple. Play: https://go.dev/play/p/Ow9Zgf_zeiA

type Tuple9 added in v1.4.0

type Tuple9[A, B, C, D, E, F, G, H, I any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
	H H
	I I
}

Tuple9 is a group of 9 elements.

func CrossJoin9 added in v1.48.0

func CrossJoin9[A, B, C, D, E, F, G, H, I any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, listI []I) []Tuple9[A, B, C, D, E, F, G, H, I]

CrossJoin9 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/6p7q8r9s0t1

Example
result := CrossJoin9([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"})
for _, r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2 plop false 42 hello world}
{a 1 false {bar} 4.2 plop false 42 hello world}
{a 2 true {bar} 4.2 plop false 42 hello world}
{a 2 false {bar} 4.2 plop false 42 hello world}
{a 3 true {bar} 4.2 plop false 42 hello world}
{a 3 false {bar} 4.2 plop false 42 hello world}
{a 4 true {bar} 4.2 plop false 42 hello world}
{a 4 false {bar} 4.2 plop false 42 hello world}
{b 1 true {bar} 4.2 plop false 42 hello world}
{b 1 false {bar} 4.2 plop false 42 hello world}
{b 2 true {bar} 4.2 plop false 42 hello world}
{b 2 false {bar} 4.2 plop false 42 hello world}
{b 3 true {bar} 4.2 plop false 42 hello world}
{b 3 false {bar} 4.2 plop false 42 hello world}
{b 4 true {bar} 4.2 plop false 42 hello world}
{b 4 false {bar} 4.2 plop false 42 hello world}

func T9 added in v1.13.0

func T9[A, B, C, D, E, F, G, H, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) Tuple9[A, B, C, D, E, F, G, H, I]

T9 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example
result := T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world")
fmt.Printf("%v %v %v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G, result.H, result.I)
Output:

hello 2 true {bar} 4.2 plop false 42 hello world

func Zip9 added in v1.4.0

func Zip9[A, B, C, D, E, F, G, H, I any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I) []Tuple9[A, B, C, D, E, F, G, H, I]

Zip9 creates a slice of grouped elements, the first of which contains the first elements of the given slices, the second of which contains the second elements of the given slices, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/jujaA6GaJTp

Example
result := Zip9([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2 plop false 42 hello world}]

func (Tuple9[A, B, C, D, E, F, G, H, I]) Unpack added in v1.32.0

func (t Tuple9[A, B, C, D, E, F, G, H, I]) Unpack() (A, B, C, D, E, F, G, H, I)

Unpack returns values contained in a tuple. Play: https://go.dev/play/p/Ow9Zgf_zeiA

Directories

Path Synopsis
internal
constraints
Package constraints defines a set of useful constraints to be used with type parameters.
Package constraints defines a set of useful constraints to be used with type parameters.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL