Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := []string{"foo", "bar", "foo"} output := basicalter.UniqueInSlice(input) fmt.Printf("%v", output) }
Output: [foo bar]
Index ¶
- func AbsoluteInt(num int) int
- func CutPrefixInString(s *string, prefix string) bool
- func CutSuffixInString(s *string, suffix string) bool
- func DelEmptyStrings(list []string) []string
- func DelInSlice[T comparable, S ~[]T](elem T, list S) S
- func DelRuneInStringWith(s *string, filtersOut ...func(rune) bool)
- func FilterInSliceWith[T any, S ~[]T](list S, filter func(T) bool) S
- func FilterRuneInStringWith(s *string, filter func(rune) bool)
- func MergeMaps[K comparable, V any, M ~map[K]V](dst M, overwrite bool, srcs ...M)
- func ReplaceInSliceWith[T any](list []T, replace func(T) T)
- func ReverseSlice[T any](list []T)deprecated
- func SortStringsByLengthDec(list []string)
- func SortStringsByLengthInc(list []string)
- func UniqueInSlice[T comparable, S ~[]T](list S) S
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AbsoluteInt ¶
AbsoluteInt return absolute value of integer.
If the 'num' is the minimum value of integers, the function panic due to the lack of a corresponding positive value with integer type.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := -10 output := basicalter.AbsoluteInt(input) fmt.Printf("%d", output) }
Output: 10
func CutPrefixInString ¶ added in v0.8.0
CutPrefixInString rewrites the value of 's' without the provided leading 'prefix' string and reports whether it found the prefix.
If 's' is nil or if its value doesn't start with 'prefix', CutPrefixInString doesn't rewrite the value of 's' and returns false.
If 'prefix' is the empty string, CutPrefixInString returns true without rewriting.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { str := "foobar" basicalter.CutPrefixInString(&str, "foo") fmt.Println(str) }
Output: bar
func CutSuffixInString ¶ added in v0.8.0
CutSuffixInString rewrites the value of 's' without the provided ending 'suffix' string and reports whether it found the suffix.
If 's' is nil or if its value doesn't end with 'suffix', CutSuffixInString doesn't rewrite the value of 's' and returns false.
If 'suffix' is the empty string, CutSuffixInString returns true without rewriting.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { str := "foobar" basicalter.CutSuffixInString(&str, "bar") fmt.Println(str) }
Output: foo
func DelEmptyStrings ¶
DelEmptyStrings remove empty elements in slice of string and return the result with a new slice.
func DelInSlice ¶ added in v0.6.0
func DelInSlice[T comparable, S ~[]T](elem T, list S) S
DelInSlice remove all occurrence of an element in a slice and return the result with a new slice.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := []string{"foo", "baz", "bar"} output := basicalter.DelInSlice("baz", input) fmt.Printf("%v", output) }
Output: [foo bar]
func DelRuneInStringWith ¶ added in v0.9.0
DelRuneInStringWith rewrites the value of 's' without the rune(s) that return true with one of the 'filtersOut' functions.
If 's' is nil, DelRuneInStringWith is a no-op.
Example ¶
package main import ( "fmt" "unicode" "github.com/jeremmfr/go-utils/basicalter" ) func main() { str := "foo 1 bar" basicalter.DelRuneInStringWith(&str, unicode.IsSpace, unicode.IsDigit) fmt.Println(str) }
Output: foobar
func FilterInSliceWith ¶ added in v0.6.0
FilterInSliceWith generate a new slice by applying on an input slice 'list' a function 'filter' to determine the inclusion of each element.
If 'list' is nil, FilterInSliceWith return nil.
If 'filter' is nil, FilterInSliceWith return list.
Example ¶
package main import ( "fmt" "strings" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := []string{"foo", "baz", "bar", "baz"} output := basicalter.FilterInSliceWith(input, func(s string) bool { return strings.HasPrefix(s, "ba") }) fmt.Printf("%v", output) }
Output: [baz bar baz]
func FilterRuneInStringWith ¶ added in v0.9.0
FilterRuneInStringWith rewrites the value of 's' with the rune(s) that return true with the function 'filter'.
If 's' or 'filter' is nil, FilterRuneInStringWith is a no-op.
Example ¶
package main import ( "fmt" "unicode" "github.com/jeremmfr/go-utils/basicalter" ) func main() { str := "foo 1 bar" basicalter.FilterRuneInStringWith(&str, unicode.IsLetter) fmt.Println(str) }
Output: foobar
func MergeMaps ¶ added in v0.7.0
func MergeMaps[K comparable, V any, M ~map[K]V](dst M, overwrite bool, srcs ...M)
MergeMaps copies all key/value pairs in 'srcs' maps to 'dst' map. Set 'overwrite' to true to allow copy value when the key is already set in 'dst' or previous element of 'srcs'.
If 'dst' is nil, MergeMaps is a no-op.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { m1 := map[string]string{"foo": "baz"} m2 := map[string]string{"bar": "baz"} basicalter.MergeMaps(m1, false, m2) fmt.Printf("%+v", m1) }
Output: map[bar:baz foo:baz]
func ReplaceInSliceWith ¶ added in v0.6.0
func ReplaceInSliceWith[T any](list []T, replace func(T) T)
ReplaceInSliceWith replace each element of a slice with the result of the function 'replace' passed in arguments.
Example ¶
package main import ( "fmt" "strings" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := []string{"Foo", "Bar", "Baz"} basicalter.ReplaceInSliceWith(input, strings.ToLower) fmt.Printf("%v", input) }
Output: [foo bar baz]
func ReverseSlice
deprecated
added in
v0.6.0
func ReverseSlice[T any](list []T)
ReverseSlice reverse order of a slice last to first, before last to second, etc.
Deprecated: use slices.Reverse() from the standard 'slices' library instead.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := []string{"foo", "baz", "bar"} basicalter.ReverseSlice(input) fmt.Printf("%v", input) }
Output: [bar baz foo]
func SortStringsByLengthDec ¶
func SortStringsByLengthDec(list []string)
SortStringsByLengthDec sort slice of string by length decreasing before lexicographic order.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := []string{"a1", "a10", "a11", "a100", "a2", "a3"} basicalter.SortStringsByLengthDec(input) fmt.Printf("%v", input) }
Output: [a100 a10 a11 a1 a2 a3]
func SortStringsByLengthInc ¶
func SortStringsByLengthInc(list []string)
SortStringsByLengthInc sort slice of string by length increasing before lexicographic order.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := []string{"a1", "a10", "a11", "a100", "a2", "a3"} basicalter.SortStringsByLengthInc(input) fmt.Printf("%v", input) }
Output: [a1 a2 a3 a10 a11 a100]
func UniqueInSlice ¶ added in v0.6.0
func UniqueInSlice[T comparable, S ~[]T](list S) S
UniqueInSlice remove duplicate elements in slice and return the result with a new slice.
Example ¶
package main import ( "fmt" "github.com/jeremmfr/go-utils/basicalter" ) func main() { input := []string{"foo", "bar", "foo"} output := basicalter.UniqueInSlice(input) fmt.Printf("%v", output) }
Output: [foo bar]
Types ¶
This section is empty.