Golang encoding/xml.Encoder type and NewEncoder() function example
package encoding/xml
An Encoder writes XML data to an output stream and NewEncoder returns a new encoder to write to.
Golang encoding/xml.Encoder type and NewEncoder() usage function
package main
import (
"encoding/xml"
"fmt"
"os"
)
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
encoder := xml.NewEncoder(os.Stdout)
if err := encoder.Encode(v); err != nil {
fmt.Println(err)
}
}
Reference :
Advertisement
Something interesting
Tutorials
+8.8k Golang : Accept any number of function arguments with three dots(...)
+24.5k Golang : Time slice or date sort and reverse sort example
+6.9k Golang : How to solve "too many .rsrc sections" error?
+8.2k Golang : Find relative luminance or color brightness
+4.8k PHP : Extract part of a string starting from the middle
+28k Golang : Move file to another directory
+6k Linux/MacOSX : Search for files by filename and extension with find command
+23.5k Golang : Read a file into an array or slice example
+7.7k Golang : Error reading timestamp with GORM or SQL driver
+9.3k Golang : Generate random Chinese, Japanese, Korean and other runes
+22.4k Golang : How to read JPG(JPEG), GIF and PNG files ?
+8.7k Golang : Combine slices but preserve order example