This package provides useful array operations functions such as can be found in the javascript and java languages.
The native array is encapsulated in a Array
struct for chaining methods and doing "functional" programming.
Go >= 1.18 version.
To install it, run:
go get github.com/drouian-m/array-utils
package main
import (
"github.com/drouian-m/array-utils"
)
func main() {
arr := array.NewArray([]int{1, 2, 3})
}
- Find : Find a value in the current array
- FindIndex : Find value index in the current array
- Filter : Filter array values (chainable)
- Map : Create a new array populated with the result of calling the input func on each array element (chainable)
- ForEach : Execute the input function on each array element
package main
import (
"github.com/drouian-m/array-utils"
)
func main() {
arr := array.NewArray([]int{1, 2, 3})
arr.ToNative() // => return []int array
}
package main
import (
"fmt"
"github.com/drouian-m/array-utils"
)
func main() {
myarr := array.NewArray([]int{1, 2, 3, 4, 5})
result := myarr.Map(func(val int) int {
return val * 3
}).Filter(func(val int) bool {
return val % 2 == 0
})
fmt.Println(result.ToNative()) // => [6 12]
}