格式化控制台输出

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