slice package - github.com/psampaz/slice - Go Packages

Package slice provides typesafe functions for common Go slice operations.

This section is empty.

This section is empty.

ContainsBool checks if a value exists in a bool slice

ContainsByte checks if a value exists in a byte slice

ContainsComplex64 checks if a value exists in a complex64 slice

ContainsComplex128 checks if a value exists in a complex128 slice

ContainsFloat32 checks if a value exists in a float32 slice

ContainsFloat64 checks if a value exists in a float64 slice

ContainsInt checks if a value exists in an int slice

a := []int{1, 2, 3, 0, 7, 5, 2}
contains := ContainsInt(a, 7)
fmt.Printf("%v", contains)
Output:

true

ContainsInt8 checks if a value exists in an int8 slice

ContainsInt16 checks if a value exists in an int16 slice

ContainsInt32 checks if a value exists in an int32 slice

ContainsInt64 checks if a value exists in an int64 slice

ContainsRune checks if a value exists in a rune slice

ContainsString checks if a value exists in a string slice

ContainsUint checks if a value exists in a uint slice

ContainsUint8 checks if a value exists in a uint8 slice

ContainsUint16 checks if a value exists in a uint16 slice

ContainsUint32 checks if a value exists in a uint32 slice

ContainsUint64 checks if a value exists in a uint64 slice

ContainsUintptr checks if a value exists in a uintptr slice

CopyBool creates a copy of a bool slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyByte creates a copy of a byte slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyComplex64 creates a copy of a complex64 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyComplex128 creates a copy of a complex128 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyFloat32 creates a copy of a float32 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyFloat64 creates a copy of a float64 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyInt creates a copy of an int slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyInt8 creates a copy of an int8 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyInt16 creates a copy of an int16 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyInt32 creates a copy of an int32 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyInt64 creates a copy of an int64 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyRune creates a copy of a rune slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyString creates a copy of a string slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyUint creates a copy of a uint slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyUint8 creates a copy of a uint8 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyUint16 creates a copy of a uint16 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyUint32 creates a copy of a uint32 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyUint64 creates a copy of a uint64 slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

CopyUintptr creates a copy of a uintptr slice. The resulting slice has the same elements as the original but the underlying array is different. See https://github.com/go101/go101/wiki

DeduplicateBool performs order preserving, in place deduplication of a bool slice

DeduplicateByte performs order preserving, in place deduplication of a byte slice

DeduplicateComplex64 performs order preserving, in place deduplication of a complex64 slice

DeduplicateComplex128 performs order preserving, in place deduplication of a complex128 slice

DeduplicateFloat32 performs order preserving, in place deduplication of a float32 slice

DeduplicateFloat64 performs order preserving, in place deduplication of a float64 slice

func DeduplicateInt(a []int) []int

DeduplicateInt performs order preserving, in place deduplication of a int slice

a := []int{1, 2, 3, 2, 5, 3}
a = DeduplicateInt(a)
fmt.Printf("%v", a)
Output:

[1 2 3 5]

DeduplicateInt8 performs order preserving, in place deduplication of a int8 slice

DeduplicateInt16 performs order preserving, in place deduplication of a int16 slice

DeduplicateInt32 performs order preserving, in place deduplication of a int32 slice

DeduplicateInt64 performs order preserving, in place deduplication of a int64 slice

DeduplicateRune performs order preserving, in place deduplication of a rune slice

DeduplicateString performs order preserving, in place deduplication of a string slice

DeduplicateUint performs order preserving, in place deduplication of a uint slice

DeduplicateUint8 performs order preserving, in place deduplication of a uint8 slice

DeduplicateUint16 performs order preserving, in place deduplication of a uint16 slice

DeduplicateUint32 performs order preserving, in place deduplication of a uint32 slice

DeduplicateUint64 performs order preserving, in place deduplication of a uint64 slice

DeduplicateUintptr performs order preserving, in place deduplication of a uintptr slice

DeleteBool removes an element at a specific index of a bool slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteByte removes an element at a specific index of a byte slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteComplex64 removes an element at a specific index of a complex64 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteComplex128 removes an element at a specific index of a complex128 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteFloat32 removes an element at a specific index of a float32 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteFloat64 removes an element at a specific index of a float64 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteInt removes an element at a specific index of an int slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

a := []int{1, 2, 3, 4, 5}
a, err := DeleteInt(a, 2)
fmt.Printf("%v, %v", a, err)
Output:

[1 2 4 5], <nil>

DeleteInt8 removes an element at a specific index of an int8 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteInt16 removes an element at a specific index of an int16 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteInt32 removes an element at a specific index of an int32 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteInt64 removes an element at a specific index of an int64 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteRangeBool deletes the elements between from and to index (inclusive) from a bool slice

DeleteRangeByte deletes the elements between from and to index (inclusive) from a byte slice

DeleteRangeComplex64 deletes the elements between from and to index (inclusive) from a complex64 slice

DeleteRangeComplex128 deletes the elements between from and to index (inclusive) from a complex128 slice

DeleteRangeFloat32 deletes the elements between from and to index (inclusive) from a float32 slice

DeleteRangeFloat64 deletes the elements between from and to index (inclusive) from a float64 slice

DeleteRangeInt deletes the elements between from and to index (inclusive) from a int slice

a := []int{1, 2, 3, 4, 5}
a, err := DeleteRangeInt(a, 2, 3)
fmt.Printf("%v, %v", a, err)
Output:

[1 2 5], <nil>

DeleteRangeInt8 deletes the elements between from and to index (inclusive) from a int8 slice

DeleteRangeInt16 deletes the elements between from and to index (inclusive) from a int16 slice

DeleteRangeInt32 deletes the elements between from and to index (inclusive) from a int32 slice

DeleteRangeInt64 deletes the elements between from and to index (inclusive) from a int64 slice

DeleteRangeRune deletes the elements between from and to index (inclusive) from a rune slice

DeleteRangeString deletes the elements between from and to index (inclusive) from a string slice

DeleteRangeUint deletes the elements between from and to index (inclusive) from a uint slice

DeleteRangeUint8 deletes the elements between from and to index (inclusive) from a uint8 slice

DeleteRangeUint16 deletes the elements between from and to index (inclusive) from a uint16 slice

DeleteRangeUint32 deletes the elements between from and to index (inclusive) from a uint32 slice

DeleteRangeUint64 deletes the elements between from and to index (inclusive) from a uint64 slice

DeleteRangeUintptr deletes the elements between from and to index (inclusive) from a uintptr slice

DeleteRune removes an element at a specific index of a rune slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteString removes an element at a specific index of a string slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteUint removes an element at a specific index of a uint slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteUint8 removes an element at a specific index of a uint8 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteUint16 removes an element at a specific index of a uint16 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteUint32 removes an element at a specific index of a uint32 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteUint64 removes an element at a specific index of a uint64 slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

DeleteUintptr removes an element at a specific index of a uintptr slice. An error is return in case the index is out of bounds or if the slice is nil or empty.

FilterBool performs in place filtering of a bool slice based on a predicate

FilterByte performs in place filtering of a byte slice based on a predicate

FilterComplex64 performs in place filtering of a complex64 slice based on a predicate

FilterComplex128 performs in place filtering of a complex128 slice based on a predicate

FilterFloat32 performs in place filtering of a float32 slice based on a predicate

FilterFloat64 performs in place filtering of a float64 slice based on a predicate

FilterInt performs in place filtering of an int slice based on a predicate

a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
keep := func(x int) bool {
	return x%2 == 0
}
a = FilterInt(a, keep)
fmt.Println(a)
// Output [2, 4, 6, 8 , 10]

FilterInt8 performs in place filtering of an int8 slice based on a predicate

FilterInt16 performs in place filtering of an int16 slice based on a predicate

FilterInt32 performs in place filtering of an int32 slice based on a predicate

FilterInt64 performs in place filtering of an int64 slice based on a predicate

FilterRune performs in place filtering of a rune slice based on a predicate

FilterString performs in place filtering of a string slice based on a predicate

FilterUint performs in place filtering of a uint slice based on a predicate

FilterUint8 performs in place filtering of a uint8 slice based on a predicate

FilterUint16 performs in place filtering of a uint16 slice based on a predicate

FilterUint32 performs in place filtering of a uint32 slice based on a predicate

FilterUint64 performs in place filtering of a uint64 slice based on a predicate

FilterUintptr performs in place filtering of a uintptr slice based on a predicate

MaxByte returns the maximum value of a byte slice or an error in case of a nil or empty slice

MaxFloat32 returns the maximum value of a float32 slice or an error in case of a nil or empty slice

MaxFloat64 returns the maximum value of a float64 slice or an error in case of a nil or empty slice

MaxInt returns the maximum value of a int slice or an error in case of a nil or empty slice

a := []int{1, 2, 3, 0, 7, 5, 2}
max, err := MaxInt(a)
fmt.Printf("%d, %v", max, err)
Output:

7, <nil>

MaxInt8 returns the maximum value of a int8 slice or an error in case of a nil or empty slice

MaxInt16 returns the maximum value of a int16 slice or an error in case of a nil or empty slice

MaxInt32 returns the maximum value of a int32 slice or an error in case of a nil or empty slice

MaxInt64 returns the maximum value of a int64 slice or an error in case of a nil or empty slice

MaxRune returns the maximum value of a rune slice or an error in case of a nil or empty slice

MaxUint returns the maximum value of a uint slice or an error in case of a nil or empty slice

MaxUint8 returns the maximum value of a uint8 slice or an error in case of a nil or empty slice

MaxUint16 returns the maximum value of a uint16 slice or an error in case of a nil or empty slice

MaxUint32 returns the maximum value of a uint32 slice or an error in case of a nil or empty slice

MaxUint64 returns the maximum value of a uint64 slice or an error in case of a nil or empty slice

MaxUintptr returns the maximum value of a uintptr slice or an error in case of a nil or empty slice

MinByte returns the minimum value of a byte slice or an error in case of a nil or empty slice

MinFloat32 returns the minimum value of a float32 slice or an error in case of a nil or empty slice

MinFloat64 returns the minimum value of a float64 slice or an error in case of a nil or empty slice

MinInt returns the minimum value of an int slice or an error in case of a nil or empty slice

a := []int{1, 2, 3, 0, 7, 5, 2}
min, err := MinInt(a)
fmt.Printf("%d, %v", min, err)
Output:

0, <nil>

MinInt8 returns the minimum value of an int8 slice or an error in case of a nil or empty slice

MinInt16 returns the minimum value of an int16 slice or an error in case of a nil or empty slice

MinInt32 returns the minimum value of an int32 slice or an error in case of a nil or empty slice

MinInt64 returns the minimum value of an int64 slice or an error in case of a nil or empty slice

MinRune returns the minimum value of a rune slice or an error in case of a nil or empty slice

MinUint returns the minimum value of a uint slice or an error in case of a nil or empty slice

MinUint8 returns the minimum value of a uint8 slice or an error in case of a nil or empty slice

MinUint16 returns the minimum value of a uint16 slice or an error in case of a nil or empty slice

MinUint32 returns the minimum value of a uint32 slice or an error in case of a nil or empty slice

MinUint64 returns the minimum value of a uint64 slice or an error in case of a nil or empty slice

MinUintptr returns the minimum value of a uintptr slice or an error in case of a nil or empty slice

PopBool removes and returns the last value a bool slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopByte removes and returns the last value a byte slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopComplex64 removes and returns the last value a complex64 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopComplex128 removes and returns the last value a complex128 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopFloat32 removes and returns the last value a float32 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopFloat64 removes and returns the last value a float64 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopInt removes and returns the last value a int slice and the remaining slice. An error is returned in case of a nil or empty slice.

a := []int{1, 2, 3, 4, 5}
v, a, err := PopInt(a)
fmt.Printf("%d, %v, %v", v, a, err)
Output:

5, [1 2 3 4], <nil>

PopInt8 removes and returns the last value a int8 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopInt16 removes and returns the last value a int16 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopInt32 removes and returns the last value a int32 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopInt64 removes and returns the last value a int64 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopRune removes and returns the last value a rune slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopString removes and returns the last value a string slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopUint removes and returns the last value a uint slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopUint8 removes and returns the last value a uint8 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopUint16 removes and returns the last value a uint16 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopUint32 removes and returns the last value a uint32 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopUint64 removes and returns the last value a uint64 slice and the remaining slice. An error is returned in case of a nil or empty slice.

PopUintptr removes and returns the last value a uintptr slice and the remaining slice. An error is returned in case of a nil or empty slice.

ReverseBool performs in place reversal of a bool slice

ReverseByte performs in place reversal of a byte slice

ReverseComplex64 performs in place reversal of a complex64 slice

ReverseComplex128 performs in place reversal of a complex128 slice

ReverseFloat32 performs in place reversal of a float32 slice

ReverseFloat64 performs in place reversal of a float64 slice

func ReverseInt(a []int) []int

ReverseInt performs in place reversal of an int slice

a := []int{1, 2, 3, 4, 5}
a = ReverseInt(a)
fmt.Printf("%v", a)
Output:

[5 4 3 2 1]

ReverseInt8 performs in place reversal of an int8 slice

ReverseInt16 performs in place reversal of an int16 slice

ReverseInt32 performs in place reversal of an int32 slice

ReverseInt64 performs in place reversal of an int64 slice

ReverseRune performs in place reversal of a rune slice

ReverseString performs in place reversal of a string slice

ReverseUint performs in place reversal of a uint slice

ReverseUint8 performs in place reversal of a uint8 slice

ReverseUint16 performs in place reversal of a uint16 slice

ReverseUint32 performs in place reversal of a uint32 slice

ReverseUint64 performs in place reversal of a uint64 slice

ReverseUintptr performs in place reversal of a uintptr slice

ShuffleBool shuffles (in place) a bool slice

ShuffleByte shuffles (in place) a byte slice

ShuffleComplex64 shuffles (in place) a complex64 slice

ShuffleComplex128 shuffles (in place) a complex128 slice

ShuffleFloat32 shuffles (in place) a float32 slice

ShuffleFloat64 shuffles (in place) a float64 slice

func ShuffleInt(a []int) []int

ShuffleInt shuffles (in place) a int slice

ShuffleInt8 shuffles (in place) a int8 slice

ShuffleInt16 shuffles (in place) a int16 slice

ShuffleInt32 shuffles (in place) a int32 slice

ShuffleInt64 shuffles (in place) a int64 slice

ShuffleRune shuffles (in place) a rune slice

ShuffleString shuffles (in place) a string slice

ShuffleUint shuffles (in place) a uint slice

ShuffleUint8 shuffles (in place) a uint8 slice

ShuffleUint16 shuffles (in place) a uint16 slice

ShuffleUint32 shuffles (in place) a uint32 slice

ShuffleUint64 shuffles (in place) a uint64 slice

ShuffleUintptr shuffles (in place) a uintptr slice

SumByte returns the sum of the values of a byte Slice or an error in case of a nil or empty slice

SumComplex64 returns the sum of the values of a complex64 Slice or an error in case of a nil or empty slice

SumComplex128 returns the sum of the values of a complex128 Slice or an error in case of a nil or empty slice

SumFloat32 returns the sum of the values of a float32 Slice or an error in case of a nil or empty slice

SumFloat64 returns the sum of the values of a float64 Slice or an error in case of a nil or empty slice

SumInt returns the sum of the values of a int Slice or an error in case of a nil or empty slice

a := []int{1, 2, 3}
sum, err := SumInt(a)
fmt.Printf("%d, %v", sum, err)
Output:

6, <nil>

SumInt8 returns the sum of the values of a int8 Slice or an error in case of a nil or empty slice

SumInt16 returns the sum of the values of a int16 Slice or an error in case of a nil or empty slice

SumInt32 returns the sum of the values of a int32 Slice or an error in case of a nil or empty slice

SumInt64 returns the sum of the values of a int64 Slice or an error in case of a nil or empty slice

SumRune returns the sum of the values of a rune Slice or an error in case of a nil or empty slice

SumUint returns the sum of the values of a uint Slice or an error in case of a nil or empty slice

SumUint8 returns the sum of the values of a uint8 Slice or an error in case of a nil or empty slice

SumUint16 returns the sum of the values of a uint16 Slice or an error in case of a nil or empty slice

SumUint32 returns the sum of the values of a uint32 Slice or an error in case of a nil or empty slice

SumUint64 returns the sum of the values of a uint64 Slice or an error in case of a nil or empty slice

SumUintptr returns the sum of the values of a uintptr Slice or an error in case of a nil or empty slice

This section is empty.