Check: a Go package for checking conditions

Hi guys,

I have recently published a new Go package, check: check package - github.com/nyggus/check - pkg.go.dev. The package enables you to check various conditions for several types of slices and maps, including their comparison. The package does not specifically aim at testing, since many a time will you need compare two slices, or maps, or check if a value is in a slice, or a map, if a slice is unique, etc. But of course, these functions can often be useful in testing, too - including the in-built testing module. In fact, I don’t think it goes against testing's assumptions; it will simply make some comparisons easier.

You will find a lot of examples in the package’s README, in a doc file with examples, but also in the documentation itself (all functions are covered with tested examples; the whole package has full test coverage).

Just a couple of examples, to show what you can do:

IsUniqueIntSlice([]int{55, 55, 56}) // false
IsUniqueMapStringString(
    map[string]string{"key1": "a", "key2": "a", "key3": "Q"},
) // false 

(For maps, uniqueness means the uniqueness of the map’s values, keys being ignored.)

Floats are compared using Epsilon, which provides the accuracy of the comparison (differences smaller than Epsilon are considered to be 0):

keys, ok := IsValueInMapStringFloat64(
    .01,
    map[string]float64{"a": .011, "b": .02, "c": .02},
    0,
) // [] false
keys, ok = IsValueInMapStringFloat64(
    .01,
    map[string]float64{"a": .011, "b": .02, "c": .02},
    0.1,
) // true [a b]

(Above, keys are those keys for which the value was found.)

AreEqualMapsStringInt(
	map[string]int{"1": 1, "2": 100, "3": 1},
	map[string]int{"1": 1, "3": 1, "2": 100},
) // true
AllKeyValuePairsInMapIntInt(
    map[int]int{1: 1, 3: 3},
    map[int]int{1: 1, 2: 2, 3: 3},
) // true
AnyKeyValuePairsInMapIntInt(
    map[int]int{1: 1, 4: 4},
    map[int]int{1: 1, 2: 2, 3: 3},
) // true
AreEqualSlicesString(
    []string{"a", "a", "b"},
    []string{"a", "b", "a"},
) // false - though the values are the same, their ordering differs
AreEqualSortedSlicesString(
    []string{"a", "a", "b"},
    []string{"a", "b", "a"},
) // true - this function first sorts the slices and then compares them, so ordering is ignored
values, ok := WhichValuesInStringSlice(
    []string{"10", "Shout Bamalama!", "Something else"},
    []string{"10", "Shout Bamalama!", "50", "This and nothing", "Guess what", "10"},
) // true map[10:[0 5] Shout Bamalama!:[1]]

The package offers a lot of such functions for checking conditions. Since it offers checking whether a slice (or a map) has unique values (does not have duplicated values), I decided to also add the corresponding functions to make unique slices (not maps, since they cannot be made unique do to different keys), e.g.,

UniqueStringSlice([]string{"a", "b", "a"}) // [a b]
UniqueIntSlice([]int{1, 1, 3, 3, 12, 1, 1, 12, 1}) // [1 3 12]

I will be obliged for any comments, which you can share here or in the repo (through issues). I imagine some of the things can be coded in a different way or better. I also imagine that some developers would prefer to code each such check themselves - but this package is here to help them, makes code shorter, simpler and easier to understand. But if you find anything wrong or incorrect, please let me know and I will be happy to work on this.

I hope at least some of you will find the package useful. It does not offer anything really complex, and all what if offers can be easily code - but instead of 5-10 lines of code you can use just one function, whose name clearly says what is being done. Simplicity was what I strived for, and hope I achieved it.

If you like the package, please give it a star in github. And if you want to contribute, feel free to do so!