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

slices

package
v1.0.109 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 7 Imported by: 16

Documentation

Overview

Package slices provides common slice operations (equal, contains, unique, etc.).

Package slices provides additional slice functions on common slice types

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolSlicesEqual deprecated

func BoolSlicesEqual(a, b []bool) bool

BoolSlicesEqual returns true only if the contents of the 2 slices are the same.

Deprecated: use slices.Equal from the standard library.

func ByteSlicesEqual deprecated

func ByteSlicesEqual(a, b []byte) bool

ByteSlicesEqual returns true only if the contents of the 2 slices are the same.

Deprecated: use slices.Equal from the standard library.

func CloneStrings deprecated

func CloneStrings(src []string) []string

CloneStrings will return an independent copy of the src slice, it preserves the distinction between a nil value and an empty slice.

Deprecated: use slices.Clone from the standard library.

func Contains deprecated added in v0.4.1

func Contains[T comparable](arr []T, val T) bool

Contains returns true if val is in arr, and false otherwise.

Deprecated: use slices.Contains from the standard library.

func ContainsString deprecated

func ContainsString(items []string, item string) bool

ContainsString returns true if the items slice contains a value equal to item. Note that this can end up traversing the entire slice, and so is only really suitable for small slices, for larger data sets, consider using a map instead.

Deprecated: use slices.Contains from the standard library.

func ContainsStringEqualFold

func ContainsStringEqualFold(items []string, item string) bool

ContainsStringEqualFold returns true if the items slice contains a value equal to item ignoring case [i.e. using EqualFold] Note that this can end up traversing the entire slice, and so is only really suitable for small slices, for larger data sets, consider using a map instead.

func Deduplicate added in v0.4.1

func Deduplicate[E comparable](slice []E) []E

Deduplicate returns a deduplicated slice.

func Float64SlicesEqual deprecated

func Float64SlicesEqual(a, b []float64) bool

Float64SlicesEqual returns true only if the contents of the 2 slices are the same.

Deprecated: use slices.Equal from the standard library.

func HashStrings deprecated added in v0.4.1

func HashStrings(values ...string) string

HashStrings returns the base64 SHA-1 of a series of string values. Adjacent strings are concatenated with no delimiter, so HashStrings("ab", "c") equals HashStrings("a", "bc").

Deprecated: SHA-1 is not a safe digest. For a non-cryptographic fingerprint use values.XXH3HashArgs128Hex. For integrity use crypto/sha256 with explicit length prefixes. Do not change this function: stored hashes would break.

func Int64SlicesEqual deprecated

func Int64SlicesEqual(a, b []int64) bool

Int64SlicesEqual returns true only if the contents of the 2 slices are the same.

Deprecated: use slices.Equal from the standard library.

func MapStringSlice

func MapStringSlice(items []string, mapFn func(in string) string) []string

MapStringSlice returns a new slices of strings that is the result of applies mapFn to each string in the input slice.

func NvlString deprecated

func NvlString(items ...string) string

NvlString returns the first string from the supplied list that has len() > 0 or "" if all the strings are empty.

Deprecated: use cmp.Or from the standard library (Go 1.22+).

func Prefixed

func Prefixed(prefix string, items []string) []string

Prefixed returns a new slice of strings with each input item prefixed by the supplied prefix e.g. Prefixed("foo", []string{"bar","bob"}) would return []string{"foobar", "foobob"} the input slice is not modified.

func Quoted

func Quoted(items []string) []string

Quoted returns a new slice of strings where each input stream has been wrapped in quotes

func Replace added in v0.4.1

func Replace[E comparable](slice []E, old, newVal E)

Replace replaces all occurrences of old with new in slice.

func StringArrayToMap added in v0.4.1

func StringArrayToMap(arr []string) (map[string]string, error)

func StringContainsOneOf

func StringContainsOneOf(item string, items []string) bool

StringContainsOneOf returns true if one of items slice is a substring of specified value.

func StringSlicesEqual deprecated

func StringSlicesEqual(a, b []string) bool

StringSlicesEqual returns true only if the contents of the 2 slices are the same.

Deprecated: use slices.Equal from the standard library.

func StringStartsWithOneOf

func StringStartsWithOneOf(value string, items []string) bool

StringStartsWithOneOf returns true if one of items slice is a prefix of specified value.

func StringUpto

func StringUpto(str string, maxLen int) string

StringUpto returns the beginning of the string up to `max`

func StringsSafeSplit added in v0.15.80

func StringsSafeSplit(s, sep string) []string

StringsSafeSplit splits a string into a slice of strings, removing whitespaces and empty strings.

func Suffixed

func Suffixed(suffix string, items []string) []string

Suffixed returns a new slice of strings which each input item suffixed by the supplied suffix e.g. Suffixed("foo", []string{"bar","bob"}) would return []string{"barfoo", "bobfoo"} the input slice is not modified

func Truncate added in v0.4.1

func Truncate[T any](arr []T, maxLen uint) []T

Truncate returns a new slice containing the first maxLen elements of arr. If maxLen is greater than the length of arr, arr is returned.

func Uint64SlicesEqual deprecated

func Uint64SlicesEqual(a, b []uint64) bool

Uint64SlicesEqual returns true only if the contents of the 2 slices are the same.

Deprecated: use slices.Equal from the standard library.

func UniqueStrings

func UniqueStrings(dups []string) []string

UniqueStrings removes duplicates from the given list

Types

type Uint64s deprecated

type Uint64s []uint64

Uint64s is a slice of uint64, that knows how to be sorted, using sort.Sort.

Deprecated: use slices.Sort on []uint64 from the standard library.

func (Uint64s) Len

func (a Uint64s) Len() int

Len returns the length of the slice, as required by sort.Interface

func (Uint64s) Less

func (a Uint64s) Less(i, j int) bool

Less returns the true if the value at index i is smaller than the value at index j, as required by sort.Interface

func (Uint64s) Swap

func (a Uint64s) Swap(i, j int)

Swap swaps the values at the indicated indexes, as required by sort.Interface

Jump to

Keyboard shortcuts

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