Skip to content

Table API Reference

Complete reference for the Table type and its methods.

Table Type

go
type Table struct {
    // Internal fields (not directly accessible)
}

The Table type represents a basic, non-paginated table window. For datasets with more than 100 rows, consider using PaginatedTable instead.


Constructor Functions

New()

Creates a new table with a given title.

go
func New(title string) *Table

Parameters:

  • title (string): The window title

Returns:

  • *Table: A new table instance

Example:

go
table := gotableview.New("My Table")
table.Columns("Name", "Age").Row("Alice", "30").Show()

Configuration Methods

Columns()

Sets the column headers for the table.

go
func (t *Table) Columns(cols ...string) *Table

Parameters:

  • cols (...string): Variable number of column names

Returns:

  • *Table: The table instance (for method chaining)

Example:

go
table.Columns("Name", "Email", "Phone")

Alias

ColumnNames() is an alias for Columns() and works identically.


ColumnNames()

Alias for Columns(). Sets the column headers.

go
func (t *Table) ColumnNames(names ...string) *Table

Parameters:

  • names (...string): Variable number of column names

Returns:

  • *Table: The table instance

Example:

go
table.ColumnNames("First Name", "Last Name", "Age")

ColumnWidths()

Sets custom widths for columns in pixels.

go
func (t *Table) ColumnWidths(widths ...int) *Table

Parameters:

  • widths (...int): Width in pixels for each column

Returns:

  • *Table: The table instance

Important:

  • Number of widths must match number of columns
  • If count doesn't match, auto-sizing is used instead
  • Widths are in pixels

Example:

go
table.Columns("ID", "Name", "Description", "Price").
    ColumnWidths(50, 150, 300, 100).
    Row("1", "Product", "Long description...", "$99.99").
    Show()

Data Methods

Row()

Adds a single row of data to the table.

go
func (t *Table) Row(values ...string) *Table

Parameters:

  • values (...string): Variable number of cell values (must be strings)

Returns:

  • *Table: The table instance

Important:

  • Number of values should match number of columns
  • All values must be strings (convert other types first)

Example:

go
table.Row("Alice", "30", "NYC")
table.Row("Bob", "25", "LA")

Rows()

Adds multiple rows of data at once.

go
func (t *Table) Rows(data [][]string) *Table

Parameters:

  • data ([][]string): 2D slice of strings representing rows

Returns:

  • *Table: The table instance

Example:

go
data := [][]string{
    {"Alice", "30", "NYC"},
    {"Bob", "25", "LA"},
    {"Carol", "28", "Chicago"},
}
table.Rows(data)

Font Customization

HeaderFont()

Sets a custom font for table headers.

go
func (t *Table) HeaderFont(name string, size int, bold, italic bool) *Table

Parameters:

  • name (string): Font name (e.g., "Arial", "Courier New")
  • size (int): Font size in points
  • bold (bool): Whether font should be bold
  • italic (bool): Whether font should be italic

Returns:

  • *Table: The table instance

Example:

go
table.HeaderFont("Arial", 12, true, false).  // Bold Arial 12pt
    Columns("Name", "Age").
    Row("Alice", "30").
    Show()

ContentFont()

Sets a custom font for table content (data rows).

go
func (t *Table) ContentFont(name string, size int, bold, italic bool) *Table

Parameters:

  • name (string): Font name
  • size (int): Font size in points
  • bold (bool): Whether font should be bold
  • italic (bool): Whether font should be italic

Returns:

  • *Table: The table instance

Example:

go
table.ContentFont("Courier New", 10, false, false).  // Regular Courier 10pt
    Columns("Code", "Value").
    Row("ABC123", "42").
    Show()

Display Method

Show()

Displays the table window and blocks until the window is closed.

go
func (t *Table) Show()

Parameters:

  • None

Returns:

  • None (void)

Important:

  • This method blocks - code after it won't run until window closes
  • Must be called last in the method chain
  • Only call once per table

Example:

go
table.Columns("A", "B").Row("1", "2").Show()
// This line runs after the window closes
fmt.Println("Table was closed")

Complete Example

go
package main

import "github.com/mansoldof/goTableView"

func main() {
    // Create table with all features
    gotableview.New("Employee Directory").
        Columns("ID", "Name", "Department", "Salary").
        ColumnWidths(60, 180, 150, 120).
        HeaderFont("Arial", 11, true, false).
        ContentFont("Segoe UI", 10, false, false).
        Row("001", "Alice Johnson", "Engineering", "$75,000").
        Row("002", "Bob Smith", "Sales", "$60,000").
        Row("003", "Carol White", "Marketing", "$65,000").
        Show()
}

Method Chaining

All methods (except Show()) return *Table, allowing you to chain calls:

go
// Good - clean and readable ✅
gotableview.New("Title").
    Columns("A", "B", "C").
    Row("1", "2", "3").
    Row("4", "5", "6").
    Show()

// Also works, but less clean ❌
table := gotableview.New("Title")
table.Columns("A", "B", "C")
table.Row("1", "2", "3")
table.Row("4", "5", "6")
table.Show()

Performance Notes

  • Basic Table is optimized for up to ~100 rows
  • For larger datasets, use PaginatedTable
  • Column auto-sizing is calculated once when window is created
  • Custom fonts may affect rendering speed slightly

See Also

Released under the MIT License.