gubrak

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2018 License: MIT Imports: 8 Imported by: 44

README

GUBRAK

Golang utility library with syntactic sugar. It's like lodash, but for golang.

Go Report Card Build Status Coverage Status

Gubrak is yet another utility library for Golang, inspired from lodash. Currently we have around 73 reusable functions available, we'll definitely adding more!

Installation

go get -u github.com/novalagung/gubrak

Documentation

Hello World Example

package main

import (
    "github.com/novalagung/gubrak"
    "fmt"
)

type Sample struct {
    EbookName      string
    DailyDownloads int
}

func main() {
    data := []Sample{
        { EbookName: "clean code", DailyDownloads: 10000 },
        { EbookName: "rework", DailyDownloads: 12000 },
        { EbookName: "detective comics", DailyDownloads: 11500 },
    }

    result, err := gubrak.Filter(data, func(each Sample) bool {
        return each.DailyDownloads > 11000
    })

    if err != nil {
        fmt.Println("Error!", err.Error)
        return
    }

    fmt.Printf("%#v \n", result.([]Sample))

    /*
    []Sample{
        { EbookName: "rework", DailyDownloads: 12000 },
        { EbookName: "detective comics", DailyDownloads: 11500 },
    }
    */
}

APIs

Below are the list of available functions on gubrak:

ChunkCompactConcatCountDifferenceDropDropRightEachEachRightFillFilterFindFindIndexFindLastFindLastIndexFirstForEachForEachRightFromPairsGroupByHeadIncludesIndexOfInitialIntersectionIsArrayIsBoolIsChannelIsDateIsEmptyIsEmptyStringIsFloatIsFunctionIsIntIsMapIsNilIsNumericIsPointerIsSliceIsStringIsStructObjectIsTrueIsUintIsZeroNumberJoinKeyByLastLastIndexOfMapNowNthOrderByPartitionPullPullAllPullAtRandomIntRandomStringReduceRejectRemoveReverseSampleSampleSizeShuffleSizeSortByTailTakeTakeRightUnionUniqWithout

Test

go get -u github.com/novalagung/gubrak
dep ensure
go test -cover -race -v ./... 

Contribution

Fork ➜ Create branch ➜ Commit ➜ Push ➜ Pull Requests

License

MIT License

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk(data interface{}, size int) (interface{}, error)

Chunk function creates a slice of elements split into groups the length of `size`. If `data` can't be split evenly, the final chunk will be the remaining elements.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

3 examples available:

Example (Chunk1)
data := []int{1, 2, 3, 4, 5}
size := 2

result, err := Chunk(data, size)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> [][]int{ { 1, 2 }, { 3, 4 }, { 5 } }
Output:

Example (Chunk2)
data := []string{"a", "b", "c", "d", "e"}
size := 3

result, err := Chunk(data, size)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> [][]string{ { "a", "b", "c" }, { "d", "e" } }
Output:

Example (Chunk3)
data := []interface{}{
	3.2, "a", -1,
	make([]byte, 0),
	map[string]int{"b": 2},
	[]string{"a", "b", "c"},
}
size := 3

result, err := Chunk(data, size)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  [][]interface{}{
		{ 3.2, "a" },
		{ -1, []uint8{} },
		{ map[string]int{ "b":2 }, []string{ "a", "b", "c" } },
	  }
*/ }
*/
Output:

func Compact

func Compact(data interface{}) (interface{}, error)

Compact function creates a slice with all falsey values removed from the `data`. These values: `false`, `nil`, `0`, `""`, `(*string)(nil)`, and other nil-able types are considered to be falsey.

Parameters

This function requires one mandatory parameter:

data // type: slice, description: the slice to compact

Return values

This function return two values:

slice // description: returns the new slice of filtered values
error // description: hold error message if there is an error

Examples

4 examples available:

Example (Compact1)
data := []int{-2, -1, 0, 1, 2}

result, err := Compact(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ -2, -1, 1, 2 }
Output:

Example (Compact2)
data := []string{"a", "b", "", "d"}

result, err := Compact(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "a", "b", "d" }
Output:

Example (Compact3)
data := []interface{}{-2, 0, 1, 2, false, true, "", "hello", nil}

result, err := Compact(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []interface{}{ -2, 1, 2, true, "hello" }
Output:

Example (Compact4)
item1, item2, item3 := "a", "b", "c"
data := []*string{&item1, nil, &item2, nil, &item3}

result, err := Compact(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []*string{ (*string)(0xc42000e1e0), (*string)(0xc42000e1f0), (*string)(0xc42000e200) }
Output:

func Concat

func Concat(data interface{}, dataConcats ...interface{}) (interface{}, error)

Concat function creates a new slice concatenating `data` with any additional slices (the 2nd parameter and rest).

Parameters

This function requires one mandatory parameter `data`, and unlimited variadic parameters:

data        // type: slice, description: the slice to concatenate
dataConcat1 // type: slice, description: the values to concatenate
dataConcat2 // type: slice, description: the values to concatenate
dataConcat3 // type: slice, description: the values to concatenate
...

Return values

This function return two values:

slice // description: returns the new concatenated slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Concat1)
data := []int{1, 2, 3, 4}
dataConcat1 := []int{4, 6, 7}
dataConcat2 := []int{8, 9}

result, err := Concat(data, dataConcat1, dataConcat2)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }
Output:

Example (Concat2)
data := []string{"my"}
dataConcat1 := []string{"name", "is"}
dataConcat2 := []string{"jason", "todd"}

result, err := Concat(data, dataConcat1, dataConcat2)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "my", "name", "is", "jason", "todd" }
Output:

func Count

func Count(data interface{}, args ...interface{}) (int, error)

Count creates an object composed of keys generated from the results of running each element of `data` thru `iteratee`. The corresponding value of each key is the number of times the key was returned by `iteratee`.

Parameters

This function requires two mandatory parameters:

data     // type: slice or map, description: the slice/map to iterate over
iteratee // optional, type: func(each anyType, i int)bool or func(value anyType, key anyType, i int), description: the function invoked per iteration.

Return values

This function return two values:

number // description: Returns the composed aggregate object
error // description: hold error message if there is an error

Examples

N examples available:

Example (CountMap1)
data := map[string]interface{}{
	"name":   "jason",
	"age":    12,
	"isMale": true,
}
result, err := Count(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 3
Output:

Example (CountMap2)
data := map[string]interface{}{
	"name":   "jason",
	"age":    12,
	"isMale": true,
}

result, err := Count(data, func(val interface{}, key string) bool {
	return strings.Contains(strings.ToLower(key), "m")
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 2
Output:

Example (CountMap3)
data := map[string]interface{}{
	"name":   "jason",
	"age":    12,
	"isMale": true,
}

result, err := Count(data, func(val interface{}, key string, i int) bool {
	return strings.Contains(strings.ToLower(key), "m") && i > 1
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 1
Output:

Example (CountSlice1)
data := []string{"damian", "grayson", "cassandra"}
result, err := Count(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 3
Output:

Example (CountSlice2)
data := []string{"damian", "grayson", "cassandra"}

result, err := Count(data, func(each string) bool {
	return strings.Contains(each, "d")
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 2
Output:

Example (CountSlice3)
data := []string{"damian", "grayson", "cassandra"}

result, err := Count(data, func(each string, i int) bool {
	return len(each) > 6 && i > 1
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 1
Output:

func Difference

func Difference(data interface{}, compareData ...interface{}) (interface{}, error)

Difference function creates a slice of `data` values not included in the other given slices. The order and references of result values are determined by the first slice.

Parameters

This function requires one mandatory parameter `data`, and unlimited variadic parameters:

data   // type: slice, description: the slice to inspect
slice1 // optional, type: slice, description: the values to exclude
slice2 // optional, type: slice, description: the values to exclude
slice3 // optional, type: slice, description: the values to exclude
...

Return values

This function return two values:

slice // description: returns the new slice of filtered values
error // description: hold error message if there is an error

Examples

3 examples available:

Example (Difference1)
data := []int{1, 2, 3, 4, 4, 6, 7}
dataDiff := []int{2, 7}

result, err := Difference(data, dataDiff)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 1, 3, 4, 4, 6 }
Output:

Example (Difference2)
data := []string{"a", "b", "b", "c", "d", "e", "f", "g", "h"}
dataDiff1 := []string{"b", "d"}
dataDiff2 := []string{"e", "f", "h"}

result, err := Difference(data, dataDiff1, dataDiff2)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "a", "c", "g" }
Output:

Example (Difference3)
data := []float64{1.1, 1.11, 1.2, 2.3, 3.0, 3, 4.0, 4.00000, 4.000000001}
dataDiff1 := []float64{1.1, 3}
dataDiff2 := []float64{4.000000001}

result, err := Difference(data, dataDiff1, dataDiff2)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{ 1.11, 1.2, 2.3, 4, 4 }
Output:

func Drop

func Drop(data interface{}, size int) (interface{}, error)

Drop function creates a slice of `data` with `n` elements dropped from the beginning.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to query
n    // type: number, description: the number of elements to drop

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Drop1)
data := []int{1, 2, 3, 4, 4, 5, 6}
n := 1

result, err := Drop(data, n)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 2, 3, 4, 4, 5, 6 }
Output:

Example (Drop2)
data := []string{"a", "b", "c", "d", "e", "f"}
n := 3

result, err := Drop(data, n)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "d", "e", "f" }
Output:

func DropRight

func DropRight(data interface{}, size int) (interface{}, error)

DropRight function creates a slice of `data` with `n` elements dropped from the end.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to query
n    // type: number, description: the number of elements to drop

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (DropRight1)
data := []int{1, 2, 3, 4, 4, 5, 6}
n := 1

result, err := DropRight(data, n)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 1, 2, 3, 4, 4, 5 }
Output:

Example (DropRight2)
data := []string{"a", "b", "c", "d", "e", "f"}
n := 3

result, err := DropRight(data, n)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "a", "b", "c" }
Output:

func Each

func Each(data, callback interface{}) error

Each iterates over elements of `data` and invokes `iteratee` for each element. Iteratee functions may exit iteration early by explicitly returning false

Parameters

This function requires two mandatory parameters:

data     // type: slice or map, description: the slice/map to iterate over
iteratee // optional, type: FuncSliceLoopOutputBool, description: the function invoked per iteration. The second argument represents index of each element, and it's optional

Return values

This function return two values:

error // description: hold error message if there is an error

Examples

N examples available:

Example (EachMap1)
data := map[string]interface{}{
	"name":   "damian",
	"age":    17,
	"gender": "male",
}

err := Each(data, func(value interface{}, key string) {
	fmt.Printf("%s: %v \n", key, value)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachMap2)
data := map[string]interface{}{
	"name":   "damian",
	"age":    17,
	"gender": "male",
}

err := Each(data, func(value interface{}, key string, i int) {
	fmt.Printf("key: %s, value: %v, index: %d \n", key, value, i)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachSlice1)
data := []string{"damian", "grayson", "cassandra"}

err := Each(data, func(each string) {
	fmt.Println(each)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachSlice2)
data := []string{"damian", "grayson", "cassandra"}

err := Each(data, func(each string, i int) {
	fmt.Printf("element %d: %s \n", i, each)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachSlice3)
type Sample struct {
	Name string
	Age  int
}

data := []Sample{
	{Name: "damian", Age: 12},
	{Name: "grayson", Age: 10},
	{Name: "cassandra", Age: 11},
}

err := Each(data, func(each Sample) {
	fmt.Printf("name: %s, age: %d \n", each.Name, each.Age)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachSlice4)
data := []string{"damian", "grayson", "cassandra", "tim", "jason", "stephanie"}

err := Each(data, func(each string, i int) bool {
	if i > 3 { // will stop after fourth loop
		return false
	}

	fmt.Println(each)
	return true
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

func EachRight

func EachRight(data, callback interface{}) error

EachRight function is like ForEach() except that it iterates over elements of collection from right to left.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (EachRightMap1)
data := map[string]interface{}{
	"name":   "damian",
	"age":    17,
	"gender": "male",
}

err := EachRight(data, func(value interface{}, key string) {
	fmt.Printf("%s: %v \n", key, value)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachRightMap2)
data := map[string]interface{}{
	"name":   "damian",
	"age":    17,
	"gender": "male",
}

err := EachRight(data, func(value interface{}, key string, i int) {
	fmt.Printf("key: %s, value: %v, index: %d \n", key, value, i)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachRightSlice1)
data := []string{"damian", "grayson", "cassandra"}

err := EachRight(data, func(each string) {
	fmt.Println(each)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachRightSlice2)
data := []string{"damian", "grayson", "cassandra"}

err := EachRight(data, func(each string, i int) {
	fmt.Printf("element %d: %s \n", i, each)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachRightSlice3)
type Sample struct {
	Name string
	Age  int
}

data := []Sample{
	{Name: "damian", Age: 12},
	{Name: "grayson", Age: 10},
	{Name: "cassandra", Age: 11},
}

err := EachRight(data, func(each Sample) {
	fmt.Printf("name: %s, age: %d \n", each.Name, each.Age)
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

Example (EachRightSlice4)
data := []string{"damian", "grayson", "cassandra", "tim", "jason", "stephanie"}

err := EachRight(data, func(each string, i int) bool {
	if i > 3 { // will stop after fourth loop
		return false
	}

	fmt.Println(each)
	return true
})
if err != nil {
	log.Fatal(err.Error())
}
Output:

func Fill

func Fill(data, value interface{}, args ...int) (interface{}, error)

Fill function fills elements of `data` with `value` from `start` up to, but not including, `end`.

Parameters

This function requires two mandatory parameters `data` and `value`; and two optional parameters:

data          // type: slice, description: the slice to fill
value         // type: anyType, description: the value to fill slice with. This variable's data type must be same with slice's element data type
start=0       // optional, type: number, description: the start position
end=len(data) // optional, type: number, description: the end position

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

3 examples available:

Example (Fill1)
data := []int{1, 2, 3, 4, 4, 5, 6}
replacement := 9

result, err := Fill(data, replacement)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 9, 9, 9, 9, 9, 9, 9 }
Output:

Example (Fill2)
data := []string{"grayson", "jason", "tim", "damian"}
replacement := "alfred"
start := 2

result, err := Fill(data, replacement, start)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ "grayson", "jason", "alfred", "alfred" }
Output:

Example (Fill3)
data := []float64{1, 2.2, 3.0002, 4, 4, 5.12, 6}
replacement := float64(9)
start, end := 3, 5

result, err := Fill(data, replacement, start, end)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{ 1, 2.2, 3.0002, 9, 9, 5.12, 6 }
Output:

func Filter

func Filter(data, callback interface{}) (interface{}, error)

Filter function iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with two arguments: (value, index).

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (FilterMap)
data := map[string]int{
	"clean code":       10000,
	"rework":           12000,
	"detective comics": 11500,
}

result, err := Filter(data, func(value int, key string) bool {
	return value > 11000
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  map[string]int{
		"rework":           12000,
		"detective comics": 11500,
	  }
*/}
*/
Output:

Example (FilterSlice)
type Sample struct {
	EbookName      string
	DailyDownloads int
}

data := []Sample{
	{EbookName: "clean code", DailyDownloads: 10000},
	{EbookName: "rework", DailyDownloads: 12000},
	{EbookName: "detective comics", DailyDownloads: 11500},
}

result, err := Filter(data, func(each Sample) bool {
	return each.DailyDownloads > 11000
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  []Sample{
		{ EbookName: "rework", DailyDownloads: 12000 },
		{ EbookName: "detective comics", DailyDownloads: 11500 },
	  }
*/}
*/
Output:

func Find

func Find(data, callback interface{}, args ...int) (interface{}, error)

Find function iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (value, index).

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (Find1)
type Sample struct {
	EbookName      string
	DailyDownloads int
}

data := []Sample{
	{EbookName: "clean code", DailyDownloads: 10000},
	{EbookName: "rework", DailyDownloads: 12000},
	{EbookName: "detective comics", DailyDownloads: 11500},
}

result, err := Find(data, func(each Sample) bool {
	return each.EbookName == "rework"
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> Sample { EbookName: "rework", DailyDownloads: 12000 }
Output:

Example (Find2)
data := []string{"clean code", "rework", "detective comics"}

result, err := Find(data, func(each string, i int) bool {
	return strings.Contains(each, "co")
}, 1)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> "detective comics"
Output:

func FindIndex

func FindIndex(data, predicate interface{}, args ...int) (int, error)

FindIndex function is similar like `Find`, except that it returns the index of the first element `predicate` returns truthy for, instead of the element itself.

Parameters

This function requires two mandatory parameters `data` and `predicate`; and two optional parameters:

data        // type: slice, description: the slice to inspect
predicate   // type: func(each anyType, i int)bool, description: the function invoked per iteration.
fromIndex=0 // optional, type: number, description: the index to search from

Return values

This function return two values:

number // description: returns the index of the found element, else `-1`
error  // description: hold error message if there is an error

Examples

5 examples available:

Example (FindIndex1)
data := []string{"damian", "grayson", "cass", "tim", "tim", "jason", "steph"}
predicate := func(each string) bool {
	return each == "tim"
}

result, err := FindIndex(data, predicate)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 3
Output:

Example (FindIndex2)
data := []int{-2, -1, 0, 1, 2}

result, err := FindIndex(data, func(each int) bool {
	return each == 4
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> -1
Output:

Example (FindIndex3)
data := []float64{1, 1.1, 1.2, 1.200001, 1.2000000001, 1.3}

result, err := FindIndex(data, func(each float64) bool {
	return each == 1.2000000001
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 4
Output:

Example (FindIndex4)
data := []int{1, 2, 3, 3, 4, 5}
predicate := func(each int) bool {
	return each == 3
}
fromIndex := 2

result, err := FindIndex(data, predicate, fromIndex)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 2
Output:

Example (FindIndex5)
data := []int{1, 2, 3, 3, 4, 5}
predicate := func(each int) bool {
	return each == 3
}
fromIndex := 3

result, err := FindIndex(data, predicate, fromIndex)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 3
Output:

func FindLast

func FindLast(data, callback interface{}, args ...int) (interface{}, error)

FindLast function is like Find() except that it iterates over elements of collection from right to left.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (FindLast1)
type Sample struct {
	EbookName      string
	DailyDownloads int
}

data := []Sample{
	{EbookName: "clean code", DailyDownloads: 10000},
	{EbookName: "rework", DailyDownloads: 12000},
	{EbookName: "detective comics", DailyDownloads: 11500},
}

result, err := FindLast(data, func(each Sample) bool {
	return strings.Contains(each.EbookName, "co")
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> Sample { EbookName: "detective comics", DailyDownloads: 11500 }
Output:

Example (FindLast2)
data := []string{"clean code", "rework", "detective comics", "coco"}

result, err := FindLast(data, func(each string, i int) bool {
	return strings.Contains(each, "co")
}, 2)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> "detective comics"
Output:

Example (FindLast3)
data := []string{"clean code", "rework", "detective comics", "coco"}

result, err := FindLast(data, func(each string, i int) bool {
	return strings.Contains(each, "co")
}, 3)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> "coco"
Output:

func FindLastIndex

func FindLastIndex(data, predicate interface{}, args ...int) (int, error)

FindLastIndex function is similar like `FindIndex`, except that it iterates over elements of `data` from right to left.

Parameters

This function requires two mandatory parameters `data` and `predicate`; and an optional parameter:

data                  // type: slice, description: the slice to inspect
predicate             // type: func(each anyType, i int)bool, description: the function invoked per iteration.it's optional
fromIndex=len(data)-1 // optional, type: number, description: the index to search from

Return values

This function return two values:

number // description: returns the index of the found element, else `-1`
error  // description: hold error message if there is an error

Examples

4 examples available:

Example (FindLastIndex1)
data := []string{"damian", "grayson", "cass", "tim", "tim", "jason", "steph"}

result, err := FindLastIndex(data, func(each string) bool {
	return each == "tim"
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 4
Output:

Example (FindLastIndex2)
data := []int{1, 2, 2, 3, 3, 4, 5}
predicate := func(each int) bool {
	return each == 3
}
fromIndex := 4

result, err := FindLastIndex(data, predicate, fromIndex)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 4
Output:

Example (FindLastIndex3)
data := []int{1, 2, 3, 3, 4, 5}
predicate := func(each int) bool {
	return each == 3
}
fromIndex := 3

result, err := FindLastIndex(data, predicate, fromIndex)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 3
Output:

Example (FindLastIndex4)
data := []int{1, 2, 3, 3, 4, 5}
predicate := func(each int) bool {
	return each == 3
}
fromIndex := 2

result, err := FindLastIndex(data, predicate, fromIndex)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> -1
Output:

func First

func First(data interface{}) (interface{}, error)

First function gets the first element of `data`.

Parameters

This function requires one mandatory parameter `data`:

data // type: slice, description: the slice to query

Return values

This function return two values:

anyType // description: returns the first element of data
error   // description: hold error message if there is an error

Examples

2 examples available:

Example (First1)
data := []string{"damian", "grayson", "cassandra"}
result, err := First(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> "damian"
Output:

Example (First2)
data := []string{}
result, err := First(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> nil
Output:

func ForEach

func ForEach(data, callback interface{}) error

ForEach is alias of Each()

func ForEachRight

func ForEachRight(data, callback interface{}) error

ForEachRight is alias of EachRight()

func FromPairs

func FromPairs(data interface{}) (interface{}, error)

FromPairs function returns an object composed from key-value `data`.

Parameters

This function requires one mandatory parameter `data`:

data // type: [][]interface{}, description: the key-value pairs

Return values

This function return two values:

map[interface{}]interface{} // description: returns the new object
error                       // description: hold error message if there is an error

Examples

2 examples available:

Example (FromPairs1)
data := []interface{}{
	[]interface{}{"a", 1},
	[]interface{}{"b", 2},
}

result, err := FromPairs(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  map[interface{}]interface{}{
		"a": 1,
		"b": 2,
	  }
*/}
*/
Output:

Example (FromPairs2)
data := []interface{}{
	[]interface{}{true, []int{1, 2, 3}},
	[]interface{}{false, []string{"damian", "grayson"}},
}

result, err := FromPairs(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	map[interface{}]interface{}{
	  true: []int{ 1, 2, 3 },
	  false: []string{ "damian", "grayson" },
	}
*/}
*/
Output:

func GroupBy

func GroupBy(data, callback interface{}) (interface{}, error)

GroupBy function creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. The iteratee is invoked with one argument: (value).

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (GroupBy1)
type Sample struct {
	Ebook    string
	Category string
}

data := []Sample{
	{Ebook: "clean code", Category: "productivity"},
	{Ebook: "rework", Category: "productivity"},
	{Ebook: "detective comics", Category: "comics"},
	{Ebook: "injustice 2", Category: "comics"},
	{Ebook: "dragon ball", Category: "manga"},
	{Ebook: "one piece", Category: "manga"},
}

result, err := GroupBy(data, func(each Sample) string {
	return each.Category
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  map[string][]main.Sample {
		"productivity": []main.Sample {
		  { Ebook: "clean code", Category: "productivity" },
		  { Ebook: "rework", Category: "productivity" },
		},
		"comics":       []main.Sample {
		  { Ebook: "detective comics", Category: "comics"},
		  { Ebook: "injustice 2", Category: "comics"},
		},
		"manga":        []main.Sample {
		  { Ebook: "dragon ball", Category: "manga" },
		  { Ebook: "one piece", Category: "manga"},
		},
	  }
*/			},
	  }
*/
Output:

Example (GroupBy2)
data := []int{1, 2, 3, 5, 6, 4, 2, 5, 2}

result, err := GroupBy(data, func(each int) int {
	return each
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
   map[int][]int{
     5: []int{ 5, 5 },
     6: []int{ 6 },
     4: []int{ 4 },
     1: []int{ 1 },
     2: []int{ 2, 2, 2 },
     3: []int{ 3 },
   }
*/	   }
*/
Output:

func Head(data interface{}) (interface{}, error)

Head function is an alias of `First`.

func Includes

func Includes(data, search interface{}, args ...int) (bool, error)

Includes function checks if value is in collection. If collection is a string, it's checked for a substring of value, otherwise SameValueZero is used for equality comparisons. If fromIndex is negative, it's used as the offset from the end of collection.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (IncludesMap1)
data := map[string]string{
	"name":  "grayson",
	"hobby": "helping people",
}

result, err := Includes(data, "grayson")
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> true
Output:

Example (IncludesMap2)
data := map[string]string{
	"name":  "grayson",
	"hobby": "helping people",
}

result, err := Includes(data, "batmobile")
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> false
Output:

Example (IncludesSlice1)
data := []string{"damian", "tim", "jason", "grayson"}
result, err := Includes(data, "tim")
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> true
Output:

Example (IncludesSlice2)
data := []string{"damian", "tim", "jason", "grayson"}
result, err := Includes(data, "tim", 2)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> false
Output:

Example (IncludesSlice3)
data := []string{"damian", "tim", "jason", "grayson"}
result, err := Includes(data, "cassandra")
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> false
Output:

Example (IncludesSlice4)
data := []interface{}{"name", 12, true}

Includes(data, "name") // ===> true
Includes(data, 12)     // ===> true
Includes(data, true)   // ===> true
Output:

Example (IncludesSlice5)
Includes("damian", "an") // ===> true
Output:

func IndexOf

func IndexOf(data interface{}, search interface{}, args ...int) (int, error)

IndexOf function gets the index at which the first occurrence of `search` is found in `data`. If `fromIndex` is negative, it's used as the offset from the end of `data`.

Parameters

This function requires two mandatory parameters `data` and `value`; and one optional parameter:

data        // type: slice, description: the slice to inspect
value       // type: anyType, description: the value to search for
fromIndex=0 // optional, type: number, description: the index to search from

Return values

This function return two values:

number // description: returns the index of the matched value, else -1
error  // description: hold error message if there is an error

Examples

3 examples available:

Example (IndexOf1)
data := []string{"damian", "grayson", "cass", "tim", "tim", "jason", "steph"}
IndexOf(data, "duke")    // ===> -1
IndexOf(data, "tim")     // ===> 3
IndexOf(data, "tim", 4)  // ===> 4
IndexOf(data, "tim", -4) // ===> 3
IndexOf(data, "tim", -3) // ===> 4
IndexOf(data, "tim", -2) // ===> -1
Output:

Example (IndexOf2)
data := []float64{2.1, 2.2, 3, 3.00000, 3.1, 3.9, 3.95}
IndexOf(data, 2.2)           // ===> 1
IndexOf(data, 3)             // ===> -1
IndexOf(data, float64(3))    // ===> 2 (because 3 is detected as int32, not float64)
IndexOf(data, float64(3), 2) // ===> 2
IndexOf(data, float64(3), 3) // ===> 3
Output:

Example (IndexOf3)
data := []interface{}{"jason", 24, true}
IndexOf(data, 24)     // ===> 1
IndexOf(data, 24, -1) // ===> -1
Output:

func Initial

func Initial(data interface{}) (interface{}, error)

Initial function gets all but the last element of `data`.

Parameters

This function requires one mandatory parameter:

data // type: slice, description: the slice to query

Return values

This function return two values:

slice // description: returns the slice of `data`
error // description: hold error message if there is an error

Examples

4 examples available:

Example (Initial1)
data := []string{"damian", "grayson", "cassandra"}
result, err := Initial(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "damian", "grayson" }
Output:

Example (Initial2)
data := []int{1, 2, 3, 4, 5}
result, err := Initial(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 1, 2, 3, 4 }
Output:

Example (Initial3)
data := []map[string]string{{"name": "jason"}}
result, err := Initial(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []map[string]string{}
Output:

Example (Initial4)
data := []float64{}
result, err := Initial(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{}
Output:

func Intersection

func Intersection(data interface{}, dataIntersects ...interface{}) (interface{}, error)

Intersection function creates a slice of unique values that are included in all given slices. The order and references of result values are determined by the first slice.

Parameters

This function requires one mandatory parameter `data`; and unlimited variadic parameters:

data           // type: slice, the slice to inspect
dataIntersect1 // optional, type: slice, the values to compare
dataIntersect2 // optional, type: slice, the values to compare
dataIntersect3 // optional, type: slice, the values to compare

...

Return values

This function return two values:

slice // description: returns the new slice of intersecting values
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Intersection1)
result, err := Intersection(
	[]string{"damian", "grayson", "cassandra", "tim", "tim", "jason"},
	[]string{"cassandra", "tim", "jason"},
	[]string{"cassandra", "jason"},
)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "cassandra", "jason" }
Output:

Example (Intersection2)
result, err := Intersection(
	[]float64{0.8, 0.8001, 0.999, 1, 1.0, 1.000001, 1.1000000, 1.1001, 1.2, 1.33, 1.4},
	[]float64{0.8, 0.8001, 0.999, 1, 1.0, 1.000001, 1.1000000, 1.2, 1.33},
	[]float64{1.1000000, 1.2, 0.8001, 0.999, 1.33, 1, 1.0, 1.000001},
	[]float64{1.2, 0.8001, 0.999, 1.33, 1.000092},
	[]float64{0.8001, 0.999, 1.33, 1.400001},
)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{ 0.8001, 0.999, 1.33 }
Output:

func IsArray added in v1.0.0

func IsArray(data interface{}) bool

IsArray will return true when type of the data is array/slice

func IsBool added in v1.0.0

func IsBool(data interface{}) bool

IsBool will return true when type of the data is boolean

func IsChannel added in v1.0.0

func IsChannel(data interface{}) bool

IsChannel will return true when type of the data is channel

func IsDate added in v1.0.0

func IsDate(data interface{}) bool

IsDate will return true when type of the data is time.Time

func IsEmpty added in v1.0.0

func IsEmpty(data interface{}) bool

IsEmpty will return false to any null-able data which value is nil (chan, func, interface, map, pointer, slice), will also return false when the value is default value of it's data type (false for bool, "" for string, 0 for numeric value), and will return false if the value is slice or map and the length is 0

func IsEmptyString added in v1.0.0

func IsEmptyString(data interface{}) bool

IsEmptyString will return true when type of the data is string and it's empty

func IsFloat added in v1.0.0

func IsFloat(data interface{}) bool

IsFloat will return true when type of the data is floating number

func IsFunction added in v1.0.0

func IsFunction(data interface{}) bool

IsFunction will return true when type of the data is closure/function

func IsInt added in v1.0.0

func IsInt(data interface{}) bool

IsInt will return true when type of the data is numeric integer

func IsMap added in v1.0.0

func IsMap(data interface{}) bool

IsMap will return true when type of the data is hash map

func IsNil added in v1.0.0

func IsNil(data interface{}) bool

IsNil will return true when type of the data is nil

func IsNumeric added in v1.0.0

func IsNumeric(data interface{}) bool

IsNumeric will return true when type of the data is numeric (float, uint, int)

func IsPointer added in v1.0.0

func IsPointer(data interface{}) bool

IsPointer will return true when type of the data is pointer

func IsSlice added in v1.0.0

func IsSlice(data interface{}) bool

IsSlice is alias of IsArray()

func IsString added in v1.0.0

func IsString(data interface{}) bool

IsString will return true when type of the data is string

func IsStructObject added in v1.0.0

func IsStructObject(data interface{}) bool

IsStructObject will return true when type of the data is object from struct

func IsTrue added in v1.0.0

func IsTrue(data interface{}) bool

IsTrue will return true when type of the data is bool, and the value is true

func IsUint added in v1.0.0

func IsUint(data interface{}) bool

IsUint will return true when type of the data is uint

func IsZeroNumber added in v1.0.0

func IsZeroNumber(data interface{}) bool

IsZeroNumber will return true when type of the data is numeric and it's has 0 value

func Join

func Join(data interface{}, separator string) (string, error)

Join function converts all elements in `data` into a string separated by `separator`.

Parameters

This function requires two mandatory parameters:

data      // type: slice, description: the slices to convert
separator // type: string, description: the element separator

Return values

This function return two values:

string // description: returns the joined string
error  // description: hold error message if there is an error

Examples

2 examples available:

Example (Join1)
data := []string{"damian", "grayson", "cassandra"}
separator := " - "

result, err := Join(data, separator)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> "damian - grayson - cassandra"
Output:

Example (Join2)
data := []int{1, 2, 3, 4}
separator := ", "

result, err := Join(data, separator)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> "1, 2, 3, 4"
Output:

func KeyBy

func KeyBy(data, callback interface{}) (interface{}, error)

KeyBy function creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the last element responsible for generating the key. The iteratee is invoked with one argument: (value).

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (KeyBy)
type HashMap map[string]string

data := []HashMap{
	{"name": "grayson", "hobby": "helping people"},
	{"name": "jason", "hobby": "punching people"},
	{"name": "tim", "hobby": "stay awake all the time"},
	{"name": "damian", "hobby": "getting angry"},
}

result, err := KeyBy(data, func(each HashMap) string {
	return each["name"]
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
   map[string]main.HashMap {
     "grayson": main.HashMap{ "hobby": "helping people", "name": "grayson" },
     "jason":   main.HashMap{ "name": "jason", "hobby": "punching people" },
     "tim":     main.HashMap{ "name": "tim", "hobby": "stay awake all the time" },
     "damian":  main.HashMap{ "name": "damian", "hobby": "getting angry" },
   }
*/  }
*/
Output:

func Last

func Last(data interface{}) (interface{}, error)

Last function gets the last element of `data`.

Parameters

This function requires one mandatory parameter:

data // type: slice, description: the slices to query

Return values

This function return two values:

anyType // description: returns the last element of `data`
error   // description: hold error message if there is an error

Examples

3 examples available:

Example (Last1)
data := []string{"damian", "grayson", "cassandra"}
result, err := Last(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> "cassandra"
Output:

Example (Last2)
data := []int{1}
result, err := Last(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 1
Output:

Example (Last3)
data := []string{}
result, err := Last(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> nil
Output:

func LastIndexOf

func LastIndexOf(data interface{}, search interface{}, args ...int) (int, error)

LastIndexOf function is like `IndexOf`, except that it iterates over elements of `data` from right to left.

Parameters

This function requires two mandatory parameters `data` and `search`; and an optional parameter:

data                  // type: slice, description: the slices to inspect
search                // type: anyType, description: the value to search for
fromIndex=len(data)-1 // type: number, description: the index to search from

Return values

This function return two values:

number // description: returns the index of the matched value, else `-1`
error  // description: hold error message if there is an error

Examples

3 examples available:

Example (LastIndexOf1)
data := []string{"damian", "grayson", "cass", "tim", "tim", "jason", "steph"}

LastIndexOf(data, "duke")    // ===> -1
LastIndexOf(data, "tim")     // ===> 4
LastIndexOf(data, "tim", 4)  // ===> 4
LastIndexOf(data, "tim", -4) // ===> 3
LastIndexOf(data, "tim", -3) // ===> 4
LastIndexOf(data, "tim", -2) // ===> 4
Output:

Example (LastIndexOf2)
data := []float64{2.1, 2.2, 3, 3.00000, 3.1, 3.9, 3.95}

LastIndexOf(data, 2.2)           // ===> 1
LastIndexOf(data, 3)             // ===> -1 (because 3 is detected as int32, not float64)
LastIndexOf(data, float64(3))    // ===> 3
LastIndexOf(data, float64(3), 2) // ===> 2
LastIndexOf(data, float64(3), 3) // ===> 3
Output:

Example (LastIndexOf3)
data := []interface{}{"jason", 24, true}

LastIndexOf(data, 24)     // ===> 1
LastIndexOf(data, 24, -1) // ===> 1
Output:

func Map

func Map(data, callback interface{}) (interface{}, error)

Map function creates an array of values by running each element in collection thru iteratee. The iteratee is invoked with two arguments: (value, index).

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (Map1)
type Sample struct {
	EbookName      string
	DailyDownloads int
}

data := []Sample{
	{EbookName: "clean code", DailyDownloads: 10000},
	{EbookName: "rework", DailyDownloads: 12000},
	{EbookName: "detective comics", DailyDownloads: 11500},
}

result, err := Map(data, func(each Sample, i int) string {
	return each.EbookName
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "clean code", "rework", "detective comics" }
Output:

Example (Map2)
type SampleOne struct {
	EbookName      string
	DailyDownloads int
	IsActive       bool
}

type SampleTwo struct {
	Ebook                string
	DownloadsInThousands float32
}

data := []SampleOne{
	{EbookName: "clean code", DailyDownloads: 10000, IsActive: true},
	{EbookName: "rework", DailyDownloads: 12000, IsActive: false},
	{EbookName: "detective comics", DailyDownloads: 11500, IsActive: true},
}

result, err := Map(data, func(each SampleOne, i int) SampleTwo {
	ebook := each.EbookName
	if !each.IsActive {
		ebook = fmt.Sprintf("%s (inactive)", each.EbookName)
	}

	downloadsInThousands := float32(each.DailyDownloads) / float32(1000)

	return SampleTwo{Ebook: ebook, DownloadsInThousands: downloadsInThousands}
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*

	  []SampleTwo {
		{ Ebook: "clean code", DownloadsInThousands: 10 },
		{ Ebook: "rework (inactive)", DownloadsInThousands: 12 },
		{ Ebook: "detective comics", DownloadsInThousands: 11.5 },
	  }

*/}

*/
Output:

func Nth

func Nth(data interface{}, i int) (interface{}, error)

Nth function gets the element at index `n` of `data`. If `n` is negative, the nth element from the end is returned.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slices to query
n=0  // type: number, description: The index of the element to return

Return values

This function return two values:

AnyType // description: returns the nth element of `data`
error   // description: hold error message if there is an error

Examples

2 examples available:

Example (Nth1)
data := []string{"grayson", "jason", "tim", "damian"}

Nth(data, 1)  // ===> "jason"
Nth(data, 2)  // ===> "tim"
Nth(data, -1) // ===> "damian"
Output:

Example (Nth2)
data := []int{1, 2, 3, 4, 5}
result, err := Nth(data, 4)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 5
Output:

func OrderBy

func OrderBy(data, callback interface{}, args ...bool) (interface{}, error)

OrderBy sort slices. If orders is unspecified, all values are sorted in ascending order. Otherwise, specify an order of "desc" for descending or "asc" for ascending sort order of corresponding values. The algorithm used is merge sort, as per savigo's post on https://sagivo.com/go-sort-faster-4869bdabc670

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (OrderBy1)
type HashMap map[string]string

data := []HashMap{
	{"name": "tim", "hobby": "stay awake all the time"},
	{"name": "grayson", "hobby": "helping people"},
	{"name": "damian", "hobby": "getting angry"},
	{"name": "jason", "hobby": "punching people"},
}

result, err := OrderBy(data, func(each HashMap) string {
	return each["name"]
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
   []main.HashMap{
     { "name": "damian", "hobby": "getting angry" },
     { "name": "grayson", "hobby": "helping people" },
     { "name": "jason", "hobby": "punching people" },
     { "name": "tim", "hobby": "stay awake all the time" },
   }
*/  }
*/
Output:

Example (OrderBy2)
type HashMap map[string]interface{}

data := []HashMap{
	{"name": "tim", "hobby": "stay awake all the time", "age": 20},
	{"name": "grayson", "hobby": "helping people", "age": 24},
	{"name": "damian", "hobby": "getting angry", "age": 17},
	{"name": "jason", "hobby": "punching people", "age": 22},
}

result, err := OrderBy(data, func(each HashMap) int {
	return each["age"].(int)
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	[]main.HashMap{
	  { "age": 17, "hobby": "getting angry", "name": "damian" },
	  { "age": 20, "name": "tim", "hobby": "stay awake all the time" },
	  { "age": 22, "name": "jason", "hobby": "punching people" },
	  { "age": 24, "name": "grayson", "hobby": "helping people" },
	}
*/		}
*/
Output:

Example (OrderBy3)
type HashMap map[string]interface{}

data := []HashMap{
	{"name": "tim", "hobby": "stay awake all the time", "age": 20},
	{"name": "grayson", "hobby": "helping people", "age": 24},
	{"name": "damian", "hobby": "getting angry", "age": 17},
	{"name": "jason", "hobby": "punching people", "age": 22},
}

result, err := OrderBy(data, func(each HashMap) int {
	return each["age"].(int)
}, false)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
   []main.HashMap{
     { "age": 24, "name": "grayson", "hobby": "helping people" },
     { "age": 22, "name": "jason", "hobby": "punching people" },
     { "age": 20, "name": "tim", "hobby": "stay awake all the time" },
     { "age": 17, "name": "damian", "hobby": "getting angry" },
   }
*/  }
*/
Output:

Example (OrderBy4)
type HashMap map[string]interface{}

data := []HashMap{
	{"name": "tim", "hobby": "stay awake all the time", "age": 20},
	{"name": "grayson", "hobby": "helping people", "age": 24},
	{"name": "damian", "hobby": "getting angry", "age": 17},
	{"name": "jason", "hobby": "punching people", "age": 22},
}

result, err := OrderBy(data, func(each HashMap) int {
	return each["age"].(int)
}, true, false)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
   []main.HashMap{
     { "age": 17, "name": "damian", "hobby": "getting angry" },
     { "age": 20, "name": "tim", "hobby": "stay awake all the time" },
     { "age": 22, "name": "jason", "hobby": "punching people" },
     { "age": 24, "name": "grayson", "hobby": "helping people" },
   }
*/  }
*/
Output:

func Partition

func Partition(data, callback interface{}) (interface{}, interface{}, error)

Partition function creates an array of elements split into two groups, the first of which contains elements predicate returns truthy for, the second of which contains elements predicate returns falsey for. The predicate is invoked with one argument: (value).

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (Partition)
type HashMap map[string]interface{}

data := []HashMap{
	{"name": "grayson", "isMale": true},
	{"name": "jason", "isMale": true},
	{"name": "barbara", "isMale": false},
	{"name": "tim", "isMale": true},
	{"name": "cassandra", "isMale": false},
	{"name": "stephanie", "isMale": false},
	{"name": "damian", "isMale": true},
	{"name": "duke", "isMale": true},
}

resultTruthy, resultFalsey, err := Partition(data, func(each HashMap) bool {
	return each["isMale"].(bool)
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Printf("%#v \n", resultTruthy)
/*
   []HashMap {
     { "name": "grayson", "isMale": true },
     { "name": "jason", "isMale": true },
     { "name": "tim", "isMale": true },
     { "name": "damian", "isMale": true },
     { "name": "duke", "isMale": true },
   }
*/   }
*/

fmt.Printf("%#v \n", resultFalsey)
/*
   []HashMap {
     { "name": "barbara", "isMale": false },
     { "name": "cassandra", "isMale": false },
     { "name": "stephanie", "isMale": false },
   }
*/ }
*/
Output:

func Pull

func Pull(data interface{}, items ...interface{}) (interface{}, error)

Pull function removes all given values from `data`.

Parameters

This function requires one mandatory parameter `data`; and unlimited variadic parameters:

data  // type: slice, description: the slices to modify
item1 // optional, type: anyType, description: item to remove
item2 // optional, type: anyType, description: item to remove
item3 // optional, type: anyType, description: item to remove
...

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

3 examples available:

Example (Pull1)
data := []int{1, 2, 3, 4, 5, 6}
result, err := Pull(data, 3)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 1, 2, 4, 5, 6 }
Output:

Example (Pull2)
data := []float64{1.1, 2.1, 3.2, 4.2, 5.2, 6.3}
result, err := Pull(data, 2.1, 3.2, 6.3)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{ 1.1, 4.2, 5.2 }
Output:

Example (Pull3)
data := []string{"damian", "grayson", "cassandra", "tim", "tim", "jason", "stephanie"}
result, err := Pull(data, "grayson", "tim")

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "damian", "cassandra", "jason", "stephanie" }
Output:

func PullAll

func PullAll(data interface{}, items interface{}) (interface{}, error)

PullAll function is similar like `Pull`, except that it accepts a slice of values to remove.

Parameters

This function requires two mandatory parameters:

data  // type: slice, description: the slices to modify
items // type: slice, description: items to remove

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (PullAll1)
data := []float64{1.1, 2.1, 3.2, 4.2, 5.2, 6.3}
exclude := []float64{2.1, 3.2, 6.3}

result, err := PullAll(data, exclude)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{ 1.1, 4.2, 5.2 }
Output:

Example (PullAll2)
data := []string{"damian", "grayson", "cassandra", "tim", "tim", "jason", "stephanie"}
exclude := []string{"grayson", "tim"}

result, err := PullAll(data, exclude)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "damian", "cassandra", "jason", "stephanie" }
Output:

func PullAt

func PullAt(data interface{}, indexes ...int) (interface{}, error)

PullAt function removes elements from `data` corresponding to `indexes` and returns an array of removed elements.

Parameters

This function requires one mandatory parameter `data`; and unlimited variadic parameters:

data     // type: slice, description: the slices to modify
indexes1 // optional, type: int, description: index to remove
indexes2 // optional, type: int, description: index to remove
indexes3 // optional, type: int, description: index to remove
...

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

1 examples available:

Example (PullAt)
data := []float64{1.1, 2.1, 3.2, 4.2, 5.2, 6.3}

result, err := PullAt(data, 1, 3)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{ 1.1, 3.2, 5.2, 6.3 }
Output:

func RandomInt added in v1.0.0

func RandomInt(min, max int) int

RandomInt function generates random numeric data between specified min and max

Example
result := RandomInt(10, 12)
fmt.Println(result) // generates random int between 10 to 12 like: 10 or 11 or 12
Output:

func RandomString added in v1.0.0

func RandomString(length int) string

RandomString function generate random alphabet string in defined length

Example
result := RandomString(32)
fmt.Println(result) // generates random 32 character like: YodQeljldGFluOhaHrlWdICKDtDHSvzA
Output:

func Reduce

func Reduce(data, callback, initial interface{}) (interface{}, error)

Reduce function reduces collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not given, the first element of collection is used as the initial value. The iteratee is invoked with four arguments: (accumulator, value, index|key, collection)

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (ReduceMap1)
type HashMap map[string]interface{}

data := HashMap{
	"name":   "grayson",
	"age":    21,
	"isMale": true,
}

result, err := Reduce(data, func(accumulator string, value interface{}, key string) string {
	if accumulator == "" {
		accumulator = fmt.Sprintf("%s: %v", key, value)
	} else {
		accumulator = fmt.Sprintf("%s, %s: %v", accumulator, key, value)
	}

	return accumulator
}, "")
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> "name: grayson, age: 21, isMale: true"
Output:

Example (ReduceSlice1)
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

result, err := Reduce(data, func(accumulator, each int) int {
	return accumulator + each
}, 0)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 55
Output:

Example (ReduceSlice2)
type HashMap map[string]interface{}

data := [][]interface{}{
	{"name", "grayson"},
	{"age", 21},
	{"isMale", true},
}

result, err := Reduce(data, func(accumulator HashMap, each []interface{}, i int) HashMap {
	accumulator[each[0].(string)] = each[1]
	return accumulator
}, HashMap{})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
   HashMap {
     "name":   "grayson",
     "age":    21,
     "isMale": true,
   }
*/ }
*/
Output:

func Reject

func Reject(data, callback interface{}) (interface{}, error)

Reject function is the opposite of Filter(); This method returns the elements of collection that predicate does not return truthy for.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (RejectMap)
data := map[string]int{
	"clean code":       10000,
	"rework":           12000,
	"detective comics": 11500,
}

result, err := Reject(data, func(value int, key string) bool {
	return value > 11000
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> map[string]int{ "clean code": 10000 }
Output:

Example (RejectSlice)
type Book struct {
	EbookName      string
	DailyDownloads int
}

data := []Book{
	{EbookName: "clean code", DailyDownloads: 10000},
	{EbookName: "rework", DailyDownloads: 12000},
	{EbookName: "detective comics", DailyDownloads: 11500},
}

result, err := Reject(data, func(each Book) bool {
	return each.DailyDownloads > 11000
})
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  []Book{
		{ EbookName: "clean code", DailyDownloads: 10000 },
	  }
*/
*/
Output:

func Remove

func Remove(data interface{}, predicate interface{}) (interface{}, interface{}, error)

Remove function removes all elements from `data` that `predicate` returns truthy for and returns a slice of the removed elements.

Parameters

This function requires two mandatory parameters:

data      // type: slice, description: the slice to inspect
predicate // type: func(each anyType, i int)bool, description: the function invoked per iteration.

Return values

This function return three values:

slice // description: returns slice after elements removed as per `predicate`
slice // description: returns slice of removed elements as per `predicate`
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Remove1)
data := []string{"jason", "damian", "grayson", "tim"}
result, removed, err := Remove(data, func(each string) bool {
	return strings.Contains(each, "m")
})

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)  // ===> []string{ "jason", "grayson" }
fmt.Println(removed) // ===> []string{ "damian", "tim" }
Output:

Example (Remove2)
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
result, removed, err := Remove(data, func(each int) bool {
	return each%2 == 0
})

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)  // ===> []int{ 1, 3, 5, 7, 9 }
fmt.Println(removed) // ===> []int{ 2, 4, 6, 8 }
Output:

func ReplaceCaseInsensitive added in v1.0.0

func ReplaceCaseInsensitive(text, find, replacement string) string

ReplaceCaseInsensitive function replace all string that match with `find` without caring about it's case

Example
result := ReplaceCaseInsensitive("lOrEm IPsUm DoLor Sit AMEt", "ipsum", "batman")
fmt.Println(result) // lOrEm batman DoLor Sit AMEt
Output:

func Reverse

func Reverse(data interface{}) (interface{}, error)

Reverse function reverses `data` so that the first element becomes the last, the second element becomes the second to last, and so on.

Parameters

This function requires one mandatory parameter:

data // type: slice, description: the slice to modify

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Reverse1)
data := []string{"jason", "damian", "grayson", "tim"}
result, err := Reverse(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "tim", "grayson", "damian", "jason" }
Output:

Example (Reverse2)
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
result, err := Reverse(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 9, 8, 7, 6, 5, 4, 3, 2, 1 }
Output:

func Sample

func Sample(data interface{}) (interface{}, error)

Sample function gets a random element from collection.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (Sample)
type Book struct {
	EbookName      string
	DailyDownloads int
}

data := []Book{
	{EbookName: "clean code", DailyDownloads: 10000},
	{EbookName: "rework", DailyDownloads: 12000},
	{EbookName: "detective comics", DailyDownloads: 11500},
}

result, err := Sample(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  the result can be this:
		{ EbookName: "clean code", DailyDownloads: 10000 },

	  this:
		{ EbookName: "rework", DailyDownloads: 12000 },

	  or this:
		{ EbookName: "detective comics", DailyDownloads: 11500 },
*/ },
*/
Output:

func SampleSize

func SampleSize(data interface{}, take int) (interface{}, error)

SampleSize function gets n random elements at unique keys from collection up to the size of collection.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (SampleSize)
type Book struct {
	EbookName      string
	DailyDownloads int
}

data := []Book{
	{EbookName: "clean code", DailyDownloads: 10000},
	{EbookName: "rework", DailyDownloads: 12000},
	{EbookName: "detective comics", DailyDownloads: 11500},
}

result, err := SampleSize(data, 2)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  the result can be this:
		[]Book{
		  { EbookName: "clean code", DailyDownloads: 10000 },
		  { EbookName: "rework", DailyDownloads: 12000 },
		}

	  this:
		[]Book{
		  { EbookName: "rework", DailyDownloads: 12000 },
		  { EbookName: "detective comics", DailyDownloads: 11500 },
		}

	  or this:
		[]Book{
		  { EbookName: "clean code", DailyDownloads: 10000 },
		  { EbookName: "detective comics", DailyDownloads: 11500 },
		}
*/1500 },
		}
*/
Output:

func Shuffle

func Shuffle(data interface{}) (interface{}, error)

Shuffle function creates an array of shuffled values, using a version of the Fisher-Yates shuffle.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (Shuffle1)
data := []int{1, 2, 3, 4}
result, err := Shuffle(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	the result can be this:
	  []int{ 1, 4, 2, 3 }

	this:
	  []int{ 4, 1, 2, 3 }

	or this:
	  []int{ 4, 1, 3, 2 }

	or this:
	  []int{ 3, 4, 1, 2 }

	or ... any other possibilities.
*/ities.
*/
Output:

Example (Shuffle2)
type Book struct {
	EbookName      string
	DailyDownloads int
}

data := []Book{
	{EbookName: "clean code", DailyDownloads: 10000},
	{EbookName: "rework", DailyDownloads: 12000},
	{EbookName: "detective comics", DailyDownloads: 11500},
}

result, err := Shuffle(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
/*
	  the result can be this:
		[]Book {
		  { EbookName: "detective comics", DailyDownloads: 11500 },
		  { EbookName: "clean code", DailyDownloads: 10000 },
		  { EbookName: "rework", DailyDownloads: 12000 },
		}

	  this:
		[]Book {
		  { EbookName: "clean code", DailyDownloads: 10000 },
		  { EbookName: "detective comics", DailyDownloads: 11500 },
		  { EbookName: "rework", DailyDownloads: 12000 },
		}

	  or this:
		[]Book {
		  { EbookName: "rework", DailyDownloads: 12000 },
		  { EbookName: "detective comics", DailyDownloads: 11500 },
		  { EbookName: "clean code", DailyDownloads: 10000 },
		}
*/: 10000 },
		}
*/
Output:

func Size

func Size(data interface{}) (int, error)

Size function gets the size of collection by returning its length for array-like values or the number of own enumerable string keyed properties for objects.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to process
size // type: number, description: the length of each chunk

Return values

This function return two values:

slice // description: returns the new slice of chunks
error // description: hold error message if there is an error

Examples

N examples available:

Example (SizeMap)
data := map[string]interface{}{
	"name":   "noval",
	"age":    24,
	"isMale": true,
}

result, err := Size(data)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> 3
Output:

Example (SizeSlice)
Size([]int{1, 2, 3, 4, 5}) // ===> 5
Size("bruce")              // ===> 5
Output:

func SortBy

func SortBy(data, callback interface{}, args ...bool) (interface{}, error)

SortBy is alias of OrderBy()

func Tail

func Tail(data interface{}) (interface{}, error)

Tail function gets all but the first element of `data`.

Parameters

This function requires one mandatory parameter:

data // type: slice, description: the slice to modify

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Tail1)
data := []string{"jason", "damian", "grayson", "tim"}
result, err := Tail(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "damian", "grayson", "tim" }
Output:

Example (Tail2)
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
result, err := Tail(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 2, 3, 4, 5, 6, 7, 8, 9 }
Output:

func Take

func Take(data interface{}, size int) (interface{}, error)

Take function creates a slice of `data` with `size` elements taken from the beginning.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to query
size // type: number, description: the number of elements to take

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Take1)
data := []string{"jason", "damian", "grayson", "tim"}
take := 2

result, err := Take(data, take)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "jason", "damian" }
Output:

Example (Take2)
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
take := 5

result, err := Take(data, take)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 1, 2, 3, 4, 5 }
Output:

func TakeRight

func TakeRight(data interface{}, size int) (interface{}, error)

TakeRight function creates a slice of `data` with `size` elements taken from the end.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to query
size // type: number, description: the number of elements to take

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (TakeRight1)
data := []string{"jason", "damian", "grayson", "tim"}
take := 2

result, err := TakeRight(data, take)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "grayson", "tim" }
Output:

Example (TakeRight2)
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
take := 5

result, err := TakeRight(data, take)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 5, 6, 7, 8, 9 }
Output:

func Union

func Union(data interface{}, slices ...interface{}) (interface{}, error)

Union function combines all slices presented on the parameters, then create slice of unique values from it. All slice must have same data type.

Parameters

This function requires two mandatory parameters:

data   // type: slice, description: the slice to inspect
slice1 // optional, type: slice, description: the slice to inspect
slice2 // optional, type: slice, description: the slice to inspect
slice3 // optional, type: slice, description: the slice to inspect
...

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Union1)
result, err := Union(
	[]string{"damian", "grayson", "grayson", "cassandra"},
	[]string{"tim", "grayson", "jason", "stephanie"},
	[]string{"duke"},
)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "damian", "grayson", "cassandra", "tim", "jason", "stephanie", "duke" }
Output:

Example (Union2)
result, err := Union(
	[]int{1, 2, 3},
	[]int{2, 3, 4, 5, 6},
	[]int{2, 5, 7, 8},
	[]int{9},
)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }
Output:

func Uniq

func Uniq(data interface{}) (interface{}, error)

Uniq function is same like `Union` but only accept one parameter.

Parameters

This function requires two mandatory parameters:

data // type: slice, description: the slice to inspect

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

2 examples available:

Example (Uniq1)
data := []string{"damian", "grayson", "grayson", "cassandra"}
result, err := Uniq(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "damian", "grayson", "cassandra" }
Output:

Example (Uniq2)
data := []float64{1.1, 3.00000, 3.1, 2.2000000, 3, 2.2, 3.0}
result, err := Uniq(data)

if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{ 1.1, 3, 3.1, 2.2 }
Output:

func Without

func Without(data interface{}, items ...interface{}) (interface{}, error)

Without creates a slice from `data` excluding all given values presented on the `items`.

Parameters

This function requires two mandatory parameters:

data  // type: slice, description: the slice to inspect
item1 // optional, type: anyType, description: item to exclude
item2 // optional, type: anyType, description: item to exclude
item3 // optional, type: anyType, description: item to exclude
...

Return values

This function return two values:

slice // description: returns slice
error // description: hold error message if there is an error

Examples

3 examples available:

Example (Without1)
data := []int{1, 2, 3, 4, 5, 6}
exclude := []int{3}

result, err := Without(data, exclude)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []int{ 1, 2, 4, 5, 6 }
Output:

Example (Without2)
data := []float64{1.1, 2.1, 3.2, 4.2, 5.2, 6.3}
exclude := []float64{2.1, 3.2, 6.3}

result, err := Without(data, exclude)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []float64{ 1.1, 4.2, 5.2 }
Output:

Example (Without3)
data := []string{"damian", "grayson", "cassandra", "tim", "tim", "jason", "stephanie"}
exclude := []string{"grayson", "tim"}

result, err := Without(data, exclude)
if err != nil {
	log.Fatal(err.Error())
}

fmt.Println(result)
// ===> []string{ "damian", "cassandra", "jason", "stephanie" }
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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