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
+12.8k Golang : http.Get example
+15.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+6.9k Fix sudo yum hang problem with no output or error messages
+24.5k Golang : How to validate URL the right way
+29.7k Golang : Record voice(audio) from microphone to .WAV file
+10.2k Golang : Bcrypting password
+23.1k Golang : simulate tail -f or read last line from log file example
+16.3k Golang : Find out mime type from bytes in buffer
+5.7k List of Golang XML tutorials
+8.3k Useful methods to access blocked websites
+21.8k SSL : How to check if current certificate is sha1 or sha2