diff --git a/console.go b/console.go index 8e1c1a2..e6faafd 100644 --- a/console.go +++ b/console.go @@ -20,7 +20,8 @@ import ( // // Date : 16:10 2023/4/4 type console struct { - enable bool + 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: