Skip to content

Instantly share code, notes, and snippets.

@yashhere
Last active April 17, 2020 09:11
Show Gist options
  • Save yashhere/f970e300a780756af56f441e69455161 to your computer and use it in GitHub Desktop.
Save yashhere/f970e300a780756af56f441e69455161 to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/a/44359967/5042046
func PrettyJson(data interface{}) (string, error) {
	const (
		empty = ""
		tab   = "\t"
	)

	buffer := new(bytes.Buffer)
	encoder := json.NewEncoder(buffer)
	encoder.SetIndent(empty, tab)

	err := encoder.Encode(data)
	if err != nil {
		return empty, err
	}
	return buffer.String(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment