Standard library / encoding/xml

encoding/xml.Encoder.Indent()

Sets prefix and indentation for subsequent encoded XML. Expected stdout: '<a>\n <b>Go</b>\n</a>\n'.

Formal Syntax & Structure

go syntaxgo
func (enc *Encoder) Indent(prefix, indent string)

Parameters & Attributes

NameTypeDescription
prefixstringPrefix written before each indented XML line.
indentstringIndentation per nesting level.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/xml"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var out bytes.Buffer  enc := xml.NewEncoder(&out)  enc.Indent("", "  ")  must(enc.Encode(struct {    XMLName xml.Name `xml:"a"`    B       string   `xml:"b"`  }{B: "Go"}))  fmt.Println(out.String())}
Go Reference