sliceutil

package
v12.41.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 1 Imported by: 2

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(slice []string, value string) bool

Contains checks if string slice contains some value

func Copy

func Copy(slice []string) []string

Copy creates copy of given slice

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

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

[A B C]

func CopyFloats

func CopyFloats(slice []float64) []float64

CopyFloats creates copy of given slice

Example
s1 := []float64{1.0, 5.0}
s2 := CopyFloats(s1)

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

[1 5]

func CopyInts

func CopyInts(slice []int) []int

CopyInts creates copy of given slice

Example
s1 := []int{1, 2, 3}
s2 := CopyInts(s1)

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

[1 2 3]

func Deduplicate

func Deduplicate(slice []string) []string

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", ErrorToString(s))
Output:

[error1]

func Exclude

func Exclude(slice []string, items ...string) []string

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(slice []string, item string) 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) []interface{}

IntToInterface converts slice with ints to slice with interface{}

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

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

[1 2]

func StringToError

func StringToError(data []string) []error

StringToError converts slice with strings to slice with errors

func StringToInterface

func StringToInterface(data []string) []interface{}

StringToInterface converts slice with strings to slice with interface{}

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

fmt.Printf("%v", 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