Categorygithub.com/mantyr/formatter
modulepackage
1.0.1
Repository: https://github.com/mantyr/formatter.git
Documentation: pkg.go.dev

# README

Formatter

Build Status GoDoc Go Report Card Software License

This stable version.

Description

Tabular data formatting package.

Supports formats

  • table text/template
  • text/template
  • raw
  • pretty
  • json
  • yaml

Installation

$ go get github.com/mantyr/formatter

Format examples

  • table {{.ID}}\t{{.Name}} - Aligns columns and adds a header
  • {{.ID}}\t{{.Name}} - Displays to a string according to the format
  • raw - Displays the object line by line (field: value)
  • json - Displays in json format
  • yaml - Displays in yaml format

Example

package main

import (
	"github.com/mantyr/formatter"
)

func main() {
	err = Print(...)
	if err != nil {
		panic(err)
	}
}

func Print(items []interface{}) error {
	format := formatter.Format("table {{.ID}}\t{{.Name}}\t")
	header := formatter.Header(map[string]string{
		"ID":   "Identifier",
		"Name": "Name",
	})
	f, err := formatter.New(os.Stdout)
	if err != nil {
		return err
	}
	defer f.Flush()

	err = f.SetFormat(format)
	if err != nil {
		return err
	}

	err = f.SetHeader(header)
	if err != nil {
		return err
	}

	for _, item := range items {
		err = f.Write(item)
		if err != nil {
			return err
		}
	}
	return nil
}

Author

Oleg Shevelev

# Packages

No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PadWithSpace adds whitespace to the input if the input is non-empty.
TruncateWithLength truncates the source string up to the length provided by the input.

# Constants

Format keys used to specify certain kinds of output formats.
Format keys used to specify certain kinds of output formats.
Format keys used to specify certain kinds of output formats.
Format keys used to specify certain kinds of output formats.
Format keys used to specify certain kinds of output formats.
Format keys used to specify certain kinds of output formats.

# Variables

BasicFunctions are the set of initial functions provided to every template.
HeaderFunctions are used to created headers of a table.

# Interfaces

No description provided by the author

# Type aliases

Format is the format string rendered using the Context.
Header is a map destined to formatter header (table format).