diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-17 20:15:41 +0200 |
|---|---|---|
| committer | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-17 20:15:41 +0200 |
| commit | 5e2b1d1dc902ba53fc537b31e835d82c0e55dfb6 (patch) | |
| tree | aad5c1904e2a552672606c739a9dec595b3326ef /utils.go | |
| parent | 8a44dc0856d7bf2cdc2eafa8594f4a47d488c3fd (diff) | |
print json correctly
Diffstat (limited to 'utils.go')
| -rw-r--r-- | utils.go | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -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") } |