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 supports the standard arithmetic operators: (Addition, Subtraction, Multiplication, Division,Remainder)
How do you handle HTTP errors in Go?
Golang write CSV records
How do you catch panic in Golang?
How do you handle HTTP server shutdown gracefully in Go?
How to check specific field exist in struct?