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
+7.5k Golang : Detect sample rate, channels or latency with PortAudio
+5k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+34k Golang : Proper way to set function argument default value
+14.1k Javascript : Prompt confirmation before exit
+9.1k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+10.9k Golang : Sieve of Eratosthenes algorithm
+6.3k Golang : Calculate US Dollar Index (DXY)
+10.2k Golang : How to profile or log time spend on execution?
+9.6k Golang : Read file with ioutil
+9.9k Golang : Check if user agent is a robot or crawler example
+19.6k Golang : Set or Add HTTP Request Headers