简体   繁体   中英

How to arrange the data in columns and rows by making a box?

I want to know, how to make a box in Golang that will contain multiple rows and columns like this

┌────────────┬─────┬────────┐
│ Name       │ Age │ Score  │
├────────────┼─────┼────────┤
│ John Smith │ 30  │ 99.223 │
│ Jane Smith │ 30  │ 99.223 │
└────────────┴─────┴────────┘

Although there is a library for it, that is called olog but it is not a good option for large datasets.

The second point is that it does not support arrays to be used in a struct like I have asked previously here . In this case, if I have many variables, not just name, age, and score, it will not allow me to write each variable in a struct using an array.

Is there any other library or a way to write the data in columns and rows?

You might try/test a more complete option with Evertras/bubble-table

气泡表

It does not infer anything from any struct though, which means you need to loop over your own array, and call

// Note that there's nothing special about "ID" or "Name", these are completely
// arbitrary columns
columns := []table.Column{
  table.NewColumn(columnKeyID, "ID", 5),
  table.NewColumn(columnKeyName, "Name", 10),
}

// For each row:
rows := []table.Row{
  // This row contains both an ID and a name
  table.NewRow(table.RowData{
    columnKeyID:          "abc",
    columnKeyName:        "Hello",
  }),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM