Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basiccheck" ) func main() { input := []string{"foo", "bar"} if basiccheck.InSlice("bar", input) { fmt.Printf("bar found in %v", input) } else { fmt.Printf("bar not found in %v", input) } }
Output: bar found in [foo bar]
Index ¶
- func AllInSliceWith[T any](list []T, valid func(T) bool) bool
- func EqualSlice[T comparable](a, b []T) bool
- func InSlice[T comparable](elem T, list []T) bool
- func OneInSliceWith[T any](list []T, find func(T) bool) bool
- func StringHasOneOfPrefixes(s string, list []string) bool
- func StringHasOneOfSuffixes(s string, list []string) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllInSliceWith ¶ added in v0.5.0
AllInSliceWith check if all elements in a slice return true with the function 'valid' passed in arguments.
If 'valid' is nil, return true.
Example ¶
package main import ( "fmt" "strings" "github.com/jeremmfr/go-utils/basiccheck" ) func main() { input := []string{"boo", "bar", "baz"} if basiccheck.AllInSliceWith(input, func(s string) bool { return strings.HasPrefix(s, "b") }) { fmt.Printf("all of %v has prefix 'b'", input) } else { fmt.Printf("one of %v has not prefix 'b'", input) } }
Output: all of [boo bar baz] has prefix 'b'
func EqualSlice ¶ added in v0.5.0
func EqualSlice[T comparable](a, b []T) bool
EqualSlice check if two slice is Equal: same length, same element in same order.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basiccheck" ) func main() { a := []string{"foo", "bar", "baz"} b := []string{"foo", "bar", "baz"} if basiccheck.EqualSlice(a, b) { fmt.Print("a == b") } else { fmt.Print("a != b") } }
Output: a == b
func InSlice ¶ added in v0.5.0
func InSlice[T comparable](elem T, list []T) bool
InSlice check if an element is present in a slice.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basiccheck" ) func main() { input := []string{"foo", "bar"} if basiccheck.InSlice("bar", input) { fmt.Printf("bar found in %v", input) } else { fmt.Printf("bar not found in %v", input) } }
Output: bar found in [foo bar]
func OneInSliceWith ¶ added in v0.5.0
OneInSliceWith check if at least one element in a slice returns true with the function 'find' passed in arguments.
If 'find' is nil, return false.
Example ¶
package main import ( "fmt" "strings" "github.com/jeremmfr/go-utils/basiccheck" ) func main() { input := []string{"foo", "bar", "baz"} if basiccheck.OneInSliceWith(input, func(s string) bool { return strings.HasPrefix(s, "b") }) { fmt.Printf("one of %v has prefix 'b'", input) } else { fmt.Printf("no one of %v has prefix 'b'", input) } }
Output: one of [foo bar baz] has prefix 'b'
func StringHasOneOfPrefixes ¶ added in v0.2.0
StringHasOneOfPrefixes check if string has one of prefix list.
func StringHasOneOfSuffixes ¶ added in v0.10.0
StringHasOneOfSuffixes check if string has one of suffix list.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.