优化JSON输出方式
This commit is contained in:
parent
33ce7169d0
commit
32f50cce5b
19
console.go
19
console.go
@ -21,6 +21,7 @@ import (
|
||||
// Date : 16:10 2023/4/4
|
||||
type console struct {
|
||||
enable bool
|
||||
formatJson bool
|
||||
}
|
||||
|
||||
// Enable 启用
|
||||
@ -41,6 +42,15 @@ func (c *console) Disable() {
|
||||
c.enable = false
|
||||
}
|
||||
|
||||
// FormatJson 是否格式化json
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:35 2023/4/4
|
||||
func (c *console) FormatJson(format bool) {
|
||||
c.formatJson = format
|
||||
}
|
||||
|
||||
// Output 禁用终端输出
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -50,8 +60,8 @@ func (c *console) Output(dataList ...interface{}) {
|
||||
if !c.enable {
|
||||
return
|
||||
}
|
||||
for idx, item := range dataList {
|
||||
fmt.Println(fmt.Sprintf("%v --> %v", idx, c.getDataStr(item)))
|
||||
for _, item := range dataList {
|
||||
fmt.Println(c.getDataStr(item))
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,8 +88,11 @@ func (c *console) getDataStr(data interface{}) string {
|
||||
case reflect.Array:
|
||||
fallthrough
|
||||
case reflect.Struct:
|
||||
var out bytes.Buffer
|
||||
byteData, _ := json.Marshal(data)
|
||||
if !c.formatJson {
|
||||
return string(byteData)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
_ = json.Indent(&out, []byte(string(byteData)+"\n"), "", "\t")
|
||||
return string(out.Bytes())
|
||||
case reflect.Func:
|
||||
|
Loading…
Reference in New Issue
Block a user