向XML字符串添加正确的缩进

We've got a legacy system with XML where the XML is not pretty (i.e. entire file has no line breaks).

Is there a built in/native way to achieve pretty printing of XML in go? If not how does one go about achieving this?

You can use xml.MarshalIndent, for example :

package main

type xmldoc struct { ........ fields ...... }

func main() {
    var doc xmlDoc
    err := xml.Unmarshal([]byte(xml-data), &doc)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    out, err := xml.MarshalIndent(doc, "", "\t")
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    fmt.Println(out)
}

or from the command line you can always use xmlint :

$ xmllint --format --recover file.xml > formatted.xml

or do it in bulk check Format all XML files in a directory and save them in a subdirectory