Skip to content

QNester/tablex

Repository files navigation

Tablex

Tests

Render your structs with go-pretty/table in go-way tags.

Installation

go get https://github.com/QNester/tablex

Usage

  1. Set up your table (see more in go-pretty/table doc)
  2. Add tablex as a tag to your struct type
  3. Render the table via Renderer interface.
import (
  "github.com/QNester/tablex"
  "github.com/jedib0t/go-pretty/v6/table"
)

type myData struct {
  ID int `tablex:"header:#"`
  Name string `table:"Name"`
}

func main() {
  writer := table.NewWriter()
  renderer := tablex.NewRenderer(writer)
  table := renderer.Render()
  print(table)
}

Result of the running this program:

+---+------------+
| # | NAME       |
+---+------------+
| 1 | Joh Watson |
+---+------------+

See more examples for usage information.

Additional information

  1. Use tablex.RenderOptions to set default empty value and table format (Text Table/CSV/HTML/MD)
renderer := tablex.NewRenderer(
  writer,
  tablex.RendererOptions{
    EmptyValue: "no data",               // what you want to see if some of your fields' data equals nil
    Format:     tablex.RenderFormatHTML, // rendering format
  },
)
  1. You can use RendererMock in your tests.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published