A Go port of the Python PrettyTable package. Display tabular data in ASCII, Unicode, Markdown, CSV, HTML, JSON, LaTeX, and MediaWiki formats.
- Create tables with custom field (column) names
- Add rows one by one or all at once
- Add columns one by one
- Delete rows and columns
- Sort and filter rows
- Control column alignment (left, center, right)
- Section dividers (add_divider)
- Advanced style options (borders, padding, custom chars)
- Output formats: ASCII, Unicode, Markdown, CSV, HTML, JSON, LaTeX, MediaWiki and Markdown
- Import from CSV or database rows
go get github.com/deny-7/prettytable
package main
import (
"fmt"
"github.com/deny-7/prettytable"
)
func main() {
t := prettytable.NewTableWithFields([]string{"City name", "Area", "Population", "Annual Rainfall"})
t.AddRows([][]any{
{"Adelaide", 1295, 1158259, 600.5},
{"Brisbane", 5905, 1857594, 1146.4},
{"Darwin", 112, 120900, 1714.7},
})
fmt.Println(t)
}
t := prettytable.NewTable()
t.SetFieldNames([]string{"City name", "Area", "Population", "Annual Rainfall"})
t.AddRow([]any{"Adelaide", 1295, 1158259, 600.5})
t.AddRow([]any{"Brisbane", 5905, 1857594, 1146.4})
t := prettytable.NewTableWithFields([]string{"City name", "Area", "Population", "Annual Rainfall"})
t.AddRows([][]any{
{"Adelaide", 1295, 1158259, 600.5},
{"Brisbane", 5905, 1857594, 1146.4},
{"Darwin", 112, 120900, 1714.7},
})
t := prettytable.NewTable()
t.AddColumn("City name", []any{"Adelaide", "Brisbane", "Darwin"})
t.AddColumn("Area", []any{1295, 5905, 112})
t.AddColumn("Population", []any{1158259, 1857594, 120900})
t.AddColumn("Annual Rainfall", []any{600.5, 1146.4, 1714.7})
f, _ := os.Open("myfile.csv")
table, _ := prettytable.FromCSV(f, ',')
rows, _ := db.Query("SELECT name, area, population, rainfall FROM cities")
table, _ := prettytable.FromDBRows(rows)
fmt.Println(table.RenderASCII()) // ASCII
fmt.Println(table.RenderUnicode()) // Unicode box-drawing
fmt.Println(table.RenderMarkdown()) // Markdown
fmt.Println(table.RenderCSV()) // CSV
fmt.Println(table.RenderJSON()) // JSON
fmt.Println(table.RenderHTML()) // HTML
fmt.Println(table.RenderLaTeX()) // LaTeX
fmt.Println(table.RenderMediaWiki()) // MediaWiki
fmt.Println(table.RenderMarkdown()) // Markdown
Or use:
fmt.Println(table.GetFormattedString("markdown"))
t.AddRow([]any{"Adelaide", 1295, 1158259, 600.5})
t.AddDivider()
t.AddRow([]any{"Brisbane", 5905, 1857594, 1146.4})
t.SetSortBy("Population", true) // Sort by Population descending
t.SetRowFilter(func(row []any) bool { return row[2].(int) > 1000000 }) // Only large cities
t.SetAlign("City name", prettytable.AlignLeft)
t.SetAlign("Population", prettytable.AlignRight)
t.SetAlignAll(prettytable.AlignCenter)
style := prettytable.TableStyle{
Border: false,
PaddingWidth: 1,
VerticalChar: ".",
HorizontalChar: "_",
JunctionChar: "*",
}
t.SetStyle(style)
ASCII:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
+-----------+------+------------+-----------------+
Markdown:
| City name | Area | Population | Annual Rainfall |
| --- | --- | --- | --- |
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
Unicode:
┌───────────┬──────┬────────────┬─────────────────┐
│ City name │ Area │ Population │ Annual Rainfall │
├───────────┼──────┼────────────┼─────────────────┤
│ Adelaide │ 1295 │ 1158259 │ 600.5 │
│ Brisbane │ 5905 │ 1857594 │ 1146.4 │
│ Darwin │ 112 │ 120900 │ 1714.7 │
└───────────┴──────┴────────────┴─────────────────┘
See GoDoc: https://pkg.go.dev/github.com/deny-7/prettytable
Ported to Go by Denys Bondar, 2025
Based on original work by Luke Maurits and contributors
Licensed under the BSD 3-Clause License