sliceutil

package
v13.9.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 3 Imported by: 4

Documentation

Overview

Package sliceutil provides methods for working with slices

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains deprecated

func Contains[K comparable](slice []K, value K) bool

Contains checks if string slice contains some value

Deprecated: Use method slices.Contains instead

func Copy deprecated

func Copy[K comparable](slice []K) []K

Copy creates copy of given slice

Deprecated: Use method slices.Clone instead

Example
s1 := []string{"A", "B", "C"}
s2 := Copy(s1)

fmt.Printf("%v\n", s2)
Output:

[A B C]

func Deduplicate deprecated

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

Deduplicate removes duplicates from slice. Slice must be sorted before deduplication.

Deprecated: Use method slices.Compact instead

Example
s := []string{"A", "A", "A", "B", "C", "C", "D"}

fmt.Println(Deduplicate(s))
Output:

[A B C D]

func ErrorToString

func ErrorToString(data []error) []string

ErrorToString converts slice with errors to slice with strings

Example
s := []error{fmt.Errorf("error1")}

fmt.Printf("%v\n", ErrorToString(s))
Output:

[error1]

func Exclude

func Exclude[K comparable](slice []K, items ...K) []K

Exclude removes items from slice

Example
s := []string{"A", "B", "C", "D"}

fmt.Println(Exclude(s, "B", "D"))
Output:

[A C]

func Index deprecated

func Index[K comparable](slice []K, item K) int

Index returns index of given item in a slice or -1 otherwise

Deprecated: Use method slices.Index instead

Example
s := []string{"A", "B", "C"}

fmt.Println(Index(s, "C"))
fmt.Println(Index(s, "D"))
Output:

2
-1

func IntToInterface

func IntToInterface(data []int) []any

IntToInterface converts slice with ints to slice with any

Example
s := []int{1, 2}

fmt.Printf("%v\n", IntToInterface(s))
Output:

[1 2]

func IsEqual deprecated

func IsEqual[K comparable](s1, s2 []K) bool

IsEqual compares two slices and returns true if the slices are equal

Deprecated: Use method slices.Equal instead

Example
s1 := []int{1, 2, 3, 4}
s2 := []int{1, 2, 3, 4}
s3 := []int{1, 3, 2, 4}

fmt.Printf("%v == %v → %t\n", s1, s2, IsEqual(s1, s2))
fmt.Printf("%v == %v → %t\n", s2, s3, IsEqual(s2, s3))
Output:

[1 2 3 4] == [1 2 3 4] → true
[1 2 3 4] == [1 3 2 4] → false

func Join added in v13.3.0

func Join[K any](slice []K, sep string) string

Join concatenates the elements of its first argument to create a single string. Unlike strings.Join, this method supports slices of any type.

Example
s1 := []string{"A", "B", "C", "D"}
s2 := []int{1, 2, 3, 4, 5}
s3 := []any{"John", 183, 98.123, false}

fmt.Println(Join(s1, ":"))
fmt.Println(Join(s2, ","))
fmt.Println(Join(s3, ";"))
Output:

A:B:C:D
1,2,3,4,5
John;183;98.123;false

func StringToError

func StringToError(data []string) []error

StringToError converts slice with strings to slice with errors

func StringToInterface

func StringToInterface(data []string) []any

StringToInterface converts slice with strings to slice with any

Example
s := []string{"A", "B"}

fmt.Printf("%v\n", StringToInterface(s))
Output:

[A B]

Types

This section is empty.

Jump to

Keyboard shortcuts

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