SoFunction
Updated on 2025-03-05

The use of Go daily database termtables

Today I will learn a simpler 😀, and termtables processes the output of table data. Suitable for outputting some state or statistical data anytime, anywhere, for easy observation and debugging. It is a very small tool library. I occasionally encountered this library when I was learning the dateparse library.

Quick use

This article's code uses Go Modules.
Create a directory and initialize:

$ mkdir termtables && cd termtables
$ go mod init /darjun/go-daily-lib/termtables

Install the termtables library:

$ go get -u /scylladb/termtables

The most original termtables library is /apcera/termtables, and then the original repository has been deleted. Currently, they are all using other people's fork repositories.
use:

package main

import (
  "fmt"
  "/scylladb/termtables"
)

func main() {
  t := ()
  ("User", "Age")
  ("dj", 18)
  ("darjun", 30)
  (())
}

run:

$ go run 
+--------+-----+
| User   | Age |
+--------+-----+
| dj     | 18  |
| darjun | 30  |
+--------+-----+

It is very convenient to use. First, call() to create a table object, call the AddHeader() method of the object to add header information, and then call AddRow() to add data row by line. Finally, call Render() to return the rendered table string.

model

To handle plain text tables, termtables also supports outputting tables in HTML and Markdown formats. Just call the SetModeHTML()/SetModeMarkdown() method of the table object to set some modes.

func main() {
  t := ()
  ("User", "Age")
  ("dj", 18)
  ("darjun", 30)
  ("HTML:")
  ()
  (())

  ("Markdown:")
  ()
  (())
}

run:

$ go run 
HTML:
<table class="termtable">
<thead>
<tr><th>User</th><th>Age</th></tr>
</thead>
<tbody>
<tr><td>dj</td><td>18</td></tr>
<tr><td>darjun</td><td>30</td></tr>
</tbody>
</table>

Markdown:
| User   | Age |
| ------ | --- |
| dj     | 18  |
| darjun | 30  |

The output format can be used directly in the Markdown/HTML file.

Summarize

Today I have a relaxing look at a small tool library termtables. Although it is not complicated to implement a similar one by one, the termtables library has helped us handle more cumbersome details such as encoding and word width. If you need to print data like tables in the sample program, you might as well try termtables.
If you find a fun and useful Go language library, welcome to submit an issue on the Go daily library GitHub
refer to

Go daily library GitHub:/darjun/go-daily-lib

This is the end of this article about the use of termstables in Go Daily Library. For more information about Go termstables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!