This stable version.
Tabular data formatting package.
- table text/template
- text/template
- raw
- pretty
- json
- yaml
$ go get github.com/mantyr/formatter
table {{.ID}}\t{{.Name}}
- Aligns columns and adds a header{{.ID}}\t{{.Name}}
- Displays to a string according to the formatraw
- Displays the object line by line (field: value)json
- Displays in json formatyaml
- Displays in yaml format
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
}