From 32f50cce5bc3969fdb498309b9f37aa8b5a7def3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 4 Apr 2023 16:37:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96JSON=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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: