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
Golang program for implementation of Levenshtein distance
What is an HTTP client in Go?
GO Program to Generate Fibonacci Sequence Up to a Certain Number
Example: How to use TeeReader from IO Package in Golang?
Defining a type that satisfies an interface in Go Programming Language
Find odd and even numbers using goroutines and channels