basiccheck

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 1 Imported by: 2

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllInSliceWith added in v0.5.0

func AllInSliceWith[T any](list []T, valid func(T) bool) bool

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

func OneInSliceWith[T any](list []T, find func(T) bool) bool

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

func StringHasOneOfPrefixes(s string, list []string) bool

StringHasOneOfPrefixes check if string has one of prefix list.

func StringHasOneOfSuffixes added in v0.10.0

func StringHasOneOfSuffixes(s string, list []string) bool

StringHasOneOfSuffixes check if string has one of suffix list.

Types

This section is empty.

Jump to

Keyboard shortcuts

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