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

这是在表中打印数据的代码,每列包含多个数据。这样一来,每一列就很难打印出准确的数据个数。


这就是为什么如果数据的数量是三并且循环的限制是 2 那么它不会打印最后一个数据列并且循环将在 2 处停止。


如何根据数据调整列?


要求的结果


╔═══╤════════════════╤═════════════════════╗

║ # │    Projects    │ Project Priorities  ║

╟━━━┼━━━━━━━━━━━━━━━━┼━━━━━━━━━━━━━━━━━━━━━╢

║ 1 │ first project  │       None          ║

║ 2 │ second project │       Low           ║

║ 3 │                │      Medium         ║

║ 4 │                │       High          ║

╚═══╧════════════════╧═════════════════════╝

代码


package main


import (

    "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)

}


holdtom
浏览 143回答 1
1回答

达令说

有一系列可能的方法;这是一个选项,无需对行数/列数做出假设(操场):func InfoTable(headings []string, allData [][]string) {&nbsp; &nbsp; if len(headings) != len(allData) {&nbsp; &nbsp; &nbsp; &nbsp; panic("Must have a heading per column")&nbsp; &nbsp; }&nbsp; &nbsp; table := simpletable.New()&nbsp; &nbsp; // Populate headings (adding one for the row number)&nbsp; &nbsp; headerCells := make([]*simpletable.Cell, len(headings)+1)&nbsp; &nbsp; headerCells[0] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: "#"}&nbsp; &nbsp; for i := range headings {&nbsp; &nbsp; &nbsp; &nbsp; headerCells[i+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: headings[i]}&nbsp; &nbsp; }&nbsp; &nbsp; table.Header = &simpletable.Header{&nbsp; &nbsp; &nbsp; &nbsp; Cells: headerCells,&nbsp; &nbsp; }&nbsp; &nbsp; // Work out number of rows needed&nbsp; &nbsp; noOfCols := len(allData)&nbsp; &nbsp; noOfRows := 0&nbsp; &nbsp; for _, col := range allData {&nbsp; &nbsp; &nbsp; &nbsp; if len(col) > noOfRows {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; noOfRows = len(col)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Populate cells (adding row number)&nbsp; &nbsp; cells := make([][]*simpletable.Cell, noOfRows)&nbsp; &nbsp; for rowNo := range cells {&nbsp; &nbsp; &nbsp; &nbsp; row := make([]*simpletable.Cell, noOfCols+1) // add column for row number&nbsp; &nbsp; &nbsp; &nbsp; row[0] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: fmt.Sprintf("%d", rowNo+1)}&nbsp; &nbsp; &nbsp; &nbsp; for col := 0; col < noOfCols; col++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(allData[col]) > rowNo {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row[col+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: allData[col][rowNo]}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row[col+1] = &simpletable.Cell{Align: simpletable.AlignCenter, Text: ""} // Blank cell&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cells[rowNo] = row&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; table.Body = &simpletable.Body{Cells: cells}&nbsp; &nbsp; table.SetStyle(simpletable.StyleUnicode)&nbsp; &nbsp; table.Println()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go