为了账号安全,请及时绑定邮箱和手机立即绑定

如何根据表中的数据增加或减少列数?

如何根据表中的数据增加或减少列数?

Go
holdtom 2023-02-14 18:23:37
这是在表中打印数据的代码,每列包含多个数据。这样一来,每一列就很难打印出准确的数据个数。这就是为什么如果数据的数量是三并且循环的限制是 2 那么它不会打印最后一个数据列并且循环将在 2 处停止。如何根据数据调整列?要求的结果╔═══╤════════════════╤═════════════════════╗║ # │    Projects    │ Project Priorities  ║╟━━━┼━━━━━━━━━━━━━━━━┼━━━━━━━━━━━━━━━━━━━━━╢║ 1 │ first project  │       None          ║║ 2 │ second project │       Low           ║║ 3 │                │      Medium         ║║ 4 │                │       High          ║╚═══╧════════════════╧═════════════════════╝代码package mainimport (    "fmt"    "github.com/alexeyco/simpletable")func InfoTable(allData [][]string) {    table := simpletable.New()    table.Header = &simpletable.Header{        Cells: []*simpletable.Cell{            {Align: simpletable.AlignCenter, Text: "#"},            {Align: simpletable.AlignCenter, Text: "Projects"},            {Align: simpletable.AlignCenter, Text: "Project Priorities"},        },    }    var cells [][]*simpletable.Cell    for i := 0; i < 2; i++ {        cells = append(cells, *&[]*simpletable.Cell{            {Align: simpletable.AlignCenter, Text: fmt.Sprintf("%d", i+1)},            {Align: simpletable.AlignCenter, Text: allData[0][i]},            {Align: simpletable.AlignCenter, Text: allData[1][i]},        })    }    table.Body = &simpletable.Body{Cells: cells}    table.SetStyle(simpletable.StyleUnicode)    table.Println()}func main() {    data := [][]string{        {"first project", "second project"},        {"None", "Low", "Medium", "High"},    }    InfoTable(data)}
查看完整描述

1 回答

?
达令说

TA贡献1821条经验 获得超6个赞

有一系列可能的方法;这是一个选项,无需对行数/列数做出假设(操场):


func InfoTable(headings []string, allData [][]string) {

    if len(headings) != len(allData) {

        panic("Must have a heading per column")

    }

    table := simpletable.New()


    // Populate headings (adding one for the row number)

    headerCells := make([]*simpletable.Cell, len(headings)+1)

    headerCells[0] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: "#"}

    for i := range headings {

        headerCells[i+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: headings[i]}

    }


    table.Header = &simpletable.Header{

        Cells: headerCells,

    }


    // Work out number of rows needed

    noOfCols := len(allData)

    noOfRows := 0

    for _, col := range allData {

        if len(col) > noOfRows {

            noOfRows = len(col)

        }

    }


    // Populate cells (adding row number)

    cells := make([][]*simpletable.Cell, noOfRows)

    for rowNo := range cells {

        row := make([]*simpletable.Cell, noOfCols+1) // add column for row number

        row[0] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: fmt.Sprintf("%d", rowNo+1)}


        for col := 0; col < noOfCols; col++ {

            if len(allData[col]) > rowNo {

                row[col+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: allData[col][rowNo]}

            } else {

                row[col+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: ""} // Blank cell

            }

            cells[rowNo] = row

        }

    }

    table.Body = &simpletable.Body{Cells: cells}


    table.SetStyle(simpletable.StyleUnicode)

    table.Println()

}


查看完整回答
反对 回复 2023-02-14
  • 1 回答
  • 0 关注
  • 141 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号