golang美化数据展示提升可读性插件库ffmt的使用

Golang美化数据展示提升可读性插件库ffmt的使用

ffmt是一个用于美化Golang数据展示的库,可以显著提升复杂数据结构的可读性。它提供了多种格式化输出方式,包括友好格式、JSON风格、表格格式等。

安装

import ffmt "gopkg.in/ffmt.v1"

使用示例

基本数据结构定义

package main

import (
	ffmt "gopkg.in/ffmt.v1"
)

func main() {
	example()
}

type mt struct {
	String string
	Int    int
	Slice  []int
	Map    map[string]interface{}
}

各种格式化方法示例

func example() {
	m := mt{
		"hello world",
		100,
		[]int{1, 2, 3, 4, 5, 6},
		map[string]interface{}{
			"A":  123,
			"BB": 456,
		},
	}

	// 1. 默认格式化输出
	fmt.Println(m) 
	/* 输出:
	{hello world 100 [1 2 3 4 5 6] map[BB:456 A:123]}
	*/

	// 2. 友好格式化输出
	ffmt.Puts(m)
	/* 输出:
	{
	 String: "hello world"
	 Int:    100
	 Slice:  [
	  1 2 3
	  4 5 6
	 ]
	 Map: {
	  "A":  123
	  "BB": 456
	 }
	}
	*/

	// 3. 不带引号的字符串输出
	ffmt.Print(m)
	/* 输出:
	{
	 String: hello world
	 Int:    100
	 Slice:  [
	  1 2 3
	  4 5 6
	 ]
	 Map: {
	  A:  123
	  BB: 456
	 }
	}
	*/

	// 4. 带类型信息的格式化输出
	ffmt.P(m)
	/* 输出:
	main.mt{
	 String: string("hello world")
	 Int:    int(100)
	 Slice:  []int[
	  int(1) int(2) int(3)
	  int(4) int(5) int(6)
	 ]
	 Map: map[string]interface {}{
	  string("A"):  int(123)
	  string("BB"): int(456)
	 }
	}
	*/

	// 5. JSON风格格式化
	ffmt.Pjson(m)
	/* 输出:
	{
	 "Int": 100
	,"Map": {
	  "A":  123
	 ,"BB": 456
	 }
	,"Slice": [
	  1,2,3
	 ,4,5,6
	 ]
	,"String": "hello world"
	}
	*/

	// 6. 表格化输出
	m0 := ffmt.ToTable(m, m) // 将字段转换为表格
	ffmt.Puts(m0)
	/* 输出:
	[
	 [
	  "String" "Int"
	  "Slice"  "Map"
	 ]
	 [
	  "hello world"   "100"
	  "[1 2 3 4 5 6]" "map[A:123 BB:456]"
	 ]
	]
	*/

	// 7. 格式化表格
	m1 := ffmt.FmtTable(m0) // 格式化表格输出
	ffmt.Puts(m1)
	/* 输出:
	[
	 "String      Int Slice         Map               "
	 "hello world 100 [1 2 3 4 5 6] map[A:123 BB:456] "
	]
	*/

	// 8. 标记位置
	ffmt.Mark("hello")
	/* 输出:
	main.go:124  hello
	*/

	// 9. 字节查看器
	ffmt.Print(ffmt.BytesViewer("Hello world! Hello All!"))
	/* 输出:
  |  Address | Hex                                             | Text             |
  | -------: | :---------------------------------------------- | :--------------- |
  | 00000000 | 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 20 48 65 6c | Hello world! Hel |
  | 00000010 | 6c 6f 20 41 6c 6c 21                            | lo All!          |
	*/
}

功能总结

  1. Puts: 友好格式化输出,字符串带引号
  2. Print: 友好格式化输出,字符串不带引号
  3. P: 带类型信息的格式化输出
  4. Pjson: JSON风格格式化输出
  5. ToTable: 将数据结构转换为表格形式
  6. FmtTable: 格式化表格输出
  7. Mark: 标记代码位置
  8. BytesViewer: 十六进制字节查看器

ffmt库特别适合在开发和调试时使用,可以清晰直观地查看复杂数据结构的内容,大大提升了开发效率。


更多关于golang美化数据展示提升可读性插件库ffmt的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang美化数据展示提升可读性插件库ffmt的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang 数据美化展示:ffmt 库使用指南

ffmt 是一个专注于提升 Go 语言数据展示可读性的格式化库,它能够将复杂的数据结构以更美观、更易读的方式呈现出来。

ffmt 主要特性

  1. 自动对齐数据结构中的字段
  2. 智能缩进和换行处理
  3. 支持颜色高亮显示
  4. 可自定义输出格式
  5. 支持各种 Go 数据类型

安装 ffmt

go get github.com/ffmt/ffmt

基础使用示例

package main

import (
	"github.com/ffmt/ffmt"
)

func main() {
	// 基本类型美化
	ffmt.P(123)        // 输出: 123
	ffmt.P("hello")    // 输出: "hello"
	ffmt.P(3.1415926)  // 输出: 3.1415926

	// 复杂数据结构
	data := map[string]interface{}{
		"name":    "John",
		"age":     30,
		"address": map[string]string{"city": "New York", "zip": "10001"},
		"scores":  []int{90, 85, 95},
	}

	ffmt.P(data)
}

高级功能

1. 带颜色输出

ffmt.Print("This is normal text")
ffmt.Mark("This is highlighted text")  // 黄色高亮
ffmt.Error("This is error text")      // 红色错误文本
ffmt.Info("This is info text")        // 蓝色信息文本
ffmt.Success("This is success text")  // 绿色成功文本

2. JSON 美化

jsonStr := `{"name":"John","age":30,"cars":["Ford","BMW","Fiat"]}`
ffmt.Json(jsonStr)

3. 表格输出

type Person struct {
	Name string
	Age  int
	City string
}

people := []Person{
	{"Alice", 25, "New York"},
	{"Bob", 30, "San Francisco"},
	{"Charlie", 35, "Chicago"},
}

ffmt.Table(people)

4. 自定义输出样式

// 设置全局选项
ffmt.Config(ffmt.ConfigOption{
	Color:  true,    // 启用颜色
	Indent: 4,       // 缩进4个空格
	Width:  80,      // 最大宽度80字符
})

// 临时选项
ffmt.Puts(data, ffmt.ConfigOption{
	Color:  false,
	Indent: 2,
})

5. 调试输出

func complexFunction() {
	// 标记开始
	ffmt.Debug("Entering complexFunction")
	defer ffmt.Debug("Leaving complexFunction")
	
	// 调试变量
	x := 42
	y := "answer"
	ffmt.Dump(x, y)  // 会输出变量名和值
}

实际应用场景

1. API 响应调试

resp, err := http.Get("https://api.example.com/data")
if err != nil {
	ffmt.Error("Request failed:", err)
	return
}
defer resp.Body.Close()

var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
	ffmt.Error("JSON decode failed:", err)
	return
}

ffmt.P("API Response:")
ffmt.Json(result)

2. 数据库查询结果展示

rows, err := db.Query("SELECT id, name, email FROM users LIMIT 5")
if err != nil {
	ffmt.Error("Query failed:", err)
	return
}
defer rows.Close()

var users []struct {
	ID    int
	Name  string
	Email string
}

for rows.Next() {
	var u struct {
		ID    int
		Name  string
		Email string
	}
	if err := rows.Scan(&u.ID, &u.Name, &u.Email); err != nil {
		ffmt.Error("Scan failed:", err)
		return
	}
	users = append(users, u)
}

ffmt.Table(users)

性能考虑

ffmt 主要设计用于开发和调试环境,不建议在生产环境中大量使用,因为它会比标准 fmt 包有额外的性能开销。对于生产环境日志,建议使用更专业的日志库如 logrus 或 zap。

总结

ffmt 提供了一种简单而强大的方式来提升 Go 程序中的数据展示可读性,特别适合在开发和调试阶段使用。通过其丰富的格式化选项和颜色支持,可以让你更直观地理解复杂数据结构的内容。

对于更高级的需求,你还可以结合其他库如 pretty (github.com/kr/pretty) 或 go-pretty (github.com/jedib0t/go-pretty) 来获得更多格式化选项。

回到顶部