Standard library / encoding/xml

encoding/xml.MarshalIndent()

Encodes XML with a specified prefix and indentation. Expected stdout: '<user>\n <name>Ada</name>\n</user>\n'.

Formal Syntax & Structure

go syntaxgo
func MarshalIndent(v any, prefix, indent string) ([]byte, error)

Parameters & Attributes

NameTypeDescription
vanyValue to encode or pointer to the destination for decoding.
prefixstringPrefix written before each indented XML line.
indentstringIndentation per nesting level.

Return Value

([]byte, error)

Resulting bytes (possibly partial for decoding) and error.

Working Example

Example Snippetgo
package main
import (  "encoding/xml"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  p, e := xml.MarshalIndent(struct {    XMLName xml.Name `xml:"user"`    Name    string   `xml:"name"`  }{Name: "Ada"}, "", "  ")  must(e)  fmt.Println(string(p))}
Go Reference