Golang encoding/xml.MarshalIndent() function example
package encoding/xml
MarshalIndent works like Marshal, but each XML element begins on a new indented line that starts with prefix and is followed by one or more copies of indent according to the nesting depth.
Golang encoding/xml.MarshalIndent() function example
package main
import (
"encoding/xml"
"fmt"
)
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
output, err := xml.MarshalIndent(v, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println(string(output))
}
Reference :
See also : Golang encoding/xml.Marshal() function examples
Advertisement
Something interesting
Tutorials
+15.3k nginx: [emerg] unknown directive "ssl"
+7.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+5.7k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+36.3k Golang : How to split or chunking a file to smaller pieces?
+16.7k Golang : Gzip file example
+10.2k Golang : How to profile or log time spend on execution?
+7.7k Golang : Command line ticker to show work in progress
+19.4k Golang : How to count the number of repeated characters in a string?
+10.2k Golang : Random Rune generator
+8.4k Your page has meta tags in the body instead of the head
+10.9k Golang : How to transmit update file to client by HTTP request example
+18.7k Golang : convert int to string