A utility library for go
go get github.com/anandp219/gotility
import "github.com/anandp219/gotility"
List of Functions added till now with examples
IsNumber("12134.0009") // true
IsNumber("a12134.0009") // false
IsEmail("xyz@gmail.com") // true
IsEmail("mail") // false
IsUrl("google.com") // true
IsUrl("123.com1") // false
IsTitleCase("Go Is VEry Popular") // true
IsTitleCase("gO IS NOT POPULAR") // false
FlattenFloat64([][]float64{{1.1, 2.1}, {3.09, 4.5}}) // => [1.1 2.1 3.09 4.5]
FlattenInt([][]int{{1, 2}, {3, 4}}) // => [1 2 3 4]
FlattenString([][]string{{"go", "is"}, {"very", "popular"}}) // => [go is very popular]
ToMatrixInt([]int{1, 2, 3, 4}, 2 /* Number of rows */) // => [[1, 2], [3, 4]]
ToMatrixFloat64([]float64{1.1, 2.1, 3.1, 4.1}, 2 /* Number of rows */) // => [[1.1, 2.1], [3.1, 4.1]]
ToMatrixString([]string{"A", "B", "C", "D"}, 2 /* Number of rows */) // => [[A B] [C D]]
SumInt([]int{1, 2, 3}) // => 6
SumFloat64([]float64{1.1, 2.2, 3.3}) // => 6.6
- Function takes 2 arguements. An array of int values and function to be applied on each of the values
- In the following examples we will use square function which is defined as
func square(arg int) int {
return arg*arg
}
MapInt([]int{1, 2, 3}, square) // => [1, 4, 9]
- In the following examples we will use square function which is defined as
func square(arg float64) float64 {
return arg*arg
}
MapFloat64([]float64{1.1, 2.2, 3.3}, square) // => [1.2100000000000002 4.840000000000001 10.889999999999999]
- In the following examples we will use addSemicolon function which is defined as
func addSemicolon(arg string) string {
return arg+";"
}
MapString([]string{"a", "b", "c"}, addSemicolon) // => [a; b; c;]
- Find the sum of slice for int, int8, int16, int32, int64, float32 and float64 data types
- returns tuple
sum(int64, float64), Error
Sum([]int{1, 2, 3}) // => 6, nil
Sum([]float32{1, 2, 3.2}) // => 6.200000047683716, nil
Sum([]bool{true}) // => 0, "cannot sum the given slice"
- Find the first occurrence of the element in the
slice
. - returns tuple
int, Error
FindIndex([]int{1, 2, 3}, 1) // => 0, nil
FindIndex([]float64{1, 2, 3}, -1.0) // => -1, nil
FindIndex([]int{1, 2, 3}, 1.0) // => -1, Error("Mismatch type of elements")
- Find the last occurrence of the element in the
slice
. - returns tuple
int, Error
FindLastIndex([]int{1, 2, 3, 1}, 1) // => 3, nil
FindLastIndex([]float64{1, 2, 3}, -1.0) // => -1, nil
FindLastIndex([]int{1, 2, 3}, 1.0) // => -1, Error("Mismatch type of elements")
- Returns the keys as an unordered slice of the given map.
- returns tuple
slice, Error
GetKeys(map[string]string{"key1": "value1", "key2": "value2}) // => ["key1", "key2"], nil
GetKeys(1) // => nil, Error("Expected map")
- Returns the values as an unordered slice of the given map.
- returns tuple
slice, Error
GetValues(map[string]string{"key1": "value1", "key2": "value2}) // => ["value1", "value2"], nil
GetValues(1) // => nil, Error("Expected map")
- Suggestions and improvements are welcome.