array

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package array provides functions for manipulating golang slices.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func At

func At[T comparable](slice []T, index int) (T, error)

The At() function returns an indexed element from a slice and returns a possible error relating to out of range indexes.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	fruits := []string{"Banana", "Orange", "Apple", "Mango"}
	first, _ := array.At(fruits, 0)
	last, _ := array.At(fruits, -1)
	fmt.Println(first, last)
}
Output:
Banana Mango

func Concat

func Concat[T comparable](slice1 []T, slice2 ...[]T) []T

The concat() function concatenates (joins) two or more slices. The concat() function returns a new slice, containing the joined slices. The concat() function does not change the existing slices.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	a := []int{1, 2, 3}
	b := []int{4, 5}
	c := []int{6}
	fmt.Println(array.Concat(a, b, c))
}
Output:
[1 2 3 4 5 6]

func ConvertIndex

func ConvertIndex[T comparable](slice []T, index int, nameOfIndex string) (int, error)

func CopyWithin

func CopyWithin[T comparable](slice []T, target int, start int, end int) ([]T, error)

The CopyWithin() function returns a copy of slice elements to another position in an slice and a possible error relating to out of range indexes.. The CopyWithin() function overwrites the existing values in the new slice.

func Every

func Every[T comparable](slice []T, fn Predicate[T]) bool

The Every() function executes a function for each slice element. The Every() function returns true if the function returns true for all elements. The Every() function returns false if the function returns false for one element. The Every() function does not execute the function for empty elements. The Every() function does not change the original slice.

func Fill

func Fill[T comparable](slice []T, value T, start int, end int) ([]T, error)

The Fill() function returns fills of specified elements in an slice with a value and a possible error due to indexes out of range.

func Filter

func Filter[T comparable](slice []T, fn Predicate[T]) []T

The filter() function creates a new slice filled with elements that pass a test provided by a function. The filter() function does not execute the function for empty elements.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	nums := []int{1, 2, 3, 4, 5}
	even := array.Filter(nums, func(n, _ int, _ []int) bool { return n%2 == 0 })
	fmt.Println(even)
}
Output:
[2 4]

func Find

func Find[T comparable](slice []T, fn Predicate[T]) *T

The Find() function returns a pointer value of the first element that passes a test. The Find() function executes a function for each slice element. The Find() function returns undefined if no elements are found. The Find() function does not execute the function for empty elements. The Find() function does not change the original slice

func FindIndex

func FindIndex[T comparable](slice []T, fn Predicate[T]) int

The FindIndex() function executes a function for each slice element. The FindIndex() function returns the index (position) of the first element that passes a test. The FindIndex() function returns -1 if no match is found. The FindIndex() function does not execute the function for empty slice elements. The FindIndex() function does not change the original slice.

func FindLast

func FindLast[T comparable](slice []T, fn Predicate[T]) *T

The FindLast() function returns the value of the last element that passes a test. The FindLast() function executes a function for each slice element. The FindLast() function returns -1 if no elements are found. The FindLast() function does not execute the function for empty elements. The FindLast() function does not change the original slice.

func FindLastIndex

func FindLastIndex[T comparable](slice []T, fn Predicate[T]) int

func Flat

func Flat[T any](slice []any, depths ...int) ([]T, error)

The Flat() function concatenates sub-arrays elements into a single new slice. The Flat() function returns a new slice containing all elements from the sub-arrays. The Flat() function does not change the original slice. The depth parameter specifies how deep a nested array structure should be flattened. The default is 1. The Flat() function can handle slices of any type, but all elements must be of the same type T. If an element cannot be converted to type T, an error is returned. Example usage:

input := []any{1, []int{2, 3}, 4}
result, err := Flat[int](input)
if err != nil {
	fmt.Println("Error:", err)
} else {
	fmt.Println(result) // Output: [1 2 3 4]
}
Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	nested := []any{1, []int{2, 3}, []int{4, 5}}
	flat, _ := array.Flat[int](nested)
	fmt.Println(flat)
}
Output:
[1 2 3 4 5]

func ForEach

func ForEach[T comparable](slice []T, fn ForEachFunc[T])

func Includes

func Includes[T comparable](slice []T, element T, start *int) bool
Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	fmt.Println(array.Includes([]string{"a", "b", "c"}, "b", nil))
}
Output:
true

func IndexOf

func IndexOf[T comparable](slice []T, element T, start *int) int

The IndexOf() function returns the first index (position) of a specified value. The IndexOf() function returns -1 if the value is not found. The IndexOf() function starts at a specified index and searches from left to right (from the given start postion to the end of the array). By default the search starts at the first element and ends at the last. Negative start values counts from the last element (but still searches from left to right).

func Join

func Join[T any](slice []T, separator *string) string

The Join() function returns an slice as a string. The Join() function does not change the original slice. Any separator can be specified. The default is comma (,).

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	sep := " - "
	fmt.Println(array.Join([]int{1, 2, 3}, &sep))
}
Output:
1 - 2 - 3

func LastIndexOf

func LastIndexOf[T comparable](slice []T, element T, start *int) int

The IndexOf() function returns the first index (position) of a specified value and a possible error due to indexes out of range. The IndexOf() function returns -1 if the value is not found. The IndexOf() function starts at a specified index and searches from left to right (from the given start postion to the end of the array). By default the search starts at the first element and ends at the last. Negative start values counts from the last element (but still searches from left to right).

func Map

func Map[T comparable, V any](slice []T, fn MapFunc[T, V]) []V

Map() creates a new slice from calling a function for every slice element. Map creates a new slice of any type. Map() does not execute the function for empty elements. Map() does not change the original slice.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	nums := []int{1, 2, 3}
	doubled := array.Map(nums, func(n, _ int, _ []int) int { return n * 2 })
	fmt.Println(doubled)
}
Output:
[2 4 6]
Example (TypeChange)
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	nums := []int{1, 2, 3}
	labels := array.Map(nums, func(n, _ int, _ []int) string {
		return fmt.Sprintf("#%d", n)
	})
	fmt.Println(labels)
}
Output:
[#1 #2 #3]

func MapStrict

func MapStrict[T comparable](slice []T, fn MapFuncStrict[T]) []T

Map() creates a new slice from calling a function for every slice element. Map creates a new slice of the original slice type. Map() does not execute the function for empty elements. Map() does not change the original slice.

func OptionalParam

func OptionalParam[T any](params []T, defaultValue T) T

func Pop

func Pop[T comparable](slice []T) ([]T, *T)

The Pop() function removes (pops) the last element of an slice. The Pop() function does not change the original slice. The Pop() function returns the new slice without the last element and a pointer of removed element.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	rest, popped := array.Pop([]int{1, 2, 3})
	fmt.Println(rest, *popped)
}
Output:
[1 2] 3

func Push

func Push[T comparable](slice []T, elements ...T) []T

The Push() function adds new items to the end of an slice. The Push() function returns the new slice.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	fmt.Println(array.Push([]int{1, 2}, 3, 4))
}
Output:
[1 2 3 4]

func Reduce

func Reduce[T comparable](slice []T, fn ReduceFunc[T], initialValue any) (any, error)

The Reduce() function executes a Reducer function for slice element. The Reduce() function returns the function's accumulated result and an an error. The Reduce() function does not execute the function for empty slice elements. The Reduce() function does not change the original slice. Note at the first callback, there is no return value from the previous callback. Normally, alice element 0 is used as initial value, and the iteration starts from slice element 1. If an initial value is supplied, this is used, and the iteration starts from slice element 0.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	nums := []int{1, 2, 3, 4}
	sum, _ := array.Reduce(nums, func(acc any, n, _ int, _ []int) any {
		return acc.(int) + n
	}, 0)
	fmt.Println(sum)
}
Output:
10

func ReduceRight

func ReduceRight[T comparable](slice []T, fn ReduceFunc[T], initialValue any) (any, error)

The Reduce() function executes a Reducer function for slice element. The ReduceRight() function works from right to left. The Reduce() function returns the function's accumulated result and an an error. The Reduce() function does not execute the function for empty slice elements. The Reduce() function does not change the original slice. Note at the first callback, there is no return value from the previous callback. Normally, alice element 0 is used as initial value, and the iteration starts from slice element 1. If an initial value is supplied, this is used, and the iteration starts from slice element 0.

func ReduceRightStrict

func ReduceRightStrict[T comparable](slice []T, fn ReduceStrictFunc[T], initialValue *T) (T, error)

The ReduceStrict() function executes a Reducer function for slice element. The ReduceRightStrict() function works from right to left . The ReduceStrict() function returns the function's accumulated result(typed with the slices type) and an an error. The ReduceStrict() function does not execute the function for empty slice elements. The ReduceStrict() function does not change the original slice. Note at the first callback, there is no return value from the previous callback. Normally, alice element 0 is used as initial value, and the iteration starts from slice element 1. If an initial value is supplied, this is used, and the iteration starts from slice element 0.

func ReduceStrict

func ReduceStrict[T comparable](slice []T, fn ReduceStrictFunc[T], initialValue *T) (T, error)

The ReduceStrict() function executes a Reducer function for slice element. The ReduceStrict() function returns the function's accumulated result(typed with the slices type) and an an error. The ReduceStrict() function does not execute the function for empty slice elements. The ReduceStrict() function does not change the original slice. Note at the first callback, there is no return value from the previous callback. Normally, alice element 0 is used as initial value, and the iteration starts from slice element 1. If an initial value is supplied, this is used, and the iteration starts from slice element 0.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	nums := []int{1, 2, 3, 4}
	initial := 0
	sum, _ := array.ReduceStrict(nums, func(acc, n, _ int, _ []int) int {
		return acc + n
	}, &initial)
	fmt.Println(sum)
}
Output:
10

func Reverse

func Reverse[T comparable](slice []T) []T

The Reverse() function Reverses the order of the elements in an slice. The Reverse() function does not overwrites the original array.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	fmt.Println(array.Reverse([]int{1, 2, 3}))
}
Output:
[3 2 1]

func Shift

func Shift[T comparable](slice []T) ([]T, *T)

The Shift() function removes the first item of an slice. The Shift() function changes the original slice. The Shift() function returns the new slice and a pointer to the shifted element.

func Slice

func Slice[T comparable](slice []T, start, end int) ([]T, error)

The Slice() function returns selected elements in an slice, as a new slice and and error. The Slice() function selects from a given start, up to a (not inclusive) given end. The Slice() function does not change the original slice.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	out, _ := array.Slice([]int{10, 20, 30, 40, 50}, 1, 4)
	fmt.Println(out)
}
Output:
[20 30 40]

func Some

func Some[T comparable](slice []T, fn Predicate[T]) bool

The Some() function checks if any slice elements pass a test (provided as a callback function). The Some() function executes the callback function once for each slice element.The Some() function returns true (and stops) if the function returns true for one of the slice elements. The Some() function returns false if the function returns false for all of the slice elements. The Some() function does not execute the function for empty slice elements. The Some() function does not change the original slice.

func Splice

func Splice[T comparable](slice []T, index int, howMany *int, elements ...T) ([]T, error)

The Splice() function adds and/or removes slice elements. The Splice() function does not overwrites the original slice.

func ToString

func ToString[T any](slice []T) string

The ToString() function returns a string with slice values separated by commas. The ToString() function does not change the original slice.

func UnShift

func UnShift[T comparable](slice []T, element ...T) []T

The Unshift() function adds new elements to the beginning of an slice. The Unshift() function does not overwrite the original slice.

func ValueOf

func ValueOf[T comparable](slice []T) []T

The ValueOf() function returns the slice itself. The ValueOf() function does not change the original slice. fruits. ValueOf() returns the same as fruits.

func With

func With[T comparable](slice []T, index int, value T) ([]T, error)

The With() function updates a specified slice element. The With() function returns a new slice. The With() function does not change the original slice.

Types

type Array

type Array[T comparable] []T
Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	arr := array.Array[int]{1, 2, 3, 4, 5}
	fmt.Println(arr.Reverse())
}
Output:
[5 4 3 2 1]
Example (Chaining)

Demonstrates method chaining: Filter -> Push -> Reverse, all on Array[T].

package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	arr := array.Array[int]{1, 2, 3, 4, 5}
	result := arr.
		Filter(func(n, _ int, _ []int) bool { return n%2 == 0 }).
		Push(6).
		Reverse()
	fmt.Println(result)
}
Output:
[6 4 2]

func (Array[T]) At

func (a Array[T]) At(index int) (T, error)

The At() method returns an indexed element from the array and returns a possible error relating to out of range indexes.

func (Array[T]) Concat

func (a Array[T]) Concat(slices ...Array[T]) Array[T]

The Concat() method concatenates the array with other slices and returns a new concatenated array.

func (Array[T]) CopyWithin

func (a Array[T]) CopyWithin(target, start, end int) (Array[T], error)

The CopyWithin() method copies array elements to another position in the array and returns the modified array.

func (Array[T]) Entries

func (a Array[T]) Entries() []Entry[T]

The Entries() method returns an array of [index, element] pairs for each element in the array.

func (Array[T]) Every

func (a Array[T]) Every(fn Predicate[T]) bool

The Every() method tests whether all elements in the array pass the provided predicate function.

func (Array[T]) Fill

func (a Array[T]) Fill(value T, start, end int) (Array[T], error)

The Fill() method fills array elements from a start index to an end index with a static value.

func (Array[T]) Filter

func (a Array[T]) Filter(fn Predicate[T]) Array[T]

The Filter() method creates a new array with elements that pass the provided predicate function.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	arr := array.Array[int]{1, 2, 3, 4, 5}
	even := arr.Filter(func(n, _ int, _ []int) bool { return n%2 == 0 })
	fmt.Println(even)
}
Output:
[2 4]

func (Array[T]) Find

func (a Array[T]) Find(fn Predicate[T]) *T

The Find() method returns the first element that passes the provided predicate function.

func (Array[T]) FindIndex

func (a Array[T]) FindIndex(fn Predicate[T]) int

The FindIndex() method returns the index of the first element that passes the provided predicate function.

func (Array[T]) FindLast

func (a Array[T]) FindLast(fn Predicate[T]) *T

The FindLast() method returns the last element that passes the provided predicate function.

func (Array[T]) FindLastIndex

func (a Array[T]) FindLastIndex(fn Predicate[T]) int

The FindLastIndex() method returns the index of the last element that passes the provided predicate function.

func (Array[T]) Flat

func (a Array[T]) Flat(depths ...int) (Array[any], error)

The Flat() method flattens nested arrays up to the specified depth. The Flat() method returns a new array with the sub-array elements concatenated into it. The depth parameter specifies how deep a nested array structure should be flattened. The default is 1. Until Generic Methods are implemented in Go, the Flat() method can only return Array[any] since it needs to handle slices of any type. Once Generic Methods are available, we can update the Flat() method to return Array[T] and handle type assertions accordingly.

func (Array[T]) ForEach

func (a Array[T]) ForEach(fn ForEachFunc[T])

The ForEach() method executes a provided function once for each array element.

func (Array[T]) Includes

func (a Array[T]) Includes(element T, start *int) bool

The Includes() method determines whether the array includes a certain element.

func (Array[T]) IndexOf

func (a Array[T]) IndexOf(element T, start *int) int

The IndexOf() method returns the first index at which a given element can be found in the array.

func (Array[T]) Join

func (a Array[T]) Join(separator ...string) string

The Join() method joins all elements of the array into a single string using the provided separator. Elements are converted to their string form, so this works for any Array[T]. If no separator is provided, a comma is used, matching JavaScript Array.join().

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	arr := array.Array[int]{1, 2, 3}
	fmt.Println(arr.Join(" | "))
}
Output:
1 | 2 | 3

func (Array[T]) LastIndexOf

func (a Array[T]) LastIndexOf(element T, start *int) int

The LastIndexOf() method returns the last index at which a given element can be found in the array.

func (Array[T]) Map

func (a Array[T]) Map(fn MapFunc[T, any]) Array[any]

The Map() method creates a new array with the results of calling a function for every array element.

func (Array[T]) MapStrict

func (a Array[T]) MapStrict(fn MapFuncStrict[T]) Array[T]

The MapStrict() method creates a new array of the same type with the results of calling a function for every array element.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	arr := array.Array[int]{1, 2, 3}
	doubled := arr.MapStrict(func(n, _ int, _ []int) int { return n * 2 })
	fmt.Println(doubled)
}
Output:
[2 4 6]

func (Array[T]) Pop

func (a Array[T]) Pop() (Array[T], *T)

The Pop() method removes the last element from the array and returns the modified array and the removed element.

func (Array[T]) Push

func (a Array[T]) Push(elements ...T) Array[T]

The Push() method adds one or more elements to the end of the array and returns the new array.

func (Array[T]) Reduce

func (a Array[T]) Reduce(fn ReduceFunc[T], initialValue any) (any, error)

The Reduce() method executes a reducer function for each array element and returns a single value.

func (Array[T]) ReduceRight

func (a Array[T]) ReduceRight(fn ReduceFunc[T], initialValue any) (any, error)

The ReduceRight() method executes a reducer function for each array element (from right to left) and returns a single value.

func (Array[T]) ReduceRightStrict

func (a Array[T]) ReduceRightStrict(fn ReduceStrictFunc[T], initialValue *T) (T, error)

The ReduceRightStrict() method executes a reducer function for each array element (from right to left) and returns a value of the same type.

func (Array[T]) ReduceStrict

func (a Array[T]) ReduceStrict(fn ReduceStrictFunc[T], initialValue *T) (T, error)

The ReduceStrict() method executes a reducer function for each array element and returns a value of the same type.

func (Array[T]) Reverse

func (a Array[T]) Reverse() Array[T]

The Reverse() method reverses the array and returns a new reversed array.

func (Array[T]) Shift

func (a Array[T]) Shift() (Array[T], *T)

The Shift() method removes the first element from the array and returns the modified array and the removed element.

func (Array[T]) Slice

func (a Array[T]) Slice(start, end int) (Array[T], error)

The Slice() method returns a shallow copy of a portion of the array.

func (Array[T]) Some

func (a Array[T]) Some(fn Predicate[T]) bool

The Some() method tests whether at least one element in the array passes the provided predicate function.

func (Array[T]) Splice

func (a Array[T]) Splice(index int, howMany *int, items ...T) (Array[T], error)

The Splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements.

func (Array[T]) ToString

func (a Array[T]) ToString() string

The ToString() method returns a string representation of the array. It mirrors JavaScript Array.toString() by joining elements with commas.

Example
package main

import (
	"fmt"

	array "github.com/bube054/go-js-array-methods/v2/array"
)

func main() {
	arr := array.Array[string]{"a", "b", "c"}
	fmt.Println(arr.ToString())
}
Output:
a,b,c

func (Array[T]) UnShift

func (a Array[T]) UnShift(elements ...T) Array[T]

The UnShift() method adds one or more elements to the beginning of the array and returns the new array.

func (Array[T]) ValueOf

func (a Array[T]) ValueOf() Array[T]

The ValueOf() method returns the array itself (identity function).

func (Array[T]) With

func (a Array[T]) With(index int, value T) (Array[T], error)

The With() method returns a new array with the element at the given index replaced with the given value.

type Entry

type Entry[T comparable] struct {
	// contains filtered or unexported fields
}

func Entries

func Entries[T comparable](slice []T) []Entry[T]

The entries() function returns an Slice with structs with field/value: {index 0, element "Banana"}. The entries() function does not change the original slice.

type ForEachFunc

type ForEachFunc[T comparable] func(element T, index int, slice []T)

type MapFunc

type MapFunc[T comparable, V any] func(element T, index int, slice []T) V

type MapFuncStrict

type MapFuncStrict[T comparable] func(element T, index int, slice []T) T

type Predicate

type Predicate[T comparable] func(element T, index int, slice []T) bool

type ReduceFunc

type ReduceFunc[T comparable] func(total any, currentValue T, currentIndex int, slice []T) any

type ReduceStrictFunc

type ReduceStrictFunc[T comparable] func(total T, currentValue T, currentIndex int, slice []T) T

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL