tools

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: GPL-3.0 Imports: 7 Imported by: 3

Documentation

Overview

Package tools contains a collection of small utility methods.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildInfoMap added in v0.1.5

func BuildInfoMap(bi *debug.BuildInfo) map[string]string

BuildInfoMap converts BuildInfo.Settings into a map[string]string.

func GetenvWithDefault

func GetenvWithDefault[T any](name string, defaultValue T, options ...any) (value T)

GetenvWithDefault is a typed version of os.Getenv that allows you to specify a default value. The return type of the method is the type of the defaultValue argument. Currently supported types:

  • string
  • int
  • bool
  • float64
  • time.Duration
  • time.Time (must provide format as third argument)

If you use any other type you will always get the default value.

Example
var boolOption bool
var intOption int
var timeOption time.Time

defaultTime, _ := time.Parse("2006-01-01", "2000-10-10")

intOption = GetenvWithDefault("MY_INT_OPTION", 10)
boolOption = GetenvWithDefault("MY_BOOL_OPTION", true)
timeOption = GetenvWithDefault("MY_TIME_OPTION", defaultTime, "2006-01-02")

fmt.Printf("int var is %d, bool var is %t, time var is %s", intOption, boolOption, timeOption)

func RangeIterator

func RangeIterator(input string) iter.Seq2[int, error]

RangeIterator parses a comma-separated string of numbers and ranges, yielding individual integer values. Supports syntax like "1,4,10" or "1,4-10".

Examples:

  • Individual numbers: "1,4,10" yields 1, 4, 10
  • Ranges: "5-8" yields 5, 6, 7, 8
  • Mixed: "1,4-10" yields 1, 4, 5, 6, 7, 8, 9, 10

Whitespace around numbers and the range operator is automatically trimmed. Ranges must be ascending (start <= end); descending ranges yield an error. Invalid input (non-numeric, malformed syntax) yields an error for that element.

The iterator yields both a value and an error. When an error occurs for a specific element, the value will be 0 and the error will be non-nil. The iterator continues processing remaining elements after an error.

Example
for num, err := range RangeIterator("1,4,6-10") {
	if err != nil {
		log.Fatalf("bad range specification")
	}
	println(num)
}

// Prints:
// 1
// 4
// 6
// 7
// 8
// 9
// 10

func ReverseMap

func ReverseMap[K comparable, V comparable](inputMap map[K]V) map[V]K

ReverseMap returns a map with keys and values reversed.

Example
forwardMap := map[int]string{
	0:  "alice",
	3:  "bob",
	11: "mallory",
}

for k, v := range ReverseMap(forwardMap) {
	fmt.Printf("%s had key %d", k, v)
}

// Prints:
// alice had key 0
// bob had key 3
// mallory had key 11

func Unindent

func Unindent(s string) string

Unindent removes common leading whitespace from a multiline string. It is designed to allow proper indentation of literal strings in your code.

The function performs the following operations:

  1. Removes any leading blank lines
  2. Removes any trailing blank lines
  3. Finds the minimum leading whitespace (spaces and tabs) among all non-empty lines and remove that amount of whitespace from each line
  4. Replaces lines containing only whitespace with blank lines

For empty strings or strings containing only whitespace, returns an empty string.

Example
aBlockOfText := `
		Three Rings for the Elven-kings under the sky,
		Seven for the dwarf-lords in their halls of stone,
		Nine for Mortal Men doomed to die,
		One for the Dark Lord on his dark throne,
		In the Land of Mordor where the Shadows lie.
		One Ring to rule them all, One Ring to find them,
		One Ring to bring them all and in the darkness bind them
		In the Land of Mordor where the Shadows lie.
		`
print(Unindent(aBlockOfText))

// Prints (with no indentation):
// Three Rings for the Elven-kings under the sky,
// Seven for the dwarf-lords in their halls of stone,
// Nine for Mortal Men doomed to die,
// One for the Dark Lord on his dark throne,
// In the Land of Mordor where the Shadows lie.
// One Ring to rule them all, One Ring to find them,
// One Ring to bring them all and in the darkness bind them
// In the Land of Mordor where the Shadows lie.

Types

This section is empty.

Jump to

Keyboard shortcuts

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