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
How to access slice items in Golang?
Go program to find CNAME record of a domain
Top Programming Languages Behind Blockchain App Development
Send and Receive values from Channel
How to initialize a struct containing a slice of structs in Golang?
Program in Go language to Program to Add Two Matrix Using Multi-dimensional Arrays