mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
fa095e4b4f
* fix(doc): fix go playground demo URL * fix(doc): add more go playground demo URL
1.5 KiB
1.5 KiB
name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | signatures | playUrl | variantHelpers | similarHelpers | position | |||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Nth | nth | it/find.go#L417 | it | find |
|
https://go.dev/play/p/FqgCobsKqva |
|
|
580 |
Returns the element at index nth of collection. Returns an error when nth is out of bounds.
Will iterate n times through the sequence.
Examples:
// Get element at specific index
numbers := it.Slice([]int{5, 2, 8, 1, 9})
element, err := it.Nth(numbers, 2)
// element: 8, err: nil
// Get first element (index 0)
first, err := it.Nth(numbers, 0)
// first: 5, err: nil
// Get last element
last, err := it.Nth(numbers, 4)
// last: 9, err: nil
// Out of bounds - negative
_, err := it.Nth(numbers, -1)
// err: nth: -1 out of bounds
// Out of bounds - too large
_, err := it.Nth(numbers, 10)
// err: nth: 10 out of bounds
// With strings
words := it.Slice([]string{"hello", "world", "go", "lang"})
element, err := it.Nth(words, 1)
// element: "world", err: nil
// With different integer types
numbers := it.Slice([]int{1, 2, 3, 4, 5})
element, err := it.Nth(numbers, int8(3))
// element: 4, err: nil
// With structs
type Person struct {
Name string
Age int
}
people := it.Slice([]Person{
{Name: "Alice", Age: 30},
{Name: "Bob", Age: 25},
{Name: "Charlie", Age: 35},
})
element, err := it.Nth(people, 1)
// element: {Name: "Bob", Age: 25}, err: nil