格式化控制台输出

This commit is contained in:
白茶清欢 2023-04-04 16:30:40 +08:00
parent 5114db00c6
commit 33ce7169d0

View File

@ -8,6 +8,7 @@
package util package util
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"reflect" "reflect"
@ -49,11 +50,9 @@ func (c *console) Output(dataList ...interface{}) {
if !c.enable { if !c.enable {
return return
} }
list := make([]interface{}, 0) for idx, item := range dataList {
for _, item := range dataList { fmt.Println(fmt.Sprintf("%v --> %v", idx, c.getDataStr(item)))
list = append(list, c.getDataStr(item))
} }
fmt.Println(list...)
} }
// getDataStr 数据转换为字符串 // getDataStr 数据转换为字符串
@ -79,12 +78,13 @@ func (c *console) getDataStr(data interface{}) string {
case reflect.Array: case reflect.Array:
fallthrough fallthrough
case reflect.Struct: case reflect.Struct:
var out bytes.Buffer
byteData, _ := json.Marshal(data) byteData, _ := json.Marshal(data)
return string(byteData) _ = json.Indent(&out, []byte(string(byteData)+"\n"), "", "\t")
return string(out.Bytes())
case reflect.Func: case reflect.Func:
return dataValue.String() return dataValue.String()
default: default:
return fmt.Sprintf("%v", data) return fmt.Sprintf("%v", data)
} }
return ""
} }