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
| Name | Type | Description |
|---|---|---|
| prefix | string | Prefix written before each indented XML line. |
| indent | string | Indentation 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())}