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
+14.5k Golang : How to check if your program is running in a terminal
+29.5k Golang : Saving(serializing) and reading file with GOB
+17.1k Golang : XML to JSON example
+16.6k Golang : Delete files by extension
+5.3k Javascript : Shuffle or randomize array example
+4.5k Java : Generate multiplication table example
+16.9k Golang : Read integer from file into array
+5k Python : Convert(cast) bytes to string example
+14.3k Golang : Get uploaded file name or access uploaded files
+17.7k Golang : [json: cannot unmarshal object into Go value of type]
+6.3k Golang : Detect face in uploaded photo like GPlus