Introduction
This challenge aims to test your knowledge of working with XML in Golang. You will be required to create a struct that can be mapped to XML, marshal and unmarshal XML data, and use field tags to define the structure of the XML output.
This challenge aims to test your knowledge of working with XML in Golang. You will be required to create a struct that can be mapped to XML, marshal and unmarshal XML data, and use field tags to define the structure of the XML output.
You are required to create a struct named Plant
that can be mapped to XML. The struct should have the following fields:
Id
(int) - an XML attributeName
(string) - a nested XML elementOrigin
([]string) - a nested XML elementYou should also create a struct named Nesting
that contains a slice of Plant
structs. The Nesting
struct should be mapped to an XML element named nesting
, and the Plant
structs should be nested under <parent><child>...
.
You should then write code to marshal the Plant
and Nesting
structs to XML, and unmarshal XML data into the Plant
struct.
Plant
struct should be mapped to an XML element named plant
.Id
field of the Plant
struct should be mapped to an XML attribute named id
.Name
field of the Plant
struct should be mapped to a nested XML element named name
.Origin
field of the Plant
struct should be mapped to a nested XML element named origin
.Nesting
struct should be mapped to an XML element named nesting
.Plant
structs in the Nesting
slice should be nested under <parent><child>...
.$ go run xml.go
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
<?xml version="1.0" encoding="UTF-8"?>
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
Plant id=27, name=Coffee, origin=[Ethiopia Brazil]
<nesting>
<parent>
<child>
<plant id="27">
<name>Coffee</name>
<origin>Ethiopia</origin>
<origin>Brazil</origin>
</plant>
<plant id="81">
<name>Tomato</name>
<origin>Mexico</origin>
<origin>California</origin>
</plant>
</child>
</parent>
</nesting>
In this challenge, you learned how to work with XML in Golang. You created a struct that can be mapped to XML, marshaled and unmarshaled XML data, and used field tags to define the structure of the XML output.