sliceutil

package
v12.120.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 1 Imported by: 12

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

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

Contains checks if string slice contains some value

func Copy

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

Copy creates copy of given slice

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

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

[A B C]

func Deduplicate

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

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

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

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

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

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 added in v12.120.0

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

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

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 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