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
+11.3k Golang : How to flush a channel before the end of program?
+7.7k Golang : Command line ticker to show work in progress
+15.6k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+7.7k Golang : Generate human readable password
+11.2k Google Maps URL parameters configuration
+12.2k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+7.9k Golang Hello World Example
+12.8k Golang : Listen and Serve on sub domain example
+16.3k Golang : Find out mime type from bytes in buffer
+12.3k Golang : 2 dimensional array example
+11.7k Golang : Calculations using complex numbers example
+16.1k Golang : Generate universally unique identifier(UUID) example