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
+9.4k Golang : Terminate-stay-resident or daemonize your program?
+80.7k Golang : How to return HTTP status code?
+7.1k Golang : Get environment variable
+20.2k Golang : Determine if directory is empty with os.File.Readdir() function
+11.6k Golang : Concurrency and goroutine example
+12.4k Golang : Search and extract certain XML data example
+13.9k Golang : How to determine if a year is leap year?
+7.3k Golang : Calculate how many weeks left to go in a given year
+15.2k Golang : Save(pipe) HTTP response into a file
+30.9k error: trying to remove "yum", which is protected
+10.3k Golang : Convert file unix timestamp to UTC time example
+8.3k Golang : Configure Apache and NGINX to access your Go service example