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
+19.9k Golang : Measure http.Get() execution time
+20.9k Golang : Convert PNG transparent background image to JPG or JPEG image
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+16.9k Golang : Set up source IP address before making HTTP request
+33.7k Golang : All update packages with go get command
+23.9k Golang : Fix type interface{} has no field or no methods and type assertions example
+15.2k JavaScript/JQuery : Detect or intercept enter key pressed example
+7.1k Restart Apache or Nginx web server without password prompt
+6.6k Golang : How to determine if request or crawl is from Google robots
+15.2k Golang : Get HTTP protocol version example
+16.1k Golang : Generate universally unique identifier(UUID) example
+22.1k Golang : Repeat a character by multiple of x factor