Home  >  Article  >  Backend Development  >  How to add table comments in Golang

How to add table comments in Golang

PHPz
PHPzOriginal
2023-04-04 17:28:11770browse

In recent years, the development of Golang programs has become more and more popular because of its characteristics such as efficiency, simplicity, stability, and security. As tables are widely used in various large-scale applications, Golang programs also need to process tabular data. In Golang programs, we often need to add comments to tables to make the data easier to understand. This article will introduce how to add table comments in Golang.

1. What is a table comment

A table comment is a short paragraph of text or mathematical formula added to a table cell to specifically and accurately explain the information of the cell. Table comments are usually used for key data or data explanations in the table to help users better understand the data.

2. Why table comments are needed

In data-rich tables, users may need more information to understand the data. At this point, table comments play an important role. In addition, annotations are also needed in tabular data visualization tools to help users better understand the data.

3. How to add table comments

The method of adding table comments in the Golang program is as follows:

  1. Use the Excel library

You can use the Excel library to read and write files in Excel format. The Excel library provides methods to operate Excel cells and supports adding comments to cells. Here is a simple example:

package main

import (
    "fmt"
    "github.com/tealeg/xlsx"
)

func main() {
    file := xlsx.NewFile()
    sheet, err := file.AddSheet("Sheet1")
    if err != nil {
        fmt.Println(err)
    }

    row := sheet.AddRow()
    cell := row.AddCell()
    cell.Value = "data"
    comment := `This is a comment`
    cell.SetComment(comment, "Golang")
    err = file.Save("test.xlsx")
    if err != nil {
        fmt.Println(err)
    }
}
  1. Using the Tablewriter library

The Tablewriter library is a library for creating a command line interface that can output data into tabular form, And supports adding comments to table cells. The following is a simple example:

package main

import (
    "fmt"
    "os"
    "github.com/olekukonko/tablewriter"
)

func main() {
    data := [][]string{
        {"A", "B", "C"},
        {"1", "2", "3"},
    }

    table := tablewriter.NewWriter(os.Stdout)
    table.SetHeader([]string{"col1", "col2", "col3"})

    for _, v := range data {
        table.Append(v)
    }
    table.SetCaption(true, "This is a caption")
    table.SetFooter([]string{"", "", "total"})
    table.SetBorder(false)
    table.SetAlignment(tablewriter.ALIGN_LEFT)

    table.Render()

    fmt.Println("\n")

    table.SetFooter([]string{"", "total", "6"})
    table.SetFooterAlignment(tablewriter.ALIGN_RIGHT)
    table.Render()

    fmt.Println("\n")

    table.SetFooter([]string{"", "total=6", ""})
    table.SetFooterAlignment(tablewriter.ALIGN_CENTER)
    table.Render()

    fmt.Println("\n")

    table.SetFooter([]string{"", "", "6"})
    table.SetFooterColor(tablewriter.Colors{tablewriter.FgGreenColor}, tablewriter.Colors{}, tablewriter.Colors{})
    table.SetFooterAlignment(tablewriter.ALIGN_LEFT)
    table.SetFooterLine(false)
    table.Render()

    fmt.Println("\n")

    table.SetFooter([]string{"", "", "6"})
    table.SetFooterColor(tablewriter.Colors{tablewriter.FgRedColor}, tablewriter.Colors{}, tablewriter.Colors{})
    table.SetFooterAlignment(tablewriter.ALIGN_RIGHT)
    table.SetFooterLine(true)
    table.Render()

    fmt.Println("\n")

    table.SetBorder(true)
    table.SetCaption(true, "This is a caption")
    table.SetAutoMergeCells(true)
    table.Append([]string{"1", "2", "3"})
    table.Append([]string{"4", "5", "6"})
    table.SetCell(0, 0, tablewriter.Color(tablewriter.FgYellowColor), tablewriter.BgGreenColor, "test")
    table.SetCell(0, 1, tablewriter.Colors{tablewriter.Bold}, tablewriter.BgBlackColor, "bold")
    table.SetCell(0, 2, tablewriter.Colors{}, tablewriter.BgBlackColor, "normal")
    table.SetCell(1, 0, "", "", "not colored")
    table.SetCell(1, 1, "", "", "not colored")
    table.SetCell(1, 2, "", "", "not colored")
    table.SetCell(2, 0, tablewriter.Colors{tablewriter.FgRedColor, tablewriter.Bold}, tablewriter.BgWhiteColor, "red bold")
    table.SetCell(2, 1, tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold}, tablewriter.BgWhiteColor, "hi red bold")
    table.SetCell(2, 2, tablewriter.Colors{tablewriter.FgHiRedColor}, tablewriter.BgWhiteColor, "hi red")
    table.SetCell(2, 2, tablewriter.Colors{tablewriter.FgHiRedColor}, tablewriter.BgWhiteColor, "hi red")
    table.SetCaption(true, "This is a caption")
    table.SetCaptionAlign(tablewriter.ALIGN_CENTER)
    table.Append([]string{"7", "8", "9"})
    table.Render()

    fmt.Println("\n")

    table.ClearRows()
    table.SetHeader([]string{"Name", "Age", "Money"})
    table.SetAutoWrapText(false)
    table.SetHeaderColor(tablewriter.Colors{tablewriter.FgHiBlueColor}, tablewriter.Colors{tablewriter.BgBlackColor}, tablewriter.Colors{})
    table.SetColumnColor(tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.BgYellowColor}, tablewriter.Colors{})
    table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
    table.SetTablePadding("\t")
    table.SetBorder(false)

    for i := 1; i <= 10; i++ {
        data := []string{fmt.Sprintf("user%d", i), fmt.Sprintf("%d", i*10), fmt.Sprintf("$%d.00", i*100)}
        table.Append(data)
    }

    table.SetRowLine(true)
    table.SetRowSeparator("-")
    table.Render()

    fmt.Println("\n")

    table.ClearRows()
    table.SetHeader([]string{"City", "Population"})
    table.SetHeaderAlignment(tablewriter.ALIGN_CENTER)
    table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor}, tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{})
    table.SetRowColor(tablewriter.Color(tablewriter.FgWhiteColor), tablewriter.Color(tablewriter.FgWhiteColor))
    table.SetColumnSeparator("|")
    table.SetRowSeparator("-")
    table.SetBorder(true)

    for _, v := range [][]string{
        {"Shanghai", "25000000"},
        {"Beijing", "21800000"},
    } {
        table.Append(v)
    }

    table.Render()

    fmt.Println("\n")

    table.ClearRows()
    table.SetOutputMirror(os.Stdout)
    table.SetAutoWrapText(false)
    table.SetHeader([]string{"Name", "NickName"})
    table.SetFooter([]string{"", "total"})
    table.SetFooterAlignment(tablewriter.ALIGN_CENTER)
    table.SetFooterColor(tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{})
    table.SetCaption(true, "This is a caption")
    table.SetCaptionAlignment(tablewriter.ALIGN_LEFT)

    table.AppendBulk([][]string{[]string{"JJJ", "kkk"}, []string{"kkk", "lll"}, []string{"aaa", "bbb"}})
    table.Render()

    fmt.Println("\n")

    table.ClearRows()
    table.SetHeader([]string{"Name", "NickName"})
    table.SetFooter([]string{"", "total"})

    for i := 1; i <= 100; i++ {
        table.Append([]string{fmt.Sprintf("user%d", i), fmt.Sprintf("nick%d", i)})
    }

    table.SetAutoWrapText(true)
    table.SetRowLine(true)
    table.SetHeaderLine(false)
    table.SetFooterLine(false)
    table.SetCaption(true, "This is a caption")
    table.AppendFooter([]string{"", "total=100"})
    table.Render()

    fmt.Println("\n")
}

Table comments can be implemented in any cell in the Tablewriter library in this example. You can use the SetFooter method to add a global comment, or you can use methods such as SetFontColor to further beautify it. sheet.

4. Summary

This article introduces how to add table comments in Golang programs. Table annotations can help users better understand table data and also make it more attractive. There are many ways to add table comments in Golang programs, among which using the Excel library and Tablewriter library is the simplest. Next time you work with tabular data, try these methods to make the data easier to understand and manage.

The above is the detailed content of How to add table comments in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn