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
Web Application to Get Trending Hashtags Near a Location
Struct Instantiation using new keyword
Find Type of Struct in Go Programming Language
How do you handle HTTP client caching in Go?
How do you handle HTTP Client server load balancing in Go?
Go program to find Forward(A) record of a domain
Program in Go language to print Floyd's Triangle
Golang program for implementation of Levenshtein distance
How do you create an HTTP server in Go?
Go language Best practices to follow in 2023