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
+6.4k Golang : How to search a list of records or data structures
+11.6k Golang : Fuzzy string search or approximate string matching example
+15.3k nginx: [emerg] unknown directive "ssl"
+17.9k Golang : Qt image viewer example
+25.7k Golang : missing Mercurial command
+9.2k Golang : How to control fmt or log print format?
+15k Golang : How do I get the local IP (non-loopback) address ?
+20.5k nginx: [emerg] unknown directive "passenger_enabled"
+30.6k Golang : Remove characters from string example
+8.8k Android Studio : Image button and button example
+5.8k Javascript : How to replace HTML inside <div>?
+11k Golang : Generate random elements without repetition or duplicate