type Person struct { Name string Age int } person := Person{Name: "Alice", Age: 25} file, err := os.Create("person.xml") if err != nil { fmt.Println("Failed to create file:", err) return } defer file.Close() encoder := xml.NewEncoder(file) encoder.Encode(person)
data := map[string]string{ "name": "Bob", "age": "30", "email": "[email protected]", } encoder := xml.NewEncoder(os.Stdout) encoder.Encode(data)In this example, we define a map called data with three key-value pairs. We create a new encoder that will write XML to the console using os.Stdout as the output stream. Finally, we call the Encode method of the encoder, passing in the data map, which writes the XML representation of the map to the console. In conclusion, the go encoding/xml package provides tools and methods for encoding and decoding XML data. It offers an Encoder struct with an Encode method that can take any value and output it as an XML representation.