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
+15.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+17.6k Golang : delete and modify XML file content
+4.8k Golang : A program that contain another program and executes it during run-time
+13.3k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+9.1k Golang : Gonum standard normal random numbers example
+13.2k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+19.1k Mac OSX : Homebrew and Golang
+6.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+8.7k Golang : How to join strings?
+18.7k Unmarshal/Load CSV record into struct in Go
+9.1k Golang : Intercept and compare HTTP response code example
+7.9k Golang : Ways to recover memory during run time.