From 5e2b1d1dc902ba53fc537b31e835d82c0e55dfb6 Mon Sep 17 00:00:00 2001 From: Philipp Tanlak Date: Thu, 17 Aug 2023 20:15:41 +0200 Subject: print json correctly --- utils.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'utils.go') diff --git a/utils.go b/utils.go index 7e59540..0ad6471 100644 --- a/utils.go +++ b/utils.go @@ -1,19 +1,24 @@ package flyscrape import ( + "bytes" "encoding/json" - "os" + "strings" ) -func PrettyPrint(v any) { - enc := json.NewEncoder(os.Stdout) +func PrettyPrint(v any, prefix string) string { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) enc.SetEscapeHTML(false) - enc.SetIndent("", " ") + enc.SetIndent(prefix, " ") enc.Encode(v) + return prefix + strings.TrimSuffix(buf.String(), "\n") } -func Print(v any) { - enc := json.NewEncoder(os.Stdout) +func Print(v any, prefix string) string { + var buf bytes.Buffer + enc := json.NewEncoder(&buf) enc.SetEscapeHTML(false) enc.Encode(v) + return prefix + strings.TrimSuffix(buf.String(), "\n") } -- cgit v1.2.3