Golang encoding/xml.Encoder.Encoder() function example
package encoding/xml
Encode writes the XML encoding of v to the stream.
See the documentation for Marshal for details about the conversion of Go values to XML.
Encode calls Flush before returning.
Golang encoding/xml.Encoder.Encoder() function usage example
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 { // <-- here
fmt.Println(err)
}
}
Reference :
Advertisement
Something interesting
Tutorials
+21.2k Golang : Convert(cast) string to rune and back to string example
+5.4k Golang : Return multiple values from function
+10k Golang : Read file and convert content to string
+16.9k Golang : How to generate QR codes?
+18.8k Golang : Implement getters and setters
+11.6k Golang : Display a text file line by line with line number example
+12.3k Golang : Display list of countries and ISO codes
+9.4k Golang : Timeout example
+8.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+31.6k Golang : Get local IP and MAC address
+34.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+25.7k Golang : How to write CSV data to file