Skip to content
Docs
Modules
tablewriter

tablewriter

The tablewriter module is used to print rows of data in a tabular format.

The core functionality is provided by github.com/olekukonko/tablewriter.

There are two ways to use the tablewriter module:

  • Use the writer function to create a tablewriter object and use its methods to configure and render the table.
  • Call the tablewriter module directly to pass in rows of data and render the table in one action.

Module

Function signature
tablewriter(rows [][]string, options map, writer io.writer = os.stdout)

Renders a table with the given rows of data. The options map can be used to set properties of the table, such as the header, footer, and alignment. If a writer is not provided, it defaults to stdout.

Example
>>> tablewriter([["Name", "Age"], ["Alice", 25], ["Bob", 30]])
+-------+-----+
| Name  | Age |
| Alice |  25 |
| Bob   |  30 |
+-------+-----+
Example
>>> tablewriter([["Alice", 25], ["Bob", 30]], {header: ["Name", "Age"], border: false, row_separator: "="})
  NAME  | AGE  
========+======
  Alice |  25  
  Bob   |  30

Available options:

OptionTypeDescription
header[]stringThe header names for the table.
footer[]stringThe footer of the table.
center_separatorstringThe center separator character used to separate columns.
borderboolWhether to draw a border around the table.
auto_wrap_textboolWhether to automatically wrap text in the cells.
auto_format_headersboolWhether to automatically format the headers.
header_alignmentintThe alignment of the header text.
header_lineboolWhether to draw a line under the header.
alignmentintThe alignment of the text in the cells.
row_separatorstringThe separator character used to separate rows.

Functions

writer

Function signature
tablewriter.writer(writer io.writer = os.stdout) tablewriter.writer

Returns a new tablewriter.writer object that writes to the given output writer. If an output writer is not provided, it defaults to stdout.

Example
>>> w := tablewriter.writer()
>>> w.append_bulk([["Name", "Age"], ["Alice", 25], ["Bob", 30]])
>>> w.render()
+-------+-----+
| Name  | Age |
| Alice |  25 |
| Bob   |  30 |
+-------+-----+

Types

writer

A table writer object.

Methods

set_header
Method signature
set_header(header []string)

Sets the header text for columns in the table.

set_footer
Method signature
set_footer(footer []string)

Sets the footer text for columns in the table.

set_center_separator
Method signature
set_center_separator(separator string)

Sets the character used to separate columns.

set_border
Method signature
set_border(border bool)

Sets whether to draw a border around the table.

append
Method signature
append(row []string)

Appends a row to the table.

append_bulk
Method signature
 
append_bulk(rows [][]string)

Appends multiple rows to the table.

set_auto_wrap_text
Method signature
set_auto_wrap_text(auto_wrap bool)

Sets whether to automatically wrap text in the cells.

set_auto_format_headers
Method signature
set_auto_format_headers(auto_format bool)

Sets whether to automatically format the headers.

set_header_alignment
Method signature
set_header_alignment(align int)

Sets the alignment of the header text. See the constants section below for acceptable values.

set_header_line
Method signature
set_header_line(header_line bool)

Sets whether to draw a line under the header.

set_alignment
Method signature
set_alignment(align int)

Sets the alignment of the text in the cells. See the constants section below for acceptable values.

set_row_separator
Method signature
set_row_separator(separator string)

Sets the character used to separate rows.

render
Method signature
render()

Renders the table to the writer.

Constants

align_default

Specifies the default alignment for the text.

Constant
>>> tablewriter.align_default
0

align_center

Specifies that text should be centered in the cell.

Constant
>>> tablewriter.align_center
1

align_right

Specifies that text should be right-aligned in the cell.

Constant
>>> tablewriter.align_right
2

align_left

Specifies that text should be left-aligned in the cell.

Constant
>>> tablewriter.align_left
3