check

package module
Version: v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2021 License: MIT Imports: 2 Imported by: 0

README

check Go package

The check package of Go helps one to check various conditions for

  • slices:
    • []int
    • []float64
    • []string
    • []bool
  • maps:
    • map[string]int
    • map[string]string
    • map[string]float64
    • map[string]bool
    • map[int]int
    • map[int]string
    • map[int]float64
    • map[int]bool

Such checks can be useful in regular code but also in testing. For instance, you can use the check package to compare obtained and expected slices (or maps) obtained. You will find many testing examples in the package's code in its repository).

You can check the following types of conditions:

  • check if all boolean values are true (in a slice and in a map)
  • check if any boolean value is true (in a slice and in a map)
  • check which of boolean values is true (in a slice and in a map)
  • check if all elements of a slice have the same value
  • check if all keys of a map have the same value
  • check if two slices are the same (either taking into account the ordering of the slices or ignoring it)
  • check if two maps are the same
  • check if a value is in a slice
  • check if several values are in a slice
  • check if any of several values are in a slice
  • check which of several values are in a slice
  • check if a key-value pair is among a map's key-values
  • check if a value is among a map's values
  • check if several values from a slice are in a map
  • check if any value from a slice are in a map
  • check which values from a slice are in a map
  • check if all key-value pairs from a map are in a map
  • check if any of key-value pairs from a map are in a map
  • check which key-value pairs from a map are in a map

In addition to these checks, the package enables one to create a unique slice (that is, with unique values) out of a slice.

Working with float64 values: All comparisons of float64 values enable the user to use an epsilon. This means that two floats differ when their absolute difference is higher than the epsilon. If you do not want to use the epsilon, simply make it 0. Always, always pay special attention to working with floats, and make sure what you're doing is what you need.

Many functions resemble one another, but yet there are no generic functions (with the exception of IsUnique). See here for explanation.

Naming convention

Most function names are long, on purpose: this is to help the user fit the function to his or her needs, without the need to remember too many details. Below are some general rules. Note that ... represent details about a slice or a map; e.g., AnyInMap... can be WhichInMapString or WhichInMapInt (which are short versions of WhichInMapStringBool or WhichInMapIntBool).

  • Any() and All(), the only short names, work with boolean slices ([]bool)
  • AnyInMap... and AllInMap... and WhichInMap... functions check if any or all values of a map is/are true (work with map[int]bool and map[string]bool), or which are
  • IsUnique...Slice checks whether a slice of particular type has unique values (IsUniqueIntSlice, IsUniqueStringSlice and IsUniqueFloat64Slice); you can use IsUnique instead, a generic function working with []int, []string and []float64 slices, but not without decrease in performance
  • Unique...Slice returns a new slice of the same type, with unique values of the original slice (so duplicates are removed)
  • IsUniqueMap... checks whether a map has unique values; note that the map's keys are ignored (since each map has unique key-value pairs)
  • AreEqualSlices... compares two slices of type ... (again, int, string or float64); the functions compares both values and the slices' ordering
  • AreEqualSortedSlices... compares two slices of type ...; unlike the above functions, these ignore ordering
  • IsValueIn...Slice checks if a slice has a particular value
  • AnyValueIn...Slice checks if any of values provided as a slice is in another slice
  • AllValuesIn...Slice checks if all values provided as a slice are in another slice
  • WhichValuesIn...Slice checks which values provided as a slice are in another slice
  • AreEqualMaps... compares whether two maps contain the same key-value pairs
  • IsValueInMap... checks whether a map contains a particular value; here, ... can be StringString, IntFloat64 and the like (see above the types of maps that the check package works with)
  • AnyValueInMap... checks whether any of values provided as a slice are among a map's values
  • AllValuesInMap... checks whether all values provided as a slice are among a map's values
  • WhichValuesInMap... checks which values provided as a slice are among a map's values
  • AnyKeyValuePairInMap... checks whether a map contains any of the key-value pairs provided as a map
  • AllKeyValuePairsInMap... checks whether a map contains all key-value pairs provided as a map
  • WhichKeyValuePairsInMap... checks which key-value pairs provided as a map are in a map

As you see, these functions offer many various types of checks, hence their names were designed in such a way so that a function's name facilitates remembering what the function does. The consequence is long names, but this cost comes with readability.

Most functions starting with Is, Any and All return a boolean value. The only exceptions are functions starting with IsValueInMap, since they also return the keys for which this value occurs in the map.

Examples

The above section shows the types of functions you will find in check and their general purposes. You will find many simple examples of using the package's functions in the package's documentation and in the docs folder of this repository, in this file. Below, you will find just several examples that aim to shed some light on what the package offers.

I want to check if two integer slices are the same

What do you mean? I mean, you can compare their values and ignore whether or not the slices are ordered in the same way, or you can take their ordering into account. Let's see:

slice1 := []int{1, 1, 1, 2, 1, 1, 2}
slice2 := []int{1, 1, 2, 1, 1, 1, 2}

Thanks to the check package, we can check if the slices have the same values, using AreEqualSlicesInt() function:

AreEqualSortedSlicesInt(slice1, slice2) // true

As the name suggests, the slices are first sorted and only then compared; hence they're the same. If we want take into account that their orderings differ, then they should not differ, and they don't:

AreEqualSlicesInt(slice1, slice2) // false
I want to check if a slice contains a particular value

Imagine you have the following slice:

slice := []string{"This", "is", "a", "string", "slice", "."}

Does slice contain a word "is"? Let's check:

IsValueInStringSlice("is", slice) // true

Yes, it does! And does it contain "Alice" and "string" and "thing"?

AllValuesInStringSlice([]string{"Alice", "string", "thing"}, slice) // false

No, it does not contain all of them; but perhaps it contains any of them?

AnyValueInStringSlice([]string{"Alice", "string", "thing"}, slice) // true

Yes, this time we do have true: this slice does contain at least one from among those three strings. Simple, but we do not know which of them - and even how many of them - are in the slice. check offers you a function to find this:

values, ok := WhichValuesInStringSlice([]string{"Alice", "string", "thing"}, slice)
// ok is true
// values is map[1:string]

As you see, the Which... functions return two values: a boolean (which is actually the second returned value) that says whether any value from slice1 is in slice2, and a map providing those values. In map[1:string], the key (1) represents the index of the value ("string") in the first slice. Consider another example of a Which... function:

slice1 := []int{1, 2, 3}
slice2 := []int{1, 1, 2, 1, 1, 1, 2, 7, 33, 12, 33, 67, 90}
values, ok := WhichValuesInIntSlice(slice1, slice2)
// ok is true
// map[1:[0 1 3 4 5] 2:[2 6]]
I want to check if a float64 number is among a map's values

As mentioned above, be very careful when working with float numbers. To help you do this, all the functions that compare floats have the Epsilon parameter, which aims to achieve the desired level of accuracy. Of course, another approach could be to round all the values before any comparisons, though not always you will want to round them. Here's an example of how to check if a float64 number is among a map's values.

searchedFloat := .023
Map := map[int]float64{1:.0214, 2:.0212, 3:.0222, 4:.0231}
IsValueInMapIntFloat64(searchedFloat, Map, 0) // false

Here, we used Epsilon = 0, so we did not decrease the accuracy: what we have is what we got, and in that case, .023 is not among Map's values. But when we change this level of accuracy, increasing it to, say, .001 (which means that if two values differ by .001 or less than they are considered equal), then

IsValueInMapIntFloat64(searchedFloat, Map, .001) // true

This is becuase .0231 - .023 is equal to Epsilon (.001), so the searched float is in fact among the map's values.

I want to check if two maps are the same

Let's compare if two maps are the same:

Map1 := map[int]string{1: "What", 2: "a", 3: "mysterious", 4: "thing"}
Map2 := map[int]string{4: "thing", 1: "What", 2: "a", 3: "mysterious"}
Map1 := map[int]string{1: "What", 2: "a", 3: "mysterious", 4: "thing!"}

AreEqualMapsIntString(Map1, Map2) // true
AreEqualMapsIntString(Map1, Map3) // false

Of course, Map1 and Map2 are the same, since the ordering of a map is random and does not matter. Map1 and Map2, however, differ from Map3 because of the exclamation mark in "thing!" of the key 4 of the latter.

I want to check is a slice is unique

You can do so for []int, []string and []float64 slices, e.g.,

IsUniqueIntSlice([]int{1, 1, 2, 12, 3, 21, 71}) // false
IsUniqueIntSlice([]int{1, 15, 2, 12, 3, 21, 71}) // true
IsUniqueFloat64Slice([]float64{.0214, .0212, .0222, .0221}, .001) // false

Note that in the latter case, Epsilon = .001, so .0222 and .0221 are considered equal (their difference is equal to Epsilon). If you increase the accuracy (that is, decrease Epsilon), the result changes:

IsUniqueFloat64Slice([]float64{.0214, .0212, .0222, .0221}, .0001) // true
I want to create a unique slice

As already mentioned, you can create a unique slice (also []float64, for which you will again have to use the Epsilon parameter), even if this does not seem to fit the check package's main purpose. You can do it in the following way:

UniqueIntSlice([]int{1, 1, 2, 2, 3, 21, 1}) // [1 2 3 21]
UniqueStringSlice([]string{"a", "a", "C", "b"}) // [a C b]
UniqueFloat64Slice([]float64{.0021, .0024, .0022, .0031, .00311}, 0) // [.0021, .0024, .0022, .0031, .00311]
UniqueFloat64Slice([]float64{.0021, .0024, .0022, .0031, .00311}, .0001) // [.0021, .0024, .0022, .0031]

Why bother? I can make all those checks directly in my code!

Sure you can! And frankly, this is not that difficult to do - but you need to be careful. The check package, however, offers you an alternative: a one-liner that will say what you're doing instead of five or ten additional lines, and you need not worry about the details. It's like with any package: It can help you save time and energy and work, and it's tested, and so you can use it without worrying that you have made a mistake somewhere there in the code or omitted something important.

Why no generics?

Many of the check functions are similar, so it might be tempting to propose several generic functions. The check package, however, offers only one such generic function, IsUnique (working with []int, []string and []float64 slices), and it does so only for the representation purpose.

One reason behind no generics is that the functions working with float64 values use the Epsilon parameter, which is not used by functions working with string and int values. Hence to make a generic function to work with different signatures would mean either complicating or simplifying things, something better to be avoided. For instance, IsUnique sets Epsilon to 0, a simplification not always preferred.

Another issue could be performance. Generics would require type checking, which could slightly decrease the performance. Nonetheless, this effect is so small that can be considered negligible, unless for very small slices. You can find some benchmarks in this file.

Let's wait for the generics to appear in Go soon, and then we'll see.

Contribution

You will find the package's code to the package in its github repository. Feel free to contribute if you see any bugs or have an idea for improving the package or adding a new functionality. You can either create an issue or submit a merge request.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(Conditions []bool) bool

All checks if all conditions are met. For no conditions, it returns false.

Example
fmt.Println(All([]bool{true, false, false}))
fmt.Println(All([]bool{true, true, true}))
fmt.Println(All([]bool{false, false, false}))
fmt.Println(All([]bool{}))
Output:


false
true
false
false

func AllInMapInt

func AllInMapInt(Conditions map[int]bool) bool

AllMapInt checks if all values in the map are true. For no conditions, it returns false.

Example
fmt.Println(AllInMapInt(map[int]bool{1: true, 2: true}))
fmt.Println(AllInMapInt(map[int]bool{1: true, 2: false}))
fmt.Println(AllInMapInt(map[int]bool{1: false, 2: false}))
Output:

true
false
false

func AllInMapString

func AllInMapString(Conditions map[string]bool) bool

AllInMapString checks if all values in the map are true. For no conditions, it returns false.

Example
fmt.Println(AllInMapString(map[string]bool{"key1": true, "key2": true}))
fmt.Println(AllInMapString(map[string]bool{"key1": true, "key2": false}))
fmt.Println(AllInMapString(map[string]bool{"key1": false, "key2": false}))
Output:

true
false
false

func AllKeyValuePairsInMapIntFloat64

func AllKeyValuePairsInMapIntFloat64(Map1, Map2 map[int]float64, Epsilon float64) bool

AllKeyValuePairsInMapIntFloat64 checks if all key-value pairs from one map[int]float64 are in another map[int]float64. When any of the maps is empty, the function returns false. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(AllKeyValuePairsInMapIntFloat64(
	map[int]float64{1: .1, 2: .2},
	map[int]float64{1: .1, 2: .2},
	0,
))
fmt.Println(AllKeyValuePairsInMapIntFloat64(
	map[int]float64{1: .11, 2: .2},
	map[int]float64{1: .1, 2: .2},
	0,
))
fmt.Println(AllKeyValuePairsInMapIntFloat64(
	map[int]float64{1: .11, 2: .2},
	map[int]float64{1: .1, 2: .2},
	.01,
))
Output:

true
false
true

func AllKeyValuePairsInMapIntInt

func AllKeyValuePairsInMapIntInt(Map1, Map2 map[int]int) bool

AllKeyValuePairsInMapIntInt checks if all key-value pairs from one map[int]int are in another map[int]int. When any of the maps is empty, the function returns false.

Example
fmt.Println(AllKeyValuePairsInMapIntInt(
	map[int]int{1: 1, 2: 2},
	map[int]int{1: 1, 2: 2},
))
fmt.Println(AllKeyValuePairsInMapIntInt(
	map[int]int{1: 1, 2: 2},
	map[int]int{1: 1, 3: 3, 2: 40},
))
Output:

true
false

func AllKeyValuePairsInMapIntString

func AllKeyValuePairsInMapIntString(Map1, Map2 map[int]string) bool

AllKeyValuePairsInMapIntString checks if all key-value pairs from one map[int]string are in another map[int]string. When any of the maps is empty, the function returns false.

Example
fmt.Println(AllKeyValuePairsInMapIntString(
	map[int]string{1: "a", 2: "b"},
	map[int]string{1: "a", 3: "c", 2: "b"},
))
fmt.Println(AllKeyValuePairsInMapIntString(
	map[int]string{1: "a", 2: "b"},
	map[int]string{1: "a", 3: "c", 2: "B"},
))
Output:

true
false

func AllKeyValuePairsInMapStringFloat64

func AllKeyValuePairsInMapStringFloat64(Map1, Map2 map[string]float64, Epsilon float64) bool

AllKeyValuePairsInMapStringFloat64 checks if all key-value pairs from one map[string]float64 are in another map[string]float64. When any of the maps is empty, the function returns false. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(AllKeyValuePairsInMapStringFloat64(
	map[string]float64{"1": .1, "2": .2},
	map[string]float64{"1": .1, "2": .2},
	0,
))
fmt.Println(AllKeyValuePairsInMapStringFloat64(
	map[string]float64{"1": .11, "2": .2},
	map[string]float64{"1": .1, "2": .2},
	0,
))
fmt.Println(AllKeyValuePairsInMapStringFloat64(
	map[string]float64{"1": .11, "2": .2},
	map[string]float64{"1": .1, "2": .2},
	.01,
))
Output:

true
false
true

func AllKeyValuePairsInMapStringInt

func AllKeyValuePairsInMapStringInt(Map1, Map2 map[string]int) bool

AllKeyValuePairsInMapStringInt checks if all key-value pairs from one map[string]int are in another map[string]int. When any of the maps is empty, the function returns false.

Example
fmt.Println(AllKeyValuePairsInMapStringInt(
	map[string]int{"1": 1, "2": 2},
	map[string]int{"1": 1, "2": 2},
))
fmt.Println(AllKeyValuePairsInMapStringInt(
	map[string]int{"1": 1, "2": 2},
	map[string]int{"1": 1, "3": 3, "2": 40},
))
Output:

true
false

func AllKeyValuePairsInMapStringString

func AllKeyValuePairsInMapStringString(Map1, Map2 map[string]string) bool

AllKeyValuePairsInMapStringString checks if all key-value pairs from one map[string]string are in another map[string]string. When any of the maps is empty, the function returns false.

Example
fmt.Println(AllKeyValuePairsInMapStringString(
	map[string]string{"1": "a", "2": "b"},
	map[string]string{"1": "a", "3": "c", "2": "b"},
))
fmt.Println(AllKeyValuePairsInMapStringString(
	map[string]string{"1": "a", "2": "b"},
	map[string]string{"1": "a", "3": "c", "2": "B"},
))
Output:

true
false

func AllValuesInFloat64Slice

func AllValuesInFloat64Slice(Slice1, Slice2 []float64, Epsilon float64) bool

AllValuesInFloat64Slice checks if all values of one float64 slice are in another slice. The Epsilon parameter sets the accuracy of the comparison of two floats. When the first slice is empty, it returns true. When the second slice is empty, it returns false.

Example
fmt.Println(AllValuesInFloat64Slice([]float64{1, 2, 3}, []float64{1, 2, 3, 4}, 0))
fmt.Println(AllValuesInFloat64Slice([]float64{.001, .002, .0022}, []float64{.001, .002}, 0))
fmt.Println(AllValuesInFloat64Slice([]float64{.001, .002, .0022}, []float64{.001, .002}, .001))
Output:

true
false
true

func AllValuesInIntSlice

func AllValuesInIntSlice(Slice1, Slice2 []int) bool

AllValuesInIntSlice checks if all values of one int slice are in another slice. When the first slice is empty, it returns true. When the second slice is empty, it returns false.

Example
fmt.Println(AllValuesInIntSlice([]int{1, 2, 3}, []int{1, 2, 3, 4}))
fmt.Println(AllValuesInIntSlice([]int{1, 2, 3}, []int{1, 3, 4, 5}))
Output:

true
false

func AllValuesInMapIntFloat64

func AllValuesInMapIntFloat64(Slice []float64, Map map[int]float64, Epsilon float64) bool

AllValuesInMapIntFloat64 checks if all values of a float64 slice are values of map[int]float64. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AllValuesInMapIntFloat64([]float64{.01, .2}, map[int]float64{1: .01, 2: .2}, 0))
fmt.Println(AllValuesInMapIntFloat64([]float64{.001, .02, 70.8}, map[int]float64{1: .0011, 2: .021}, .01))
Output:

true
false

func AllValuesInMapIntInt

func AllValuesInMapIntInt(Slice []int, Map map[int]int) bool

AllValuesInMapIntInt checks if all values of an int slice are values of map[int]int. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AllValuesInMapIntInt([]int{10, 20, 70}, map[int]int{1: 10, 2: 20}))
fmt.Println(AllValuesInMapIntInt([]int{10, 20, 70}, map[int]int{1: 20, 2: 10, 5: 70}))
fmt.Println(AllValuesInMapIntInt([]int{10, 1, 7000}, map[int]int{1: 10, 2: 10, 3: 1, 5: 1, 100: 7000}))
Output:

false
true
true

func AllValuesInMapIntString

func AllValuesInMapIntString(Slice []string, Map map[int]string) bool

AllValuesInMapIntString checks if all values of a string slice are in values of map[int]string. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AllValuesInMapIntString([]string{"a", "b", "C"}, map[int]string{1: "a", 2: "b", 3: "C"}))
fmt.Println(AllValuesInMapIntString([]string{"a", "C"}, map[int]string{1: "a", 2: "b"}))
Output:

true
false

func AllValuesInMapStringFloat64

func AllValuesInMapStringFloat64(Slice []float64, Map map[string]float64, Epsilon float64) bool

AllValuesInMapStringFloat64 checks if all values of a float64 slice are values of map[string]float64. The Epsilon parameter sets the accuracy of the comparison of two floats. When either the slice or the map is empty, it returns false. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(AllValuesInMapStringFloat64([]float64{.01, .2}, map[string]float64{"1": .01, "2": .2}, 0))
fmt.Println(AllValuesInMapStringFloat64([]float64{.001, .02, 70.8}, map[string]float64{"1": .0011, "2": .021}, .01))
Output:

true
false

func AllValuesInMapStringInt

func AllValuesInMapStringInt(Slice []int, Map map[string]int) bool

AllValuesInMapStringInt checks if all values of a string slice are values of map[string]int. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AllValuesInMapStringInt([]int{10, 20, 70}, map[string]int{"1": 10, "2": 20, "a": 70}))
fmt.Println(AllValuesInMapStringInt([]int{10, 20, 70}, map[string]int{"1": 10, "2": 20}))
fmt.Println(AllValuesInMapStringInt([]int{1, 2, 7000}, map[string]int{"1": 10, "2": 10, "3": 1, "5": 2, "100": 7000}))
Output:

true
false
true

func AllValuesInMapStringString

func AllValuesInMapStringString(Slice []string, Map map[string]string) bool

AllValuesInMapStringString checks if all values of a string slice are values of map[string]string. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AllValuesInMapStringString([]string{"a", "b", "c"}, map[string]string{"1": "a", "2": "b"}))
fmt.Println(AllValuesInMapStringString([]string{"a", "b", "c"}, map[string]string{"1": "nothing", "2": "else", "3": "matters"}))
fmt.Println(AllValuesInMapStringString([]string{"a", "b", "z"}, map[string]string{"1": "a", "2": "b", "3": "haha", "5": "z", "100": "Zigi says hi!"}))
Output:

false
false
true

func AllValuesInStringSlice

func AllValuesInStringSlice(Slice1, Slice2 []string) bool

AllValuesInStringSlice checks if all values of one string slice are in another slice. When the first slice is empty, it returns true. When the second slice is empty, it returns false.

Example
fmt.Println(AllValuesInStringSlice([]string{"a", "b"}, []string{"a", "b", "c"}))
fmt.Println(AllValuesInStringSlice([]string{"a", "b"}, []string{"a", "c", "d"}))
Output:

true
false

func Any

func Any(Conditions []bool) bool

Any checks if any of the conditions is met. For no conditions, it returns false.

Example
fmt.Println(Any([]bool{true, false, false}))
fmt.Println(Any([]bool{false, false, false}))
fmt.Println(Any([]bool{true}))
fmt.Println(Any([]bool{}))
Output:


true
false
true
false

func AnyInMapInt

func AnyInMapInt(Conditions map[int]bool) bool

AnyInMapInt checks if any of the map's values is true. For no conditions, it returns false.

Example
fmt.Println(AnyInMapInt(map[int]bool{1: true, 2: false}))
fmt.Println(AnyInMapInt(map[int]bool{1: false, 2: false}))
Output:

true
false

func AnyInMapString

func AnyInMapString(Conditions map[string]bool) bool

AnyInMapString checks if any of the map's values is true. Returns a tuple of true/false, keys, where keys are the keys with true.

Example
fmt.Println(AnyInMapString(map[string]bool{"key1": true, "key2": false}))
fmt.Println(AnyInMapString(map[string]bool{"key1": false, "key2": false}))
Output:

true
false

func AnyKeyValuePairInMapIntFloat64

func AnyKeyValuePairInMapIntFloat64(Map1, Map2 map[int]float64, Epsilon float64) bool

AnyKeyValuePairInMapIntFloat64 checks if any key-value pair from one map[int]float64 is in another map[int]float64. When any of the maps is empty, the function returns false. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(AnyKeyValuePairInMapIntFloat64(
	map[int]float64{1: .1, 2: .2},
	map[int]float64{1: .1, 2: 2.2},
	0,
))
fmt.Println(AnyKeyValuePairInMapIntFloat64(
	map[int]float64{1: .11, 2: 2.2},
	map[int]float64{1: .1, 2: .2},
	0,
))
fmt.Println(AnyKeyValuePairInMapIntFloat64(
	map[int]float64{1: .11, 2: .2},
	map[int]float64{1: .1, 2: 2.2},
	.01,
))
Output:

true
false
true

func AnyKeyValuePairInMapIntInt

func AnyKeyValuePairInMapIntInt(Map1, Map2 map[int]int) bool

AnyKeyValuePairInMapIntInt checks if any key-value pair from one map[int]int is in another map[int]int. When any of the maps is empty, the function returns false.

Example
fmt.Println(AnyKeyValuePairInMapIntInt(
	map[int]int{1: 1, 2: 2},
	map[int]int{1: 1, 2: 23},
))
fmt.Println(AnyKeyValuePairInMapIntInt(
	map[int]int{1: 1, 2: 2},
	map[int]int{1: 21, 3: 3, 2: 40},
))
Output:

true
false

func AnyKeyValuePairInMapIntString

func AnyKeyValuePairInMapIntString(Map1, Map2 map[int]string) bool

AnyKeyValuePairInMapIntString checks if any key-value pair from one map[int]string is in another map[int]string. When any of the maps is empty, the function returns false.

Example
fmt.Println(AnyKeyValuePairInMapIntString(
	map[int]string{1: "a", 2: "b"},
	map[int]string{1: "a", 3: "c", 2: "X"},
))
fmt.Println(AnyKeyValuePairInMapIntString(
	map[int]string{1: "a", 2: "b"},
	map[int]string{1: "B", 3: "c", 2: "B"},
))
Output:

true
false

func AnyKeyValuePairInMapStringFloat64

func AnyKeyValuePairInMapStringFloat64(Map1, Map2 map[string]float64, Epsilon float64) bool

AnyKeyValuePairInMapStringFloat64 checks if any key-value pair from one map[string]float64 is in another map[string]float64. When any of the maps is empty, the function returns false. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(AnyKeyValuePairInMapStringFloat64(
	map[string]float64{"1": .1, "2": .2},
	map[string]float64{"1": .1, "2": 2.2},
	0,
))
fmt.Println(AnyKeyValuePairInMapStringFloat64(
	map[string]float64{"1": .11, "2": 2.2},
	map[string]float64{"1": .1, "2": .2},
	0,
))
fmt.Println(AnyKeyValuePairInMapStringFloat64(
	map[string]float64{"1": .11, "2": .2},
	map[string]float64{"1": .1, "2": 2.2},
	.01,
))
Output:

true
false
true

func AnyKeyValuePairInMapStringInt

func AnyKeyValuePairInMapStringInt(Map1, Map2 map[string]int) bool

AnyKeyValuePairInMapStringInt checks if any key-value pair from one map[string]int is in another map[string]int. When any of the maps is empty, the function returns false.

Example
fmt.Println(AnyKeyValuePairInMapStringInt(
	map[string]int{"1": 1, "2": 2},
	map[string]int{"1": 1, "2": 23},
))
fmt.Println(AnyKeyValuePairInMapStringInt(
	map[string]int{"1": 1, "2": 2},
	map[string]int{"1": 21, "3": 3, "2": 40},
))
Output:

true
false

func AnyKeyValuePairInMapStringString

func AnyKeyValuePairInMapStringString(Map1, Map2 map[string]string) bool

AnyKeyValuePairInMapStringString checks if any key-value pair from one map[string]string is in another map[string]string. When any of the maps is empty, the function returns false.

Example
fmt.Println(AnyKeyValuePairInMapStringString(
	map[string]string{"1": "a", "2": "b"},
	map[string]string{"1": "a", "3": "c", "2": "X"},
))
fmt.Println(AnyKeyValuePairInMapStringString(
	map[string]string{"1": "a", "2": "b"},
	map[string]string{"1": "B", "3": "c", "2": "B"},
))
Output:

true
false

func AnyValueInFloat64Slice

func AnyValueInFloat64Slice(Slice1, Slice2 []float64, Epsilon float64) bool

AnyValueInFloat64Slice checks if any of the values of one float64 slice is in another slice. The Epsilon parameter sets the accuracy of the comparison of two floats. When the first or the second (or both) slice is empty, it returns false.

Example
fmt.Println(AnyValueInFloat64Slice([]float64{10, 20, 70}, []float64{10, 20}, .00000001))
fmt.Println(AnyValueInFloat64Slice([]float64{.1, .2, .2}, []float64{.3, .11, .3}, .00000001))
fmt.Println(AnyValueInFloat64Slice([]float64{.1, .2, .2}, []float64{.3, .11, .3}, .1))
Output:

true
false
true

func AnyValueInIntSlice

func AnyValueInIntSlice(Slice1, Slice2 []int) bool

AnyValueInIntSlice checks if any of the values of one int slice is in another slice. When the first or the second (or both) slice is empty, it returns false.

Example
fmt.Println(AnyValueInIntSlice([]int{10, 20}, []int{10, 30, 50}))
fmt.Println(AnyValueInIntSlice([]int{10, 11, 15, 10, 2000}, []int{12, 22}))
fmt.Println(AnyValueInIntSlice([]int{10, 10, 10}, []int{20, 20, 10}))
Output:

true
false
true

func AnyValueInMapIntFloat64

func AnyValueInMapIntFloat64(Slice []float64, Map map[int]float64, Epsilon float64) bool

AnyValueInMapIntFloat64 checks if any of the values of a float64 slice is in map[int]float64. When either the slice or the map is empty, it returns false. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(AnyValueInMapIntFloat64([]float64{.001, .02}, map[int]float64{1: .0011, 2: .021}, .01))
fmt.Println(AnyValueInMapIntFloat64([]float64{.001, .02}, map[int]float64{1: .0011, 2: .021}, 0))
Output:

true
false

func AnyValueInMapIntInt

func AnyValueInMapIntInt(Slice []int, Map map[int]int) bool

AnyValueInMapIntInt checks if any of the values of an int slice is a value of map[int]int. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AnyValueInMapIntInt([]int{10, 20, 70}, map[int]int{1: 10, 2: 20}))
fmt.Println(AnyValueInMapIntInt([]int{10, 20, 70}, map[int]int{1: 30, 2: 40}))
fmt.Println(AnyValueInMapIntInt([]int{1, 2, 7000}, map[int]int{1: 10, 2: 10, 3: 1, 5: 1, 100: 7000}))
Output:

true
false
true

func AnyValueInMapIntString

func AnyValueInMapIntString(Slice []string, Map map[int]string) bool

AnyValueInMapIntString checks if any of the values of a string slice is a value of map[int]string. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AnyValueInMapIntString([]string{"a", "b"}, map[int]string{1: "v", 2: "g"}))
fmt.Println(AnyValueInMapIntString([]string{"a", "b"}, map[int]string{1: "a", 2: "g"}))
Output:

false
true

func AnyValueInMapStringFloat64

func AnyValueInMapStringFloat64(Slice []float64, Map map[string]float64, Epsilon float64) bool

AnyValueInMapStringFloat64 checks if any of the values of a float64 slice is a value of map[string]float64. The Epsilon parameter sets the accuracy of the comparison of two floats. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AnyValueInMapStringFloat64([]float64{.01, .2}, map[string]float64{"1": .01, "2": .2}, 0))
fmt.Println(AnyValueInMapStringFloat64([]float64{.001, .02, 70.8}, map[string]float64{"1": .0011, "2": .021}, .01))
fmt.Println(AnyValueInMapStringFloat64([]float64{.01, .02, 70.8}, map[string]float64{"1": .0111, "2": .0211}, .001))
Output:

true
true
false

func AnyValueInMapStringInt

func AnyValueInMapStringInt(Slice []int, Map map[string]int) bool

AnyValueInMapStringInt checks if any of the values of a string slice is a value of map[string]int. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AnyValueInMapStringInt([]int{10, 20, 70}, map[string]int{"1": 10, "2": 20}))
fmt.Println(AnyValueInMapStringInt([]int{10, 20, 70}, map[string]int{"1": 30, "2": 40}))
fmt.Println(AnyValueInMapStringInt([]int{1, 2, 7000}, map[string]int{"1": 10, "2": 10, "3": 1, "5": 1, "100": 7000}))
Output:

true
false
true

func AnyValueInMapStringString

func AnyValueInMapStringString(Slice []string, Map map[string]string) bool

AnyValueInMapStringString checks if any of the values of a string slice is a value of map[string]string. When either the slice or the map is empty, it returns false.

Example
fmt.Println(AnyValueInMapStringString([]string{"a", "b", "c"}, map[string]string{"1": "a", "2": "b"}))
fmt.Println(AnyValueInMapStringString([]string{"a", "b", "c"}, map[string]string{"1": "nothing", "2": "else", "3": "matters"}))
fmt.Println(AnyValueInMapStringString([]string{"a", "b", "z"}, map[string]string{"1": "a", "2": "b", "3": "haha", "5": "z", "100": "Zigi says hi!"}))
Output:

true
false
true

func AnyValueInStringSlice

func AnyValueInStringSlice(Slice1, Slice2 []string) bool

AnyValueInStringSlice checks if any of the values of one string slice is in another slice. When the first or the second (or both) slice is empty, it returns false.

Example
fmt.Println(AnyValueInStringSlice([]string{"10"}, []string{"10", "20"}))
fmt.Println(AnyValueInStringSlice([]string{"10 ", "21", "31"}, []string{"10", "20", "30"}))
fmt.Println(AnyValueInStringSlice([]string{"10 ", "21", "31"}, []string{"10 ", "20", "30"}))
Output:

true
false
true

func AreEqualMapsIntFloat64

func AreEqualMapsIntFloat64(Map1, Map2 map[int]float64, Epsilon float64) bool

AreEqualMapsIntFloat64 compares two maps map[int]float64. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(AreEqualMapsIntFloat64(
	map[int]float64{1: 1.01, 2: 100.001, 3: 1.},
	map[int]float64{1: 1.1, 3: 1., 2: 100.001},
	.00000001,
))
fmt.Println(AreEqualMapsIntFloat64(
	map[int]float64{1: 1.01, 2: 100.001, 3: 1.},
	map[int]float64{1: 1.1, 3: 1., 2: 100.001},
	.1,
))
fmt.Println(AreEqualMapsIntFloat64(
	map[int]float64{1: 1.01, 2: 100.001, 3: 1.},
	map[int]float64{1: 1.01, 3: 1., 2: 100.001},
	0,
))
Output:

false
true
true

func AreEqualMapsIntInt

func AreEqualMapsIntInt(Map1, Map2 map[int]int) bool

AreEqualMapsIntInt compares two maps map[int]int.

Example
fmt.Println(AreEqualMapsIntInt(
	map[int]int{1: 1, 2: 100, 3: 1},
	map[int]int{1: 1, 3: 1, 2: 100},
))
fmt.Println(AreEqualMapsIntInt(
	map[int]int{1: 1, 2: 100, 3: 1},
	map[int]int{1: 1, 3: 1, 2: 101},
))
Output:

true
false

func AreEqualMapsIntString

func AreEqualMapsIntString(Map1, Map2 map[int]string) bool

AreEqualMapsIntString compares two maps map[int]string.

Example
fmt.Println(AreEqualMapsIntString(
	map[int]string{1: "1", 2: "101", 3: "1"},
	map[int]string{1: "1", 3: "1", 2: "101"},
))
fmt.Println(AreEqualMapsIntString(
	map[int]string{1: "1", 2: "100", 3: "|"},
	map[int]string{1: "1", 3: "1", 2: "101"},
))
Output:

true
false

func AreEqualMapsStringFloat64

func AreEqualMapsStringFloat64(Map1, Map2 map[string]float64, Epsilon float64) bool

AreEqualMapsStringFloat64 compares two maps map[string]float64. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(AreEqualMapsStringFloat64(
	map[string]float64{"1": 1.01, "2": 100.001, "3": 1.},
	map[string]float64{"1": 1.1, "3": 1., "2": 100.001},
	.00000001,
))
fmt.Println(AreEqualMapsStringFloat64(
	map[string]float64{"1": 1.01, "2": 100.001, "3": 1.},
	map[string]float64{"1": 1.1, "3": 1., "2": 100.001},
	.1,
))
fmt.Println(AreEqualMapsStringFloat64(
	map[string]float64{"1": 1.01, "2": 100.001, "3": 1.},
	map[string]float64{"1": 1.01, "3": 1., "2": 100.001},
	0,
))
Output:

false
true
true

func AreEqualMapsStringInt

func AreEqualMapsStringInt(Map1, Map2 map[string]int) bool

AreEqualMapsStringInt compares two maps map[string]int.

Example
fmt.Println(AreEqualMapsStringInt(
	map[string]int{"1": 1, "2": 100, "3": 1},
	map[string]int{"1": 1, "3": 1, "2": 100},
))
fmt.Println(AreEqualMapsStringInt(
	map[string]int{"1": 1, "2": 100, "3": 1},
	map[string]int{"1": 1, "3": 1, "2": 101},
))
Output:

true
false

func AreEqualMapsStringString

func AreEqualMapsStringString(Map1, Map2 map[string]string) bool

AreEqualMapsStringString compares two maps map[string]string.

Example
fmt.Println(AreEqualMapsStringString(
	map[string]string{"1": "1", "2": "100", "3": "1"},
	map[string]string{"1": "1", "3": "1", "2": "100"},
))
fmt.Println(AreEqualMapsStringString(
	map[string]string{"1": "1", "2": "100", "3": "|"},
	map[string]string{"1": "1", "3": "1", "2": "101"},
))
Output:

true
false

func AreEqualSlicesFloat64

func AreEqualSlicesFloat64(Slice1, Slice2 []float64, Epsilon float64) bool

AreEqualSlicesFloat64 compares two float64 slices. The Epsilon parameter sets the accuracy of the comparison of two floats. The function ignores sorting, so compares both values and sorting of the slices. Thus, if the slices have the same values but different orders, they are not considered the same. When both slices has zero length, true is returned. If you want the function to sort the slices first, use AreEqualSortedSlicesFloat64.

Example
fmt.Println(AreEqualSlicesFloat64([]float64{.55, .55, .55}, []float64{.55, .55, .54}, 0))
fmt.Println(AreEqualSlicesFloat64([]float64{.55, .55, .55}, []float64{.55, .55, .54}, .1))
fmt.Println(AreEqualSlicesFloat64([]float64{.55, .55, .55}, []float64{.55, .55001, .55}, .000001))
Output:

false
true
false

func AreEqualSlicesInt

func AreEqualSlicesInt(Slice1, Slice2 []int) bool

AreEqualSlicesInt compares two int slices. The function ignores sorting, so compares both values and sorting of the slices. If you want the function to sort the slices first, use AreEqualSortedSlicesInt instead. When both slices has zero length, true is returned.

Example
fmt.Println(AreEqualSlicesInt([]int{55, 55, 45}, []int{55, 55, 45}))
fmt.Println(AreEqualSlicesInt([]int{55, 55, 45}, []int{55, 45, 55}))
Output:

true
false

func AreEqualSlicesString

func AreEqualSlicesString(Slice1, Slice2 []string) bool

AreEqualSlicesString compares two string slices. The function ignores sorting, so compares both values and sorting of the slices. If you want the function to sort the slices, use AreEqualSortedSlicesString instead. When both slices has zero length, true is returned.

Example
fmt.Println(AreEqualSlicesString([]string{"a", "a", "b"}, []string{"a", "b"}))
fmt.Println(AreEqualSlicesString([]string{"a", "a", "b"}, []string{"a", "a", "b"}))
fmt.Println(AreEqualSlicesString([]string{"a", "a", "b"}, []string{"a", "b", "a"}))
Output:

false
true
false

func AreEqualSortedSlicesFloat64

func AreEqualSortedSlicesFloat64(Slice1, Slice2 []float64, Epsilon float64) bool

AreEqualSortedSlicesFloat64 compares two float64 slices. The Epsilon parameter sets the accuracy of the comparison of two floats. The function takes into account sorting, meaning that if they have the same values, they are considered the same even if they are differently ordered. When both slices has zero length, true is returned.

Example
fmt.Println(AreEqualSortedSlicesFloat64([]float64{.55, .51, .55}, []float64{.55, .55, .51}, 0))
fmt.Println(AreEqualSortedSlicesFloat64([]float64{.55, .51, .55}, []float64{.55, .55, .52}, .0001))
Output:

true
false

func AreEqualSortedSlicesInt

func AreEqualSortedSlicesInt(Slice1, Slice2 []int) bool

AreEqualSortedSlicesInt compares two int slices. The function takes into account sorting, meaning that if they have the same values, they are considered the same even if they are differently ordered. When both slices has zero length, true is returned.

Example
fmt.Println(AreEqualSortedSlicesInt([]int{55, 55, 45}, []int{55, 45, 55}))
fmt.Println(AreEqualSortedSlicesInt([]int{1, 2, 3}, []int{3, 2, 1}))
Output:

true
true

func AreEqualSortedSlicesString

func AreEqualSortedSlicesString(Slice1, Slice2 []string) bool

AreEqualSortedSlicesString compares two string slices. The function takes into account sorting, meaning that if they have the same values, they are considered the same even if they are differently ordered. When both slices has zero length, true is returned.

Example
fmt.Println(AreEqualSortedSlicesString([]string{"a", "a", "b"}, []string{"a", "a", "b"}))
fmt.Println(AreEqualSortedSlicesString([]string{"a", "a", "b"}, []string{"a", "b", "a"}))
Output:

true
true

func IsUnique

func IsUnique(Slice interface{}) bool

IsUnique checks whether a slice ([]int, []string and []float64) is unique (so it does not contain duplicated elements). Note that if you're using it for a float slice, the comparisons are made with Epsilon set to 0. If you need to use a different level of accuracy, use IsUniqueFloat64 instead.

func IsUniqueFloat64Slice

func IsUniqueFloat64Slice(Slice []float64, Epsilon float64) bool

IsUniqueFloat64Slice checks if all elements of a float64 slice are unique (so the slice does not contain duplicated elements). The Epsilon parameter sets the accuracy of the comparison of two floats. If the slice has no elements, the function returns true (since it does not contain duplicated elements).

Example
fmt.Println(IsUniqueFloat64Slice([]float64{.551, .552, .553}, .0000001))
// Watch for floats! The below example shows this:
fmt.Println(IsUniqueFloat64Slice([]float64{.551, .552, .553}, .001))
// But
fmt.Println(IsUniqueFloat64Slice([]float64{.551, .552, .553}, .0011))
Output:

true
true
false

func IsUniqueIntSlice

func IsUniqueIntSlice(Slice []int) bool

IsUniqueIntSlice checks if all elements of an int slice are unique (so the slice does not contain duplicated elements). If the slice has no elements, the function returns true (since it does not contain duplicated elements).

Example
fmt.Println(IsUniqueIntSlice([]int{55, 55, 55}))
fmt.Println(IsUniqueIntSlice([]int{55, 56, 57}))
Output:

false
true

func IsUniqueMapIntFloat64

func IsUniqueMapIntFloat64(Map map[int]float64, Epsilon float64) bool

IsUniqueMapIntFloat64 checks if all values of map[int]float64 are unique (so the map does not contain duplicated elements). The Epsilon parameter sets the accuracy of the comparison of two floats. Note that uniqueness here means the equality of values, since a map will never be unique in terms of key-value pairs.

Example
fmt.Println(IsUniqueMapIntFloat64(map[int]float64{1: .1, 2: .11, 3: .101, 100: .1105}, 0))
fmt.Println(IsUniqueMapIntFloat64(map[int]float64{1: .1, 2: .11, 3: .101, 100: .1105}, .1))
Output:

true
false

func IsUniqueMapIntInt

func IsUniqueMapIntInt(Map map[int]int) bool

IsUniqueMapIntInt checks if all values of map[int]int are unique (so the map does not contain duplicated elements). Note that uniqueness here means the equality of values, since a map will never be unique in terms of key-value pairs.

Example
fmt.Println(IsUniqueMapIntInt(map[int]int{1: 1, 2: 2, 3: 3, 100: 4}))
fmt.Println(IsUniqueMapIntInt(map[int]int{1: 1, 2: 1, 3: 1, 100: 1}))
Output:

true
false

func IsUniqueMapIntString

func IsUniqueMapIntString(Map map[int]string) bool

IsUniqueMapIntString checks if all values of map[int]string are unique (so the map does not contain duplicated elements). Note that uniqueness here means the equality of values, since a map will never be unique in terms of key-value pairs.

Example
fmt.Println(IsUniqueMapIntString(map[int]string{1: "a", 2: "a", 3: "a", 100: "a"}))
fmt.Println(IsUniqueMapIntString(map[int]string{1: "a", 2: "b", 3: "B", 100: "A"}))
Output:

false
true

func IsUniqueMapStringFloat64

func IsUniqueMapStringFloat64(Map map[string]float64, Epsilon float64) bool

IsUniqueMapStringFloat64 checks if all values of map[string]float64 are unique (so the map does not contain duplicated elements). The Epsilon parameter sets the accuracy of the comparison of two floats. Note that uniqueness here means the equality of values, since a map will never be unique in terms of key-value pairs.

Example
fmt.Println(IsUniqueMapStringFloat64(map[string]float64{"a": .1, "b": .11, "c": .101, "Zigi says hi!": .1105}, 0))
fmt.Println(IsUniqueMapStringFloat64(map[string]float64{"a": .1, "b": .11, "c": .101, "Zigi says hi!": .1105}, .1))
Output:

true
false

func IsUniqueMapStringInt

func IsUniqueMapStringInt(Map map[string]int) bool

IsUniqueMapStringInt checks if all values of map[string]int are unique (so the map does not contain duplicated elements). Note that uniqueness here means the equality of values, since a map will never be unique in terms of key-value pairs.

Example
fmt.Println(IsUniqueMapStringInt(
	map[string]int{"a": 1, "b": 21, "c": 11, "Blues is my soul, blues is my heart": 2},
))
fmt.Println(IsUniqueMapStringInt(
	map[string]int{"a": 1, "b": 1, "c": 1, "Blues is my soul, blues is my heart": 1},
))
Output:

true
false

func IsUniqueMapStringString

func IsUniqueMapStringString(Map map[string]string) bool

IsUniqueMapStringString checks if all values of map[int]string are unique (so the map does not contain duplicated elements). Note that uniqueness here means the equality of values, since a map will never be unique in terms of key-value pairs.

Example
fmt.Println(IsUniqueMapStringString(
	map[string]string{"key1": "a", "key2": "a", "key3": "Q"},
))
fmt.Println(IsUniqueMapStringString(
	map[string]string{"key1": "a", "key2": "Q", "key3": "A"},
))
Output:

false
true

func IsUniqueStringSlice

func IsUniqueStringSlice(Slice []string) bool

IsUniqueStringSlice checks if all elements in a string slice are unique (so the slice does not contain duplicated elements). If the slice has no elements, the function returns true (since it does not contain duplicated elements).

Example
fmt.Println(IsUniqueStringSlice([]string{"a", "a", "a"}))
fmt.Println(IsUniqueStringSlice([]string{"a", "b", "c"}))
Output:

false
true

func IsValueInFloat64Slice

func IsValueInFloat64Slice(X float64, Slice []float64, Epsilon float64) bool

IsValueInFloat64Slice checks if a float64 (X) is in a slice. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
fmt.Println(IsValueInFloat64Slice(.001, []float64{.001, .001, .001}, 0))
fmt.Println(IsValueInFloat64Slice(.001, []float64{.002, .011, .002}, .0001))
fmt.Println(IsValueInFloat64Slice(.001, []float64{.002, .011, .002}, .1))
Output:

true
false
true

func IsValueInIntSlice

func IsValueInIntSlice(X int, Slice []int) bool

IsValueInIntSlice checks if an int (X) is in a slice.

Example
fmt.Println(IsValueInIntSlice(1, []int{1, 2, 3}))
fmt.Println(IsValueInIntSlice(1, []int{2, 2, 3}))
Output:

true
false

func IsValueInMapIntFloat64

func IsValueInMapIntFloat64(X float64, Map map[int]float64, Epsilon float64) ([]int, bool)

IsValueInMapIntFloat64 checks if a float64 (X) is among the map's values. The Epsilon parameter sets the accuracy of the comparison of two floats. Returns a tuple of slice providing the map's keys that have this value, and a bool value (true if the returned slice is not empty).

Example
keys, ok := IsValueInMapIntFloat64(.01, map[int]float64{1: .01, 2: .02, 3: .02}, 0)
fmt.Println(ok, len(keys))
keys, ok = IsValueInMapIntFloat64(.01, map[int]float64{1: .011, 2: .02, 3: .02}, 0)
fmt.Println(ok, len(keys))
keys, ok = IsValueInMapIntFloat64(.01, map[int]float64{1: .011, 2: .02, 3: .02}, 0.1)
fmt.Println(ok, len(keys))
Output:

true 1
false 0
true 3

func IsValueInMapIntInt

func IsValueInMapIntInt(X int, Map map[int]int) ([]int, bool)

IsValueInMapIntInt checks if an int (X) is among the map's values. Returns a tuple of slice providing the map's keys that have this value, and a bool value (true if the returned slice is not empty).

Example
fmt.Println(IsValueInMapIntInt(2, map[int]int{1: 1, 2: 1, 3: 2}))
fmt.Println(IsValueInMapIntInt(3, map[int]int{1: 1, 2: 1, 3: 2}))
Output:

[3] true
[] false

func IsValueInMapIntString

func IsValueInMapIntString(X string, Map map[int]string) ([]int, bool)

IsValueInMapIntString checks if an int (X) is among the map's values. Returns a tuple of slice providing the map's keys that have this value, and a bool value (true if the returned slice is not empty).

Example
keys, ok := IsValueInMapIntString("a", map[int]string{1: "a", 2: "b", 3: "a"})
fmt.Println(ok, len(keys))
keys, ok = IsValueInMapIntString("a", map[int]string{1: "c", 2: "b", 3: "d"})
fmt.Println(ok, len(keys))
Output:

true 2
false 0

func IsValueInMapStringFloat64

func IsValueInMapStringFloat64(X float64, Map map[string]float64, Epsilon float64) ([]string, bool)

IsValueInMapStringFloat64 checks if a foat64 (X) is among the map's values. The Epsilon parameter sets the accuracy of the comparison of two floats. Returns a tuple of slice providing the map's keys that have this value, and a bool value (true if the returned slice is not empty).

Example
fmt.Println(IsValueInMapStringFloat64(.01, map[string]float64{"a": .01, "b": .02, "c": .02}, 0))
fmt.Println(IsValueInMapStringFloat64(.01, map[string]float64{"a": .011, "b": .02, "c": .02}, 0))
keys, ok := IsValueInMapStringFloat64(.01, map[string]float64{"a": .011, "b": .02, "c": .02}, 0.1)
fmt.Println(ok, len(keys))
Output:

[a] true
[] false
true 3

func IsValueInMapStringInt

func IsValueInMapStringInt(X int, Map map[string]int) ([]string, bool)

IsValueInMapStringInt checks if an int (X) is among the map's values. Returns a tuple of slice providing the map's keys that have this value, and a bool value (true if the returned slice is not empty).

Example
fmt.Println(IsValueInMapStringInt(2, map[string]int{"a": 1, "b": 1, "c": 2}))
fmt.Println(IsValueInMapStringInt(3, map[string]int{"a": 1, "b": 1, "c": 2}))
Output:

[c] true
[] false

func IsValueInMapStringString

func IsValueInMapStringString(X string, Map map[string]string) ([]string, bool)

IsValueInMapStringString checks if a string (X) is among the map's values. Returns a tuple of slice providing the map's keys that have this value, and a bool value (true if the returned slice is not empty).

Example
fmt.Println(IsValueInMapStringString("c", map[string]string{"a": "a", "b": "b", "c": "c"}))
fmt.Println(IsValueInMapStringString("d", map[string]string{"a": "a", "b": "b", "c": "c"}))
Output:

[c] true
[] false

func IsValueInStringSlice

func IsValueInStringSlice(X string, Slice []string) bool

IsValueInStringSlice checks if a string (X) is in a slice.

Example
fmt.Println(IsValueInStringSlice("a", []string{"a", "b", "c"}))
fmt.Println(IsValueInStringSlice("A", []string{"a", "b", "c"}))
Output:

true
false

func UniqueFloat64Slice

func UniqueFloat64Slice(Slice []float64, Epsilon float64) []float64

UniqueFloat64Slice returns a slice with unique elements of a float64 slice. The Epsilon parameter sets the accuracy of the comparison of two floats. If the slice has no elements, the function returns an empty slice.

Example
fmt.Println(UniqueFloat64Slice([]float64{1., 1.01, 2}, 0))
fmt.Println(UniqueFloat64Slice([]float64{1., 1.01, 2}, .1))
Output:

[1 1.01 2]
[1 2]

func UniqueIntSlice

func UniqueIntSlice(Slice []int) []int

UniqueIntSlice returns a slice with unique elements of an int slice. If the slice has no elements, the function returns an empty slice.

Example
fmt.Println(UniqueIntSlice([]int{1, 1, 2}))
fmt.Println(UniqueIntSlice([]int{1, 1, 12, 1, 1, 12, 1}))
Output:

[1 2]
[1 12]

func UniqueStringSlice

func UniqueStringSlice(Slice []string) []string

UniqueStringSlice returns a slice with unique elements of a string slice. If the slice has no elements, the function returns an empty slice.

Example
fmt.Println(UniqueStringSlice([]string{"a", "a", "a"}))
fmt.Println(UniqueStringSlice([]string{"a", "b", "a"}))
Output:

[a]
[a b]

func WhichInMapInt

func WhichInMapInt(Conditions map[int]bool) ([]int, bool)

WhichInMapInt checks which of the map's values is true. Returns a tuple of true/false, keys, where keys are the keys with true.

Example
keys, ok := WhichInMapInt(map[int]bool{1: true, 2: false})
fmt.Println(ok, keys)
keys, ok = WhichInMapInt(map[int]bool{1: false, 2: false})
fmt.Println(ok, keys)
Output:

true [1]
false []

func WhichInMapString

func WhichInMapString(Conditions map[string]bool) ([]string, bool)

WhichInMapString checks which of the map's values is true. Returns a tuple of true/false, keys, where keys are the keys with true.

Example
keys, ok := WhichInMapString(map[string]bool{"key1": true, "key2": false})
fmt.Println(ok, keys)
keys, ok = WhichInMapString(map[string]bool{"key1": false, "key2": false})
fmt.Println(ok, keys)
Output:

true [key1]
false []

func WhichKeyValuePairsInMapIntFloat64

func WhichKeyValuePairsInMapIntFloat64(Map1, Map2 map[int]float64, Epsilon float64) (map[int]float64, bool)

WhichKeyValuePairsInMapIntFloat64 checks if any key-value pair from one map[int]float64 is in another map[int]float64. Returns a tuple of a map with the found key-value pairs, and true if the map is not empty. When any of the maps is empty, the function returns empty slice and false. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
keys, ok := WhichKeyValuePairsInMapIntFloat64(
	map[int]float64{1: .1, 2: .2},
	map[int]float64{1: .1, 2: 2.2},
	0,
)
fmt.Println(ok, keys)
keys, ok = WhichKeyValuePairsInMapIntFloat64(
	map[int]float64{1: .11, 2: 2.2},
	map[int]float64{1: .1, 2: .2},
	0,
)
fmt.Println(ok, keys)
keys, ok = WhichKeyValuePairsInMapIntFloat64(
	map[int]float64{1: .11, 2: .2},
	map[int]float64{1: .1, 2: 2.2},
	.01,
)
fmt.Println(ok, keys)
Output:

true map[1:0.1]
false map[]
true map[1:0.11]

func WhichKeyValuePairsInMapIntInt

func WhichKeyValuePairsInMapIntInt(Map1, Map2 map[int]int) (map[int]int, bool)

WhichKeyValuePairsInMapIntInt checks if any key-value pair from one map[int]int is in another map[int]int. Returns a tuple of a map with the found key-value pairs, and true if the map is not empty. When any of the maps is empty, the function returns empty slice and false.

Example
keys, ok := WhichKeyValuePairsInMapIntInt(
	map[int]int{1: 1, 2: 2},
	map[int]int{1: 1, 2: 23},
)
fmt.Println(ok, keys)
keys, ok = WhichKeyValuePairsInMapIntInt(
	map[int]int{1: 1, 2: 2},
	map[int]int{1: 21, 3: 3, 2: 40},
)
fmt.Println(ok, keys)
Output:

true map[1:1]
false map[]

func WhichKeyValuePairsInMapIntString

func WhichKeyValuePairsInMapIntString(Map1, Map2 map[int]string) (map[int]string, bool)

WhichKeyValuePairsInMapIntString checks if any key-value pair from one map[int]string is in another map[int]string. Returns a tuple of a map with the found key-value pairs, and true if the map is not empty. When any of the maps is empty, the function returns empty slice and false.

Example
keys, ok := WhichKeyValuePairsInMapIntString(
	map[int]string{1: "a", 2: "b"},
	map[int]string{1: "a", 3: "c", 2: "b"},
)
fmt.Println(ok, keys)
keys, ok = WhichKeyValuePairsInMapIntString(
	map[int]string{1: "a", 2: "b"},
	map[int]string{1: "a", 3: "c", 2: "B"},
)
fmt.Println(ok, keys)
Output:

true map[1:a 2:b]
true map[1:a]

func WhichKeyValuePairsInMapStringFloat64

func WhichKeyValuePairsInMapStringFloat64(Map1, Map2 map[string]float64, Epsilon float64) (map[string]float64, bool)

WhichKeyValuePairsInMapStringFloat64 checks if any key-value pair from one map[string]float64 is in another map[string]float64. Returns a tuple of a map with the found key-value pairs, and true if the map is not empty. When any of the maps is empty, the function returns empty slice and false. The Epsilon parameter sets the accuracy of the comparison of two floats.

Example
keys, ok := WhichKeyValuePairsInMapStringFloat64(
	map[string]float64{"1": .1, "2": .2},
	map[string]float64{"1": .1, "2": 2.2},
	0,
)
fmt.Println(ok, keys)
keys, ok = WhichKeyValuePairsInMapStringFloat64(
	map[string]float64{"1": .11, "2": 2.2},
	map[string]float64{"1": .1, "2": .2},
	0,
)
fmt.Println(ok, keys)
keys, ok = WhichKeyValuePairsInMapStringFloat64(
	map[string]float64{"1": .11, "2": .2},
	map[string]float64{"1": .1, "2": 2.2},
	.01,
)
fmt.Println(ok, keys)
Output:

true map[1:0.1]
false map[]
true map[1:0.11]

func WhichKeyValuePairsInMapStringInt

func WhichKeyValuePairsInMapStringInt(Map1, Map2 map[string]int) (map[string]int, bool)

WhichKeyValuePairsInMapStringInt checks which key-value pair from one map[string]int is in another map[string]int. Returns a tuple of a map with the found key-value pairs, and true if the map is not empty. When any of the maps is empty, the function returns empty slice and false.

Example
fmt.Println(WhichKeyValuePairsInMapStringInt(
	map[string]int{"1": 1, "2": 2},
	map[string]int{"1": 1, "2": 23},
))
fmt.Println(WhichKeyValuePairsInMapStringInt(
	map[string]int{"1": 1, "2": 2},
	map[string]int{"1": 21, "3": 3, "2": 40},
))
Output:

map[1:1] true
map[] false

func WhichKeyValuePairsInMapStringString

func WhichKeyValuePairsInMapStringString(Map1, Map2 map[string]string) (map[string]string, bool)

WhichKeyValuePairsInMapStringString checks which key-value pairs from one map[string]string is in another map[string]string. Returns a tuple of a map with the found key-value pairs, and true if the map is not empty. When any of the maps is empty, the function returns empty slice and false.

Example
keys, ok := WhichKeyValuePairsInMapStringString(
	map[string]string{"1": "a", "2": "b"},
	map[string]string{"1": "a", "3": "c", "2": "X"},
)
fmt.Println(ok, keys)
keys, ok = WhichKeyValuePairsInMapStringString(
	map[string]string{"1": "a", "2": "b"},
	map[string]string{"1": "B", "3": "c", "2": "B"},
)
fmt.Println(ok, keys)
Output:

true map[1:a]
false map[]

func WhichValuesInFloat64Slice

func WhichValuesInFloat64Slice(Slice1, Slice2 []float64, Epsilon float64) (map[float64][]int, bool)

WhichValuesInFloat64Slice checks which values of one float64 slice are in another slice. The Epsilon parameter sets the accuracy of the comparison of two floats. When the first or the second (or both) slice is empty, it returns false. The function returns a tuple with a map with values from Slice1 as keys and their indices from Slice2 as the map's values,// and a boolean value (true if the returned map is not empty).

Example
values, ok := WhichValuesInFloat64Slice([]float64{.10, .20, .70}, []float64{.10, .20}, 0)
fmt.Println(ok, values)
values, ok = WhichValuesInFloat64Slice([]float64{.1, .2, .2}, []float64{.3, .11, .3}, .00000001)
fmt.Println(ok, values)
values, ok = WhichValuesInFloat64Slice([]float64{.10, .20, .30, .101}, []float64{.10, .101}, .1)
fmt.Println(ok, values)
Output:

true map[0.1:[0] 0.2:[1]]
false map[]
true map[0.1:[0 1]]

func WhichValuesInIntSlice

func WhichValuesInIntSlice(Slice1, Slice2 []int) (map[int][]int, bool)

WhichValuesInIntSlice checks which values of one int slice are in another slice. When the first or the second (or both) slice is empty, it returns false and an empty map. The function returns a tuple with a map with values from Slice1 as keys and their indices from Slice2 as the map's values, and a boolean value (true if the returned map is not empty).

Example
values, ok := WhichValuesInIntSlice([]int{10, 20}, []int{10, 10, 50})
fmt.Println(ok, values)
values, ok = WhichValuesInIntSlice([]int{10, 11, 15, 10, 2000}, []int{12, 22})
fmt.Println(ok, values)
values, ok = WhichValuesInIntSlice([]int{10, 10, 10, 20}, []int{20, 20, 10})
fmt.Println(ok, values)
Output:

true map[10:[0 1]]
false map[]
true map[10:[2] 20:[0 1]]

func WhichValuesInMapIntFloat64

func WhichValuesInMapIntFloat64(Slice []float64, Map map[int]float64, Epsilon float64) (map[float64][]int, bool)

WhichValuesInMapIntFloat64 checks which values of a float64 slice are values of map[int]float64. When either the slice or the map is empty, it returns false. The Epsilon parameter sets the accuracy of the comparison of two floats. The function returns a tuple with a map with values from Slice as keys and slices of keys in the map that have these values, and a boolean value (true if the returned map is not empty). BEWARE! Do note that we work with floats, so it's safest to round them before using this function, since they will be keys of a returned map. Thus, this WhichValuesInMapIntFloat64({[]float64{.01002, .01, .2}, map[int]float64{1: .01, 2: .011}, .0001) will return the following map: map[float64][]int{.01: {1}, .01002: {1}}}

Example
values, ok := WhichValuesInMapIntFloat64([]float64{.01, .2}, map[int]float64{1: .01, 2: .2}, 0)
fmt.Println(ok, values)
// Watch this!
values, ok = WhichValuesInMapIntFloat64([]float64{.01002, .01, .2}, map[int]float64{1: .01, 2: .011}, .0001)
fmt.Println(ok, values)
Output:

true map[0.01:[1] 0.2:[2]]
true map[0.01:[1] 0.01002:[1]]

func WhichValuesInMapIntInt

func WhichValuesInMapIntInt(Slice []int, Map map[int]int) (map[int][]int, bool)

WhichValuesInMapIntInt checks which values of an int slice are values of map[int]int. When either the slice or the map is empty, it returns false. The function returns a tuple with a map with values from Slice as keys and slices of keys in the map that have these values, and a boolean value (true if the returned map is not empty).

Example
values, ok := WhichValuesInMapIntInt([]int{10, 20, 70}, map[int]int{1: 10, 2: 20})
fmt.Println(ok, values)
values, ok = WhichValuesInMapIntInt([]int{10, 20, 70}, map[int]int{1: 11, 2: 21})
fmt.Println(ok, values)
Output:

true map[10:[1] 20:[2]]
false map[]

func WhichValuesInMapIntString

func WhichValuesInMapIntString(Slice []string, Map map[int]string) (map[string][]int, bool)

WhichValuesInMapIntString checks which values of a string slice are values of map[int]string. When either the slice or the map is empty, it returns false. The function returns a tuple with a map with values from Slice as keys and slices of keys in the map that have these values, and a boolean value (true if the returned map is not empty).

Example
values, ok := WhichValuesInMapIntString([]string{"a", "b", "c"}, map[int]string{1: "a", 2: "b"})
fmt.Println(ok, values)
values, ok = WhichValuesInMapIntString([]string{"a", "b", "c"}, map[int]string{1: "a", 2: "a", 3: "b", 50: "a", 99: "a", 7: "b"})
fmt.Println(ok, AreEqualSortedSlicesInt([]int{1, 2, 50, 99}, values["a"]), AreEqualSortedSlicesInt([]int{3, 7}, values["b"]))
values, ok = WhichValuesInMapIntString([]string{"a", "b", "c"}, map[int]string{1: "F", 2: "H"})
fmt.Println(ok, values)
Output:

true map[a:[1] b:[2]]
true true true
false map[]

func WhichValuesInMapStringFloat64

func WhichValuesInMapStringFloat64(Slice []float64, Map map[string]float64, Epsilon float64) (map[float64][]string, bool)

WhichValuesInMapStringFloat64 checks which values of an int slice are values of map[int]float64. The Epsilon parameter sets the accuracy of the comparison of two floats. When either the slice or the map is empty, it returns false. The function returns a tuple with a map with values from Slice as keys and slices of keys in the map that have these values, and a boolean value (true if the returned map is not empty). BEWARE! Do note that we work with floats, so it's safest to round them before using this function. Thus, this WhichValuesInMapStringFloat64({[]float64{.01002, .01, .2}, map[string]float64{"a": .01, "b": .011}, .0001) will return the following map: map[float64][]string{.01: {"a"}, .01002: {"a"}}}

Example
values, ok := WhichValuesInMapStringFloat64([]float64{.01, .2}, map[string]float64{"a": .01, "b": .2}, 0)
fmt.Println(ok, values)
// Watch this!
values, ok = WhichValuesInMapStringFloat64([]float64{.01002, .01, .2}, map[string]float64{"a": .01, "b": .011}, .0001)
fmt.Println(ok, values)
Output:

true map[0.01:[a] 0.2:[b]]
true map[0.01:[a] 0.01002:[a]]

func WhichValuesInMapStringInt

func WhichValuesInMapStringInt(Slice []int, Map map[string]int) (map[int][]string, bool)

WhichValuesInMapStringInt checks which values of an int slice are values of map[string]int. When either the slice or the map is empty, it returns false. The function returns a tuple with a map with values from Slice as keys and slices of keys in the map that have these values, and a boolean value (true if the returned map is not empty).

Example
values, ok := WhichValuesInMapStringInt([]int{10, 20, 7}, map[string]int{"a": 10, "G": 7, "H": 190})
fmt.Println(ok, values)
values, ok = WhichValuesInMapStringInt([]int{10, 20, 7}, map[string]int{"a": 10, "G": 7, "H": 190, "ZZ": 10, "St": 10})
fmt.Println(ok, values[7], len(values[10]))
values, ok = WhichValuesInMapStringInt([]int{10, 20, 7}, map[string]int{"a": 110, "G": 17})
fmt.Println(ok, values)
Output:

true map[7:[G] 10:[a]]
true [G] 3
false map[]

func WhichValuesInMapStringString

func WhichValuesInMapStringString(Slice []string, Map map[string]string) (map[string][]string, bool)

WhichValuesInMapStringString checks which values of a string slice are values of map[string]string. The Epsilon parameter sets the accuracy of the comparison of two floats. When either the slice or the map is empty, it returns false. The function returns a tuple with a map with values from Slice as keys and slices of keys in the map that have these values, and a boolean value (true if the returned map is not empty).

Example
values, ok := WhichValuesInMapStringString([]string{"a", "b", "c"}, map[string]string{"1": "a", "2": "b"})
fmt.Println(ok, values)
values, ok = WhichValuesInMapStringString(
	[]string{"a", "Nothing like that, forget it", "c"},
	map[string]string{"1": "a", "2": "b", "Whatever": "a", "Something else": "a", "Zigi": "Nothing like that, forget it"},
)
fmt.Println(ok, AreEqualSortedSlicesString([]string{"1", "Whatever", "Something else"}, values["a"]))
Output:

true map[a:[1] b:[2]]
true true

func WhichValuesInStringSlice

func WhichValuesInStringSlice(Slice1, Slice2 []string) (map[string][]int, bool)

WhichValuesInStringSlice checks which values of one string slice are in another slice. When the first or the second (or both) slice is empty, it returns false. The function returns a tuple with a map with values from Slice1 as keys and their indices from Slice2 as the map's values, and a boolean value (true if the returned map is not empty).

Example
values, ok := WhichValuesInStringSlice([]string{"10", "Shout Bamalama!"}, []string{"10", "Shout Bamalama!", "50"})
fmt.Println(ok, values)
values, ok = WhichValuesInStringSlice([]string{"Sing", "a", "sing", "about", "the", "sun!"}, []string{"sing", "sun", "the"})
fmt.Println(ok, values)
values, ok = WhichValuesInStringSlice([]string{"a", "a", "b"}, []string{"A", "A", "B"})
fmt.Println(ok, values)
Output:

true map[10:[0] Shout Bamalama!:[1]]
true map[sing:[0] the:[2]]
false map[]

Types

This section is empty.

Jump to

Keyboard shortcuts

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