Documentation ¶
Overview ¶
Package stringutils is the obligatory add-on for convenience functions related to strings.
Index ¶
- func Contract(m map[string]string, f func(string, string) string) []string
- func DefaultIf(value, defaultValue, condition string) string
- func DefaultIfEmpty(value, defaultValue string) string
- func ErrorMessages(errs []error) []string
- func Keys(m map[string]string) []string
- func MapWithDefault(m map[string]string, def string) func(string) string
- func Quote(s []string) []string
- func Transform(s []string, f func(string) string) []string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contract ¶
Contract takes a map[string]string and contracts pairs of key and values.
Example ¶
Contract takes a map[string]string and pipes key-value pairs through a function and returns accumulated results.
package main import ( "fmt" "sort" "github.com/gotohr/fritzctl/internal/stringutils" ) func main() { m := map[string]string{"key1": "value1", "key2": "value2"} vs := stringutils.Contract(m, func(k string, v string) string { return v }) sort.Strings(vs) fmt.Println(vs) }
Output: [value1 value2]
func DefaultIf ¶
DefaultIf falls back to a default value if the passed value is equal to the condition parameter.
Example ¶
DefaultIf takes three strings v, d, c and returns (v != c)? v : d.
package main import ( "fmt" "github.com/gotohr/fritzctl/internal/stringutils" ) func main() { s := stringutils.DefaultIf("xxx", "[CENSORED]", "xxx") fmt.Println(s) s = stringutils.DefaultIf("abc", "[CENSORED]", "xxx") fmt.Println(s) }
Output: [CENSORED] abc
func DefaultIfEmpty ¶
DefaultIfEmpty falls back to a default value if the passed value is empty
Example ¶
DefaultIfEmpty takes two strings v, d and returns (v != "")? v : d.
package main import ( "fmt" "github.com/gotohr/fritzctl/internal/stringutils" ) func main() { s := stringutils.DefaultIfEmpty("my string", "<no value>") fmt.Println(s) s = stringutils.DefaultIfEmpty("", "<no value>") fmt.Println(s) }
Output: my string <no value>
func ErrorMessages ¶
ErrorMessages accumulates the error messages from slice of errors.
Example ¶
ErrorMessages extracts the messages from a slice of errors.
package main import ( "errors" "fmt" "github.com/gotohr/fritzctl/internal/stringutils" ) func main() { es := []error{errors.New("an_error"), errors.New("another_error")} ms := stringutils.ErrorMessages(es) fmt.Println(ms) }
Output: [an_error another_error]
func Keys ¶
Keys extracts the key of a map[string]string and returns them as a slice of strings.
Example ¶
Keys extracts the keys of a map[string]string.
package main import ( "fmt" "sort" "github.com/gotohr/fritzctl/internal/stringutils" ) func main() { m := map[string]string{"key1": "value1", "key2": "value2"} ks := stringutils.Keys(m) sort.Strings(ks) fmt.Println(ks) }
Output: [key1 key2]
func MapWithDefault ¶
MapWithDefault provides a closure that does the regular map lookup, and defaults to the passed value no value is found.
func Quote ¶
Quote takes a slice of strings and returns a slice of strings where each element of the input is put in double quotation marks.
Example ¶
Quote puts double quotation marks around each element of a string slice.
package main import ( "fmt" "github.com/gotohr/fritzctl/internal/stringutils" ) func main() { s := []string{"hello", "world"} q := stringutils.Quote(s) fmt.Println(q) }
Output: ["hello" "world"]
func Transform ¶
Transform takes a slice of strings, transforms each element and returns a slice of strings containing the transformed values
Example ¶
Transform applies a given operation to all elements of a string slice.
package main import ( "fmt" "strings" "github.com/gotohr/fritzctl/internal/stringutils" ) func main() { s := []string{"hello", "world"} u := stringutils.Transform(s, strings.ToUpper) fmt.Println(u) }
Output: [HELLO WORLD]
Types ¶
This section is empty.