Help us shape the future of Go! Take the 2026 Go Developer Survey

slices

package module
v0.0.0-...-0191b04 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2019 License: MIT Imports: 0 Imported by: 1

README

go-slices

Go Report Card Tweet

Unlike many other programming languages, Go doesn't provide helper functions for slices in it's core. I felt like this was quite an essential feature and there weren't any libraries out there that would do this, so I made my own. The functions are the same as the in the Go's official "strings" package, but you can use them on slices.

How to install

Open your terminal and type:

go get github.com/merkur0/go-slices

Then, import this package in your .go file like this:

import (
  "github.com/merkur0/go-slices"
)

And that's it!

Usage:

slices.FunctionName(parameters)

Example:

mySlice := []int{1, 2, 3}
slices.ContainsInt(mySlice, 3) // Returns "true"

List of functions:

Contains

Returns true if the slice contains the item and false if it doesn't. Example:

mySlice := []float32{1.1, 1.2, 1.3, 1.4, 1.5}
slices.ContainsFloat32(myArr, 1.2) // returns true
slices.ContainsFloat32(myArr, 2.3) // returns false

Count

Returns the number of instances of the item within the slice. Example:

mySlice := []int{1, 1, 1, 2, 1}
slices.CountInt(mySlice, 1) // returns 4
slices.CountInt(mySlice, 3) // returns 0

Delete

Returns a copy of the slice where the item with the index (second parameter) is deleted. Example:

mySlice := []int{1, 2, 3, 4, 5}
slices.DeleteInt(mySlice, 0) // returns [2, 3, 4, 5]

Index

Returns the index of the first instance of the second parameter in the slice, or -1 if item is not present in the slice. Example:

mySlice := []int{4, 1, 5}
slices.IndexInt(mySlice, 5) // returns 2

LastIndex

Returns the index of the last instance of the second parameter in the slice, or -1 if item is not present in the slice. Example:

mySlice := []int{1, 2, 1, 1, 4}
slices.LastIndexInt(mySlice, 1) // returns 3

Map

Returns a copy of the slice with all its items modified according to the mapping function. Example:

myFunc := func(number int) int {
    return number * 2
}
mySlice := []int{1, 2, 3, 4}

slices.MapInt(mySlice, myFunc) // returns [2, 4, 6, 8]

Max

Returns the largest element in the slice. Example:

mySlice := []int{1, 2, 3, 4}

slices.MaxInt(mySlice) // returns 4

Min

Returns the smallest element in the slice. Example:

mySlice := []int{1, 2, 3, 4}

slices.MinInt(mySlice) // returns 1

Pop

Returns a copy of the slice with it's last item removed. Example:

mySlice := []int{1, 2, 3, 4}
slices.PopInt(mySlice) // returns [1, 2, 3]

Replace

Returns a copy of the slice where the first instance of the old item (second parameter) is replaced with the new item (third parameter). Example:

mySlice := []int{1, 1, 3, 5, 1}
slices.ReplaceInt(mySlice, 1, 0) // returns [0, 1, 3, 5, 1]

ReplaceAll

Returns a copy of the slice where every instance of the old item (second parameter) is replaced with the new item (third parameter) and returns the new slice. Example:

mySlice := []int{1, 1, 3, 5, 1}
slices.ReplaceAllInt(mySlice, 1, 0) // returns [0, 0, 3, 5, 1]

Shift

Returns a copy of the slice with it's first item removed. Example:

mySlice := []int{1, 2, 3}
slices.ShiftInt(mySlice) // returns [2, 3]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsBool

func ContainsBool(slice []bool, n bool) bool

func ContainsByte

func ContainsByte(slice []byte, n byte) bool

func ContainsComplex64

func ContainsComplex64(slice []complex64, n complex64) bool

func ContainsComplex128

func ContainsComplex128(slice []complex128, n complex128) bool

func ContainsFloat32

func ContainsFloat32(slice []float32, n float32) bool

func ContainsFloat64

func ContainsFloat64(slice []float64, n float64) bool

func ContainsInt

func ContainsInt(slice []int, n int) bool

func ContainsInt8

func ContainsInt8(slice []int8, n int8) bool

func ContainsInt16

func ContainsInt16(slice []int16, n int16) bool

func ContainsInt32

func ContainsInt32(slice []int32, n int32) bool

func ContainsInt64

func ContainsInt64(slice []int64, n int64) bool

func ContainsString

func ContainsString(slice []string, n string) bool

func ContainsUint

func ContainsUint(slice []uint, n uint) bool

func ContainsUint8

func ContainsUint8(slice []uint8, n uint8) bool

func ContainsUint16

func ContainsUint16(slice []uint16, n uint16) bool

func ContainsUint32

func ContainsUint32(slice []uint32, n uint32) bool

func ContainsUint64

func ContainsUint64(slice []uint64, n uint64) bool

func ContainsUintptr

func ContainsUintptr(slice []uintptr, n uintptr) bool

func CountBool

func CountBool(slice []bool, n bool) int

func CountByte

func CountByte(slice []byte, n byte) int

func CountComplex64

func CountComplex64(slice []complex64, n complex64) int

func CountComplex128

func CountComplex128(slice []complex128, n complex128) int

func CountFloat32

func CountFloat32(slice []float32, n float32) int

func CountFloat64

func CountFloat64(slice []float64, n float64) int

func CountInt

func CountInt(slice []int, n int) int

func CountInt8

func CountInt8(slice []int8, n int8) int

func CountInt16

func CountInt16(slice []int16, n int16) int

func CountInt32

func CountInt32(slice []int32, n int32) int

func CountInt64

func CountInt64(slice []int64, n int64) int

func CountString

func CountString(slice []string, n string) int

func CountUint

func CountUint(slice []uint, n uint) int

func CountUint8

func CountUint8(slice []uint8, n uint8) int

func CountUint16

func CountUint16(slice []uint16, n uint16) int

func CountUint32

func CountUint32(slice []uint32, n uint32) int

func CountUint64

func CountUint64(slice []uint64, n uint64) int

func CountUintptr

func CountUintptr(slice []uintptr, n uintptr) int

func DeleteBool

func DeleteBool(slice []bool, i int) []bool

func DeleteByte

func DeleteByte(slice []byte, i int) []byte

func DeleteComplex64

func DeleteComplex64(slice []complex64, i int) []complex64

func DeleteComplex128

func DeleteComplex128(slice []complex128, i int) []complex128

func DeleteFloat32

func DeleteFloat32(slice []float32, i int) []float32

func DeleteFloat64

func DeleteFloat64(slice []float64, i int) []float64

func DeleteInt

func DeleteInt(slice []int, i int) []int

func DeleteInt8

func DeleteInt8(slice []int8, i int) []int8

func DeleteInt16

func DeleteInt16(slice []int16, i int) []int16

func DeleteInt32

func DeleteInt32(slice []int32, i int) []int32

func DeleteInt64

func DeleteInt64(slice []int64, i int) []int64

func DeleteString

func DeleteString(slice []string, i int) []string

func DeleteUint

func DeleteUint(slice []uint, i int) []uint

func DeleteUint8

func DeleteUint8(slice []uint8, i int) []uint8

func DeleteUint16

func DeleteUint16(slice []uint16, i int) []uint16

func DeleteUint32

func DeleteUint32(slice []uint32, i int) []uint32

func DeleteUint64

func DeleteUint64(slice []uint64, i int) []uint64

func DeleteUintptr

func DeleteUintptr(slice []uintptr, i int) []uintptr

func IndexBool

func IndexBool(slice []bool, n bool) int

func IndexByte

func IndexByte(slice []byte, n byte) int

func IndexComplex64

func IndexComplex64(slice []complex64, n complex64) int

func IndexComplex128

func IndexComplex128(slice []complex128, n complex128) int

func IndexFloat32

func IndexFloat32(slice []float32, n float32) int

func IndexFloat64

func IndexFloat64(slice []float64, n float64) int

func IndexInt

func IndexInt(slice []int, n int) int

func IndexInt8

func IndexInt8(slice []int8, n int8) int

func IndexInt16

func IndexInt16(slice []int16, n int16) int

func IndexInt32

func IndexInt32(slice []int32, n int32) int

func IndexInt64

func IndexInt64(slice []int64, n int64) int

func IndexString

func IndexString(slice []string, n string) int

func IndexUint

func IndexUint(slice []uint, n uint) int

func IndexUint8

func IndexUint8(slice []uint8, n uint8) int

func IndexUint16

func IndexUint16(slice []uint16, n uint16) int

func IndexUint32

func IndexUint32(slice []uint32, n uint32) int

func IndexUint64

func IndexUint64(slice []uint64, n uint64) int

func IndexUintptr

func IndexUintptr(slice []uintptr, n uintptr) int

func LastIndexBool

func LastIndexBool(slice []bool, n bool) int

func LastIndexByte

func LastIndexByte(slice []byte, n byte) int

func LastIndexComplex64

func LastIndexComplex64(slice []complex64, n complex64) int

func LastIndexComplex128

func LastIndexComplex128(slice []complex128, n complex128) int

func LastIndexFloat32

func LastIndexFloat32(slice []float32, n float32) int

func LastIndexFloat64

func LastIndexFloat64(slice []float64, n float64) int

func LastIndexInt

func LastIndexInt(slice []int, n int) int

func LastIndexInt8

func LastIndexInt8(slice []int8, n int8) int

func LastIndexInt16

func LastIndexInt16(slice []int16, n int16) int

func LastIndexInt32

func LastIndexInt32(slice []int32, n int32) int

func LastIndexInt64

func LastIndexInt64(slice []int64, n int64) int

func LastIndexString

func LastIndexString(slice []string, n string) int

func LastIndexUint

func LastIndexUint(slice []uint, n uint) int

func LastIndexUint8

func LastIndexUint8(slice []uint8, n uint8) int

func LastIndexUint16

func LastIndexUint16(slice []uint16, n uint16) int

func LastIndexUint32

func LastIndexUint32(slice []uint32, n uint32) int

func LastIndexUint64

func LastIndexUint64(slice []uint64, n uint64) int

func LastIndexUintptr

func LastIndexUintptr(slice []uintptr, n uintptr) int

func MapBool

func MapBool(slice []bool, f func(bool) bool) []bool

func MapByte

func MapByte(slice []byte, f func(byte) byte) []byte

func MapComplex64

func MapComplex64(slice []complex64, f func(complex64) complex64) []complex64

func MapComplex128

func MapComplex128(slice []complex128, f func(complex128) complex128) []complex128

func MapFloat32

func MapFloat32(slice []float32, f func(float32) float32) []float32

func MapFloat64

func MapFloat64(slice []float64, f func(float64) float64) []float64

func MapInt

func MapInt(slice []int, f func(int) int) []int

func MapInt8

func MapInt8(slice []int8, f func(int8) int8) []int8

func MapInt16

func MapInt16(slice []int16, f func(int16) int16) []int16

func MapInt32

func MapInt32(slice []int32, f func(int32) int32) []int32

func MapInt64

func MapInt64(slice []int64, f func(int64) int64) []int64

func MapString

func MapString(slice []string, f func(string) string) []string

func MapUint

func MapUint(slice []uint, f func(uint) uint) []uint

func MapUint8

func MapUint8(slice []uint8, f func(uint8) uint8) []uint8

func MapUint16

func MapUint16(slice []uint16, f func(uint16) uint16) []uint16

func MapUint32

func MapUint32(slice []uint32, f func(uint32) uint32) []uint32

func MapUint64

func MapUint64(slice []uint64, f func(uint64) uint64) []uint64

func MapUintptr

func MapUintptr(slice []uintptr, f func(uintptr) uintptr) []uintptr

func MaxFloat32

func MaxFloat32(slice []float32) float32

func MaxFloat64

func MaxFloat64(slice []float64) float64

func MaxInt

func MaxInt(slice []int) int

func MaxInt8

func MaxInt8(slice []int8) int8

func MaxInt16

func MaxInt16(slice []int16) int16

func MaxInt32

func MaxInt32(slice []int32) int32

func MaxInt64

func MaxInt64(slice []int64) int64

func MaxString

func MaxString(slice []string) string

func MaxUint

func MaxUint(slice []uint) uint

func MaxUint8

func MaxUint8(slice []uint8) uint8

func MaxUint16

func MaxUint16(slice []uint16) uint16

func MaxUint32

func MaxUint32(slice []uint32) uint32

func MaxUint64

func MaxUint64(slice []uint64) uint64

func MinFloat32

func MinFloat32(slice []float32) float32

func MinFloat64

func MinFloat64(slice []float64) float64

func MinInt

func MinInt(slice []int) int

func MinInt8

func MinInt8(slice []int8) int8

func MinInt16

func MinInt16(slice []int16) int16

func MinInt32

func MinInt32(slice []int32) int32

func MinInt64

func MinInt64(slice []int64) int64

func MinString

func MinString(slice []string) string

func MinUint

func MinUint(slice []uint) uint

func MinUint8

func MinUint8(slice []uint8) uint8

func MinUint16

func MinUint16(slice []uint16) uint16

func MinUint32

func MinUint32(slice []uint32) uint32

func MinUint64

func MinUint64(slice []uint64) uint64

func PopBool

func PopBool(slice []bool) []bool

func PopByte

func PopByte(slice []byte) []byte

func PopComplex64

func PopComplex64(slice []complex64) []complex64

func PopComplex128

func PopComplex128(slice []complex128) []complex128

func PopFloat32

func PopFloat32(slice []float32) []float32

func PopFloat64

func PopFloat64(slice []float64) []float64

func PopInt

func PopInt(slice []int) []int

func PopInt8

func PopInt8(slice []int8) []int8

func PopInt16

func PopInt16(slice []int16) []int16

func PopInt32

func PopInt32(slice []int32) []int32

func PopInt64

func PopInt64(slice []int64) []int64

func PopString

func PopString(slice []string) []string

func PopUint

func PopUint(slice []uint) []uint

func PopUint8

func PopUint8(slice []uint8) []uint8

func PopUint16

func PopUint16(slice []uint16) []uint16

func PopUint32

func PopUint32(slice []uint32) []uint32

func PopUint64

func PopUint64(slice []uint64) []uint64

func PopUintptr

func PopUintptr(slice []uintptr) []uintptr

func ReplaceAllBool

func ReplaceAllBool(slice []bool, old, new bool) []bool

func ReplaceAllByte

func ReplaceAllByte(slice []byte, old, new byte) []byte

func ReplaceAllComplex64

func ReplaceAllComplex64(slice []complex64, old, new complex64) []complex64

func ReplaceAllComplex128

func ReplaceAllComplex128(slice []complex128, old, new complex128) []complex128

func ReplaceAllFloat32

func ReplaceAllFloat32(slice []float32, old, new float32) []float32

func ReplaceAllFloat64

func ReplaceAllFloat64(slice []float64, old, new float64) []float64

func ReplaceAllInt

func ReplaceAllInt(slice []int, old, new int) []int

func ReplaceAllInt8

func ReplaceAllInt8(slice []int8, old, new int8) []int8

func ReplaceAllInt16

func ReplaceAllInt16(slice []int16, old, new int16) []int16

func ReplaceAllInt32

func ReplaceAllInt32(slice []int32, old, new int32) []int32

func ReplaceAllInt64

func ReplaceAllInt64(slice []int64, old, new int64) []int64

func ReplaceAllString

func ReplaceAllString(slice []string, old, new string) []string

func ReplaceAllUint

func ReplaceAllUint(slice []uint, old, new uint) []uint

func ReplaceAllUint8

func ReplaceAllUint8(slice []uint8, old, new uint8) []uint8

func ReplaceAllUint16

func ReplaceAllUint16(slice []uint16, old, new uint16) []uint16

func ReplaceAllUint32

func ReplaceAllUint32(slice []uint32, old, new uint32) []uint32

func ReplaceAllUint64

func ReplaceAllUint64(slice []uint64, old, new uint64) []uint64

func ReplaceAllUintptr

func ReplaceAllUintptr(slice []uintptr, old, new uintptr) []uintptr

func ReplaceBool

func ReplaceBool(slice []bool, old, new bool) []bool

func ReplaceByte

func ReplaceByte(slice []byte, old, new byte) []byte

func ReplaceComplex64

func ReplaceComplex64(slice []complex64, old, new complex64) []complex64

func ReplaceComplex128

func ReplaceComplex128(slice []complex128, old, new complex128) []complex128

func ReplaceFloat32

func ReplaceFloat32(slice []float32, old, new float32) []float32

func ReplaceFloat64

func ReplaceFloat64(slice []float64, old, new float64) []float64

func ReplaceInt

func ReplaceInt(slice []int, old, new int) []int

func ReplaceInt8

func ReplaceInt8(slice []int8, old, new int8) []int8

func ReplaceInt16

func ReplaceInt16(slice []int16, old, new int16) []int16

func ReplaceInt32

func ReplaceInt32(slice []int32, old, new int32) []int32

func ReplaceInt64

func ReplaceInt64(slice []int64, old, new int64) []int64

func ReplaceString

func ReplaceString(slice []string, old, new string) []string

func ReplaceUint

func ReplaceUint(slice []uint, old, new uint) []uint

func ReplaceUint8

func ReplaceUint8(slice []uint8, old, new uint8) []uint8

func ReplaceUint16

func ReplaceUint16(slice []uint16, old, new uint16) []uint16

func ReplaceUint32

func ReplaceUint32(slice []uint32, old, new uint32) []uint32

func ReplaceUint64

func ReplaceUint64(slice []uint64, old, new uint64) []uint64

func ReplaceUintptr

func ReplaceUintptr(slice []uintptr, old, new uintptr) []uintptr

func ShiftBool

func ShiftBool(slice []bool) []bool

func ShiftByte

func ShiftByte(slice []byte) []byte

func ShiftComplex64

func ShiftComplex64(slice []complex64) []complex64

func ShiftComplex128

func ShiftComplex128(slice []complex128) []complex128

func ShiftFloat32

func ShiftFloat32(slice []float32) []float32

func ShiftFloat64

func ShiftFloat64(slice []float64) []float64

func ShiftInt

func ShiftInt(slice []int) []int

func ShiftInt8

func ShiftInt8(slice []int8) []int8

func ShiftInt16

func ShiftInt16(slice []int16) []int16

func ShiftInt32

func ShiftInt32(slice []int32) []int32

func ShiftInt64

func ShiftInt64(slice []int64) []int64

func ShiftString

func ShiftString(slice []string) []string

func ShiftUint

func ShiftUint(slice []uint) []uint

func ShiftUint8

func ShiftUint8(slice []uint8) []uint8

func ShiftUint16

func ShiftUint16(slice []uint16) []uint16

func ShiftUint32

func ShiftUint32(slice []uint32) []uint32

func ShiftUint64

func ShiftUint64(slice []uint64) []uint64

func ShiftUintptr

func ShiftUintptr(slice []uintptr) []uintptr

Types

This section is empty.

Jump to

Keyboard shortcuts

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