• Go 之 gotable 格式化打印表格


    Github 链接:GitHub - liushuochen/gotable

    gotable是一个第三方库,虽然一些复杂功能可能还不完善,但是对于简单的展示还是不成问题的。安装这些就不多说了,直接上例子吧。

    示例

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/liushuochen/gotable"
    5. )
    6. func main() {
    7. title := []string{"id", "name", "phone", "age", "sex", "description"}
    8. content := make([][]string, 0)
    9. content = append(content, []string{"1", "zhangsan", "132****2889", "25", "M", "NoDesc"})
    10. content = append(content, []string{"3", "lisi", "152****7873", "18", "F", "None"})
    11. content = append(content, []string{"5", "wangwu", "136****2908", "25", "M", "Nothing"})
    12. content = append(content, []string{"10", "zhaoliu", "138****5322", "15", "M", "Nothing"})
    13. table, err := gotable.Create(title...) // 根据title先创建空表
    14. if err != nil {
    15. fmt.Println("Create table failed:" + err.Error())
    16. }
    17. for i := 0; i < len(content); i++ {
    18. table.AddRow(content[i]) // 添加数据
    19. }
    20. fmt.Println(table)
    21. fmt.Println(table.HasColumn("name")) // 列存在性判断
    22. fmt.Println("columns:", table.GetColumns()) // 获取列名(也即title)
    23. fmt.Println("length:", table.Length()) // 获取数据条数
    24. table.AddColumn("create_time") // 添加新列
    25. table.Align("name", 1) // 设置对齐方式。0 居中, 1 左对齐, 2 右对齐
    26. // table.CloseBorder() // 去掉边框
    27. // table.OpenBorder() // 加上边框
    28. // table.Clear() // 清除表格
    29. fmt.Println("empty?:", table.Empty()) // 为空判断
    30. table.ToCSVFile("test.csv") // 写入 csv 文件
    31. table.ToJsonFile("test.json", 2) // 写入 json 文件
    32. fmt.Println(table.XML(2)) // 输出 xml
    33. json, err := table.JSON(2)
    34. if err != nil {
    35. fmt.Println(err)
    36. }
    37. fmt.Println(json) // 输出 json
    38. }

    输出

    1. +----+----------+-------------+-----+-----+-------------+
    2. | id | name | phone | age | sex | description |
    3. +----+----------+-------------+-----+-----+-------------+
    4. | 1 | zhangsan | 132****2889 | 25 | M | NoDesc |
    5. | 3 | lisi | 152****7873 | 18 | F | None |
    6. | 5 | wangwu | 136****2908 | 25 | M | Nothing |
    7. | 10 | zhaoliu | 138****5322 | 15 | M | Nothing |
    8. +----+----------+-------------+-----+-----+-------------+
    9. true
    10. columns: [id name phone age sex description]
    11. length: 4
    12. empty?: false
    13. "1.0" encoding="utf-8" standalone="yes"?>
    14. 25
    15. M
    16. NoDesc
    17. 1
    18. zhangsan
    19. 132****2889
    20. lisi
    21. 152****7873
    22. 18
    23. F
    24. None
    25. 3
    26. 25
    27. M
    28. Nothing
    29. 5
    30. wangwu
    31. 136****2908
    32. Nothing
    33. 10
    34. zhaoliu
    35. 138****5322
    36. 15
    37. M
    38. [
    39. {
    40. "age": "25",
    41. "create_time": "",
    42. "description": "NoDesc",
    43. "id": "1",
    44. "name": "zhangsan",
    45. "phone": "132****2889",
    46. "sex": "M"
    47. },
    48. {
    49. "age": "18",
    50. "create_time": "",
    51. "description": "None",
    52. "id": "3",
    53. "name": "lisi",
    54. "phone": "152****7873",
    55. "sex": "F"
    56. },
    57. {
    58. "age": "25",
    59. "create_time": "",
    60. "description": "Nothing",
    61. "id": "5",
    62. "name": "wangwu",
    63. "phone": "136****2908",
    64. "sex": "M"
    65. },
    66. {
    67. "age": "15",
    68. "create_time": "",
    69. "description": "Nothing",
    70. "id": "10",
    71. "name": "zhaoliu",
    72. "phone": "138****5322",
    73. "sex": "M"
    74. }
    75. ]

    test.csv

    1. id,name,phone,age,sex,description,create_time
    2. 1,zhangsan,132****2889,25,M,NoDesc,
    3. 3,lisi,152****7873,18,F,None,
    4. 5,wangwu,136****2908,25,M,Nothing,
    5. 10,zhaoliu,138****5322,15,M,Nothing,

    test.json

    1. [
    2. {
    3. "age": "25",
    4. "create_time": "",
    5. "description": "NoDesc",
    6. "id": "1",
    7. "name": "zhangsan",
    8. "phone": "132****2889",
    9. "sex": "M"
    10. },
    11. {
    12. "age": "18",
    13. "create_time": "",
    14. "description": "None",
    15. "id": "3",
    16. "name": "lisi",
    17. "phone": "152****7873",
    18. "sex": "F"
    19. },
    20. {
    21. "age": "25",
    22. "create_time": "",
    23. "description": "Nothing",
    24. "id": "5",
    25. "name": "wangwu",
    26. "phone": "136****2908",
    27. "sex": "M"
    28. },
    29. {
    30. "age": "15",
    31. "create_time": "",
    32. "description": "Nothing",
    33. "id": "10",
    34. "name": "zhaoliu",
    35. "phone": "138****5322",
    36. "sex": "M"
    37. }
    38. ]
  • 相关阅读:
    软考-论文写作-论架构风格论文
    Linux编辑器vim
    科技云报道:防患于未然,云安全要像空气和水一样无处不在
    Spring源码深度解析(六):Spring事务原理详解
    打造千万级流量秒杀系统第五课 领域建模:DDD 原理及秒杀系统领域模型
    基于TensorRT部署和C++推理目前最SOTA的图像降噪网络
    C# ASP.NET利用SQLDMO备份和还原数据库
    c++ poco读取中文文件路径
    研究04丨波动率与CTA盈利关键
    学习开发一个RISC-V上的操作系统(汪辰老师) — 环境配置
  • 原文地址:https://blog.csdn.net/TomorrowAndTuture/article/details/133748595