format

package
v0.0.0-...-e715f5b Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(format string) error

Validate returns an error if given not valid formatter string. Validate usually should be called before NewFormatter to be sure that runtime error will not happend during formatting later.

Types

type Formatter

type Formatter interface {
	Format(data map[string]string, width int) string
}

Formatter allows format structs into string. Formatting rules are described with special mini language which allows to align fields.

Formatting language description. Format string is a regular string which may includes special substitution forms. Substitution forms are surrounded with curly braces. For instance: "foo {%a} bar". This is a simple format string with one trivial substitution ("{%a}") inside. Substitution has a following format: {[[-]width[%]]:name}. Where "width" is a number of chars which will be allocated for substitution's result text. Optional minus sign sets aligning to the left, instead of right which is the default. With % sign width can be set in percents instead of absolute value. Percent width values are calculated during formatter creation depends on the given parameters. "format" is a format string like in Printf is used. Format verbs should be prefixed with percent sign. Available format verb values depends on data map parameter passed to the Format method. Examples.

If data is map[string]string{
        "a": "A",
        "b": "B",
}

"foo {%a} bar {%b}" formatted to "foo A bar B" "{{{10:%a-%b}}}" formatted to "{ A-B}" "{{{-10:%a-%b}}}" formatted to "{A-B }"

func NewFormatter

func NewFormatter(format string) Formatter

NewFormatter returns new Formatter object for the given format pattern.

Jump to

Keyboard shortcuts

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