Render your structs with go-pretty/table in go-way tags.
go get https://github.com/QNester/tablex
- Set up your table (see more in go-pretty/table doc)
- Add
tablex
as a tag to your struct type - 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.
- 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
},
)
- You can use
RendererMock
in your tests.