这是在表中打印数据的代码,每列包含多个数据。这样一来,每一列就很难打印出准确的数据个数。
这就是为什么如果数据的数量是三并且循环的限制是 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)
}
达令说
相关分类