在CLI中打印表格----gotable使用介绍

目录

介绍

获取gotable

在github中获取

下载源码

git clone

go mod

API

创建table

从结构体中创建空table

获取版本信息

获取版本列表

打印表格

给表格添加行

给表格添加多个行

给表格添加列

设置默认值

默认值常量:gotable.Default

获取默认值

获取默认值映射

删除默认值

指定对齐方式

居中对齐

左对齐

右对齐

表格是否为空

获取column列表

将表格转换为[]Map


介绍

gotable是Golang的开源第三方库,它的主要功能是将数据组成表格并在CLI中打印,效果如下:

    +---------------+-------------+
    |    country    |    city     |
    +---------------+-------------+
    |     China     |   Beijing   |
    |     Japan     |    Tokyo    |
    |  North Korea  |  Pyongyang  |
    +---------------+-------------+

gotable可以根据字段的长度自动调整每列的宽度,并且自动地将字段按照对齐要求显示。表格可以动态的新增列和数据。本篇博客是gotable库的中文使用文档,并伴随gotable库不断更新。

获取gotable

在github中获取

gotable的github传送门

下载源码

下载源码zip文件

git clone

使用git clone:

git clone https://github.com/liushuochen/gotable.git

go mod

在gomod中: (关于version的获取,请参考go mod 私有项目版本号指定)

require github.com/liushuochen/gotable <version>

API

创建table

使用函数Create创建一个空table。Create函数允许接收一个可变长字符串参数,用来表示table的列(Column)。

func Create(columns ...string) (*table.Table, error)

使用示例

package main

import (
	"fmt"
	"github.com/liushuochen/gotable"
)

func main() {
	_, err := gotable.Create("id", "name")
	if err != nil {
		fmt.Println("create table failed: ", err.Error())
		return
	}
}

当输入一个有重复内容的列时,创建table会失败。

package main

import (
	"fmt"
	"github.com/liushuochen/gotable"
)

func main() {
	_, err := gotable.Create("id", "name", "id")
	if err != nil {
		fmt.Println("create table failed: ", err.Error())
		// output: create table failed:  value id has exit
		return
	}
}

 当没有任何列输入时,创建table失败(返回一个详细的错误类型*exception.ColumnsLengthError)。

package main

import (
	"fmt"
	"github.com/liushuochen/gotable"
)

func main() {
	_, err := gotable.Create()
	if err != nil {
		fmt.Println("Create table failed: ", err.Error())
		// output: Create table failed:  columns length must more than zero
		return
	}
}

从结构体中创建空table

使用gotable函数CreateByStruct从一个结构体中创建一个空table。

函数签名

func CreateByStruct(v interface{}) (*table.Table, error)

使用示例

首先定义一个结构体。使用结构体标签`gotable:"column_name"`来指定column名称。对于没有使用结构体标签的字段,column名称默认与字段名称相同。

type Student struct {
	Id		string	`gotable:"id"`
	Name	string
}

当结构体中不包含任何字段时,CreateByStruct函数创建table失败(返回一个详细的错误类型*exception.ColumnsLengthError)。

package main

import (
	"fmt"
	"github.com/liushuochen/gotable"
)

type Student struct {
	Id		string	`gotable:"id"`
	Name	string
}

func main() {
	_, err := gotable.CreateByStruct(&Student{})
	if err != nil {
		fmt.Println("Create table failed: ", err.Error())
	}
}

获取版本信息

func Version() string

使用gotable函数Version获取gotable的版本信息:

package main

import (
	"fmt"
	"github.com/liushuochen/gotable"
)

func main() {
	version := gotable.Version()
	fmt.Println(version)
	// output: gotable 5.18.0
}

获取版本列表

func Versions() []string

使用gotable函数Ve

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值