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
+18.5k Golang : Example for RSA package functions
+9.2k Golang : does not implement flag.Value (missing Set method)
+6.3k Apt-get to install and uninstall Golang
+7.5k Golang : Handling Yes No Quit query input
+11k Golang : Create S3 bucket with official aws-sdk-go package
+15.2k Golang : Get HTTP protocol version example
+10.6k Android Studio : Simple input textbox and intercept key example
+5.7k Golang : Struct field tags and what is their purpose?
+7k Golang : Levenshtein distance example
+22.2k Golang : Securing password with salt