This is the 27th day of my participation in Gwen Challenge

A background

When you print the result using the CLI, format the output. However, due to the long content, you need to specify other fields such as the width. You can use goTable to automatically adjust the width of each column according to the length of the field and display the field in the center automatically. Tables can dynamically add columns and data.

The second library

Gotable can print tables to the console. Currently, it supports ASCII characters and Chinese characters.

Can be very easy to achieve according to the content of self-adjustment bureau display, very convenient.

Three code

3.1 Creating a Table

func Create(columns ...string) (*table.Table, error)
Copy the code

3.2 increase the row

  • Increase the single
func (tb *Table) AddRow(row map[string]string) error
Copy the code
  • Add more lines
func (tb *Table) AddRows(rows []map[string]string) []map[string]string
Copy the code

3.3 increase the column

func (tb *Table) AddColumn(column string) error

Copy the code

3.4 Printing the Table

func (tb *Table) PrintTable(a)
Copy the code

3.5 Border Operation

  • Close the border
func (tb *Table) CloseBorder(a)
Copy the code
  • Open borders
func (tb *Table) OpenBorder(a)
Copy the code

3.6 output json

func (tb *Table) Json(indent int) (string, error)
Copy the code

Four test

4.1 Creating a Table

package main

import (
	"fmt"
	"github.com/liushuochen/gotable"
)

func main(a) {

	table, err := gotable.Create("country"."city")
	iferr ! =nil {
		fmt.Println(err.Error())
		return
	}

	values := []map[string]string{{"country": "China"."city": "Beijing"},
		{"country": "Japan"."city": "Tokyo"},
		{"country": "North Korea"."city": "Pyongyang"}}
	for _, value := range values {
		err := table.AddRow(value)
		iferr ! =nil {
			fmt.Println(err.Error())
			return
		}
	}
	r, _ := table.Json(4)
	fmt.Println(r)
	table.CloseBorder()
	table.PrintTable()
}

Copy the code

4.2 output json

package main

import (
	"fmt"
	"github.com/liushuochen/gotable"
)

func main(a) {
	tb, err := gotable.Create("Name"."ID"."salary")
	iferr ! =nil {
		fmt.Println("Create table failed: ", err.Error())
		return
	}

	rows := make([]map[string]string.0)
	for i := 0; i < 3; i++ {
		row := make(map[string]string)
		row["Name"] = fmt.Sprintf("employee-%d", i)
		row["ID"] = fmt.Sprintf("00%d", i)
		row["salary"] = "60000"
		rows = append(rows, row)
	}

	jsonString, err := tb.Json(4)
	iferr ! =nil {
		fmt.Println("ERROR: ", err.Error())
		return
	}
	fmt.Println(jsonString)
	// output: []

	tb.AddRows(rows)

	jsonString, err = tb.Json(4)
	iferr ! =nil {
		fmt.Println("ERROR: ", err.Error())
		return
	}
	fmt.Println(jsonString)
	// output:
	/ / /
	/ / {
	// "ID": "000",
	// "Name": "employee-0",
	// "salary": "60000"
	/ /},
	/ / {
	// "ID": "001",
	// "Name": "employee-1",
	// "salary": "60000"
	//
	//
	// "ID": "002",
	// "Name": "employee-2",
	// "salary": "60000"
	/ /}
	/ /]
}
Copy the code

Five other

Golang tabWriter can also be used to print tables, but the definition is more troublesome, more advanced functions can be defined themselves, goTable is ready to use, can match table output and JSON output to the console scenario.

Refer to the link

  • Github.com/liushuochen…