Various small go extensions without dependencies.
Feel free to include this library or just copy the needed files or parts of them to your projects.
Cmd:
Env:
Maps:
Run:
- TODO
Slices:
Strings:
- StringContainsAny
- StringTrimAllPrefix
- StringTrimAllSuffix
- StringTrimNewlineSuffix
- StringSplitByNewLine
TablePrinter:
Ternary:
Allows running executable with arguments and various options.
The following options are available:
- WorkingDirectory: Runs the command in the given working directory
- OutputToConsole: Outputs stdout to the console
- SkipPostProcessOutput: Does not post-process the output (remove newlines)
- AdditionalEnv: Specify addional environment variables that should be set
Runners can be configured with setting the properties or by using With... methods in a fluent manner.
You can create and configure a runner and re-use it to run multiple commands.
There are also a few pre-defined runners available that can be used as follows:
// Default runner with no options
goext.CmdRunners.Default.Run("cmd", "arg1")
// Runner that outputs to the console
goext.CmdRunners.Console.Run("cmd", "arg1")Usually, each argument is passed as its own value. If you have a string with all arguments, you can use the following so split the arguments:
goext.CmdRunners.Console.Run("cmd", goext.Cmd.SplitArgs("arg1 arg2")...)Usage is as follows:
err := goext.NewCmdRunner().With<...>.Run("cmd", "arg1", "arg2")Runs the command with the given options.
err := goext.NewCmdRunner().Run("myapp", "arg1", "arg2")Runs the command and returns the separate output from stdout and stderr.
stdout, stderr, err := goext.NewCmdRunner().RunGetOutput("myapp")Runs the command and returns the output from stdout and stderr combined.
output, err := goext.NewCmdRunner().RunGetCombinedOutput("myapp")args := goext.Cmd.SplitArgs("arg1 arg2")err := <execute cmd>
exitCode := goext.Cmd.ErrorExitCode(err)Checks if the given environment variable exists or not.
exists := goext.Env.Exists("MY_VAR")Returns the value if the environment variable exists or the default otherwise.
value, exists := goext.Env.ValueOrDefault("MY_VAR", "default")Copies a file copy from source to destination.
Checks if a file exists (and it is not a directory).
Writes the given object into a file.
Returns an iterator for the given map that yields the key-value pairs in sorted order.
myMap := map[int]string{1: "b", 0: "a", 3: "d", 2: "c"}
for key, value := range goext.MapSortedByKey(myMap) {
fmt.Printf("%d->%s\n", key, value)
}
// Always prints
// 0->a
// 1->b
// 2->c
// 3->dA runner for go functions with various options.
Appends the given values to a slice if the condition is fulfilled.
mySlice := []int{1, 2, 3}
mySlice = goext.SliceAppendIf(mySlice, myCondition, 4, 5)Appends the given value if the condition is fulfilled. The value is lazily evaluated.
valueFunc := func() []int { return []int{4, 5} }
mySlice := []int{1, 2, 3}
mySlice = goext.SliceAppendIfFunc(mySlice, myCondition, valueFunc)Prepends the given elements to the given array.
mySlice := []int{1, 2, 3}
mySlice = goext.SlicePrepend(testSlice, 4, 5)
// [4, 5, 1, 2, 3]Prepends the given values to a slice if the condition is fulfilled.
mySlice := []int{1, 2, 3}
mySlice = goext.SlicePrependIf(mySlice, myCondition, 4, 5)Prepends the given value if the condition is fulfilled. The value is lazily evaluated.
valueFunc := func() []int { return []int{4, 5} }
mySlice := []int{1, 2, 3}
mySlice = goext.SlicePrependIfFunc(mySlice, myCondition, valueFunc)Checks if the string contains at least one of the substrings.
goext.StringContainsAny("Hello", "Hello", "World")
goext.StringContainsAny("World", []string{"Hello", "World"}...)
// Both return trueTrims all occurrences of the given prefix from the start of the string.
value := goext.StringTrimAllPrefix("xxxyyyxxx", "x")Trims all occurrences of the given suffix from the end of the string.
value := goext.StringTrimAllSuffix("xxxyyyxxx", "x")Trims all occurrences of newline characters from the end of the string.
value := goext.StringTrimNewlineSuffix("my-text\n\r\n")Splits the string by new line characters, supporting both "\n" and "\r\n".
lines := goext.StringSplitByNewLine("line1\r\nline2\nline3")tablePrinter := goext.NewTablePrinter(nil)
tablePrinter.SetHeaders("Id", "Name", "Age", "City")
tablePrinter.Columns[0].ValueAlignment = goext.TABLE_PRINTER_ALIGNMENT_RIGHT
tablePrinter.AddRows(
[]any{1, "Alice", "30", "New York"},
[]any{2, "Bob", "25", "Los Angeles"},
[]any{3, "Charlie", "35", "Chicago"},
)
tablePrinter.PrintStdout()
/* Prints:
┌──────┬───────────┬───────┬───────────────┐
│ Id │ Name │ Age │ City │
├──────┼───────────┼───────┼───────────────┤
│ 1 │ Alice │ 30 │ New York │
│ 2 │ Bob │ 25 │ Los Angeles │
│ 3 │ Charlie │ 35 │ Chicago │
└──────┴───────────┴───────┴───────────────┘
*/A simple ternary function that returns one of two values based on a boolean condition.
value := goext.Ternary(myCondition, "Value A", "Value B")Like Ternary but uses functions to lazily evaluate the values.
aFunc := func() string { return "Value A" }
bFunc := func() string { return "Value B"}
value := goext.TernaryFunc(myCondition, aFunc, bFunc)Like TernaryFunc but returns an error as well.
aFunc := func() (string,error) { return "Value A", nil }
bFunc := func() (string,error) { return "Value B", nil }
value, err := goext.TernaryFuncErr(myCondition, aFunc, bFunc)