Golang custom encoder/decoder for XML to Golang struct
I’m consuming a REST service which gives various lists in both JSON and XML formats, something similar to these ones:
[
{
"id":803267,
"name":"Paris, Ile-de-France, France",
"region":"Ile-de-France",
"country":"France",
"is_day":1,
"localtime":"2018-05-12 12:53"
},
{
"id":760995,
"name":"Batignolles, Ile-de-France, France",
"region":"Ile-de-France",
"country":"France",
"is_day":0,
"localtime":"2018-05-12"
}
]
<?xml version="1.0" encoding="UTF-8"?>
<root>
<geo>
<id>803267</id>
<name>Paris, Ile-de-France, France</name>
<region>Ile-de-France</region>
<country>France</country>
<is_day>1</is_day>
<localtime>2018-05-12 12:53</localtime>
</geo>
<geo>
<id>760995</id>
<name>Batignolles, Ile-de-France, France</name>
<region>Ile-de-France</region>
<country>France</country>
<is_day>0</is_day>
<localtime>2018-05-12</localtime>
</geo>
</root>
And I wanted to get them into a slice of this type of structures:
type Locations []Location
type Location struct {
ID int `json:"id" xml:"id"`
Name string `json:"name" xml:"name"`
Region string `json:"region" xml:"region"`
Country string `json:"country" xml:"country"`
IsDay int `json:"is_day" xml:"is_day"`
LocalTime string `json:"localtime" xml:"localtime"`
}
It’s straight forward for JSON: Continue reading Unmarshal JSON and XML into the same Go structure slice with proper data type