mantyr / formatter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

License:MIT License


Languages

Language:Go 100.0%