Golang write struct to XML file
The xml package has an
Marshal()
function which is used to serialized values from a struct and write them to a file in XML format.
Example
package main
import (
"encoding/xml"
"io/ioutil"
)
type notes struct {
To string `xml:"to"`
From string `xml:"from"`
Heading string `xml:"heading"`
Body string `xml:"body"`
}
func main() {
note := ¬es{To: "Nicky",
From: "Rock",
Heading: "Meeting",
Body: "Meeting at 5pm!",
}
file, _ := xml.MarshalIndent(note, "", " ")
_ = ioutil.WriteFile("notes1.xml", file, 0644)
}
Most Helpful This Week
GO Program to Swap Number Without Using Temporary Variables
How to create an empty Slice in Golang?
How do you read cookies in an HTTP request with an HTTP client in Go?
How to append struct member dynamically using Empty Interface?
How to convert Boolean Type to String in Go?
How do you handle HTTP authentication with an HTTP client in Go?