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
| Name | Type | Description |
|---|---|---|
| v | any | Value to encode or pointer to the destination for decoding. |
| prefix | string | Prefix written before each indented XML line. |
| indent | string | Indentation 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))}