goext

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2025 License: MIT Imports: 8 Imported by: 0

README

goext

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.

Maps:

Slices:

Strings:

TablePrinter:

Ternary:

Maps

MapSortedByKey

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->d

Slices

SliceAppendIf

Appends the given values to a slice if the condition is fulfilled.

mySlice := []int{1, 2, 3}
mySlice = goext.SliceAppendIf(mySlice, myCondition, 4, 5)
SliceAppendIfFunc

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)
SlicePrepend

Prepends the given elements to the given array.

mySlice := []int{1, 2, 3}
mySlice = goext.SlicePrepend(testSlice, 4, 5)
// [4, 5, 1, 2, 3]
SlicePrependIf

Prepends the given values to a slice if the condition is fulfilled.

mySlice := []int{1, 2, 3}
mySlice = goext.SlicePrependIf(mySlice, myCondition, 4, 5)
SlicePrependIfFunc

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)

Strings

StringContainsAny

Checks if the string contains at least one of the substrings.

goext.StringContainsAny("Hello", "Hello", "World")
goext.StringContainsAny("World", []string{"Hello", "World"}...)
// Both return true

TablePrinter

Examples
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      │
└──────┴───────────┴───────┴───────────────┘
*/

Ternary

Ternary

A simple ternary function that returns one of two values based on a boolean condition.

value := goext.Ternary(myCondition, "Value A", "Value B")
TernaryFunc

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)
TernaryFuncErr

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)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TablePrinterStyleAscii = &TablePrinterStyle{
	TopLeft:               "+",
	TopRight:              "+",
	TopIntersection:       "+",
	TopSpacer:             "-",
	MiddleLeft:            "|",
	MiddleRight:           "|",
	MiddleIntersection:    "|",
	SeparatorLeft:         "+",
	SeparatorRight:        "+",
	SeparatorIntersection: "+",
	SeparatorSpacer:       "-",
	BottomLeft:            "+",
	BottomRight:           "+╯",
	BottomIntersection:    "+",
	BottomSpacer:          "-",
}
View Source
var TablePrinterStyleDefault = &TablePrinterStyle{
	TopLeft:               "┌",
	TopRight:              "┐",
	TopIntersection:       "┬",
	TopSpacer:             "─",
	MiddleLeft:            "│",
	MiddleRight:           "│",
	MiddleIntersection:    "│",
	SeparatorLeft:         "├",
	SeparatorRight:        "┤",
	SeparatorIntersection: "┼",
	SeparatorSpacer:       "─",
	BottomLeft:            "└",
	BottomRight:           "┘",
	BottomIntersection:    "┴",
	BottomSpacer:          "─",
}
View Source
var TablePrinterStyleRounded = &TablePrinterStyle{
	TopLeft:               "╭",
	TopRight:              "╮",
	TopIntersection:       "┬",
	TopSpacer:             "─",
	MiddleLeft:            "│",
	MiddleRight:           "│",
	MiddleIntersection:    "│",
	SeparatorLeft:         "├",
	SeparatorRight:        "┤",
	SeparatorIntersection: "┼",
	SeparatorSpacer:       "─",
	BottomLeft:            "╰",
	BottomRight:           "╯",
	BottomIntersection:    "┴",
	BottomSpacer:          "─",
}

Functions

func MapSortedByKey

func MapSortedByKey[Map ~map[K]V, K cmp.Ordered, V any](m Map) iter.Seq2[K, V]

Returns an iterator for the given map that yields the key-value pairs in sorted order.

func SliceAppendIf

func SliceAppendIf[T any](slice []T, cond bool, values ...T) []T

Appends the given values to a slice if the condition is fulfilled.

func SliceAppendIfFunc

func SliceAppendIfFunc[T any](slice []T, cond bool, f func() []T) []T

Appends the given value if the condition is fulfilled. The value is lazily evaluated.

func SlicePrepend

func SlicePrepend[T any](slice []T, elems ...T) []T

Prepends the given elements to the given array.

func SlicePrependIf

func SlicePrependIf[T any](slice []T, cond bool, values ...T) []T

Prepends the given values to a slice if the condition is fulfilled.

func SlicePrependIfFunc

func SlicePrependIfFunc[T any](slice []T, cond bool, f func() []T) []T

Prepends the given value if the condition is fulfilled. The value is lazily evaluated.

func StringContainsAny

func StringContainsAny(s string, substrings ...string) bool

Checks if the string contains at least one of the substrings.

func Ternary

func Ternary[T any](cond bool, vtrue, vfalse T) T

A simple ternary function that returns one of two values based on a boolean condition.

func TernaryFunc

func TernaryFunc[T any](cond bool, vtrue, vfalse func() T) T

Like Ternary but uses functions to lazily evaluate the values.

func TernaryFuncErr

func TernaryFuncErr[T any](cond bool, vtrue, vfalse func() (T, error)) (T, error)

Like TernaryFunc but returns an error as well.

Types

type TablePrinter

type TablePrinter struct {
	Options *TablePrinterOptions
	Columns []*TablePrinterColumn
	Rows    []*TablePrinterRow
}

func NewTablePrinter

func NewTablePrinter(options *TablePrinterOptions) *TablePrinter

Create a new TablePrinter with the given options or default options.

func (*TablePrinter) AddRows

func (tp *TablePrinter) AddRows(valueRows ...[]any)

Add rows to the TablePrinter with the given values.

func (*TablePrinter) Print

func (tp *TablePrinter) Print(writer io.Writer)

Print the table to the given writer.

func (*TablePrinter) PrintStdout

func (tp *TablePrinter) PrintStdout()

Print the table to stdout.

func (*TablePrinter) PrintToFile

func (tp *TablePrinter) PrintToFile(filePath string) error

Print the table to a file.

func (*TablePrinter) SetHeaders

func (tp *TablePrinter) SetHeaders(headers ...string)

Set the headers for the TablePrinter.

type TablePrinterAlignment

type TablePrinterAlignment int
const (
	TABLE_PRINTER_ALIGNMENT_LEFT TablePrinterAlignment = iota
	TABLE_PRINTER_ALIGNMENT_RIGHT
)

type TablePrinterColumn

type TablePrinterColumn struct {
	Header          string
	HeaderAlignment TablePrinterAlignment
	ValueAlignment  TablePrinterAlignment
	Hide            bool
	// contains filtered or unexported fields
}

type TablePrinterOptions

type TablePrinterOptions struct {
	Padding int
	Style   *TablePrinterStyle
}

type TablePrinterRow

type TablePrinterRow struct {
	Values []string
}

type TablePrinterStyle

type TablePrinterStyle struct {
	TopLeft               string
	TopRight              string
	TopIntersection       string
	TopSpacer             string
	MiddleLeft            string
	MiddleRight           string
	MiddleIntersection    string
	SeparatorLeft         string
	SeparatorRight        string
	SeparatorIntersection string
	SeparatorSpacer       string
	BottomLeft            string
	BottomRight           string
	BottomIntersection    string
	BottomSpacer          string
}

Jump to

Keyboard shortcuts

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