February 2019
Intermediate to advanced
450 pages
11h 59m
English
Working with XML is very similar to JSON. In fact, as XML and Go share the semantics of capitalizing their public variable names, there's less mapping annotation required, so the struct only requires a single mapping tag:
type EmailMessage struct { Subject string Content string To Email From Email Date time.Time `xml:"Sent"`}
Encoding and decoding are almost identical; obviously, we need to create xml.Encoder and xml.Decoder rather than the JSON counterparts. The only other difference is the method call to set the indenting (only required for pretty printing):
fmt.Println("To XML:")encoder := xml.NewEncoder(os.Stdout)encoder.Indent("", " ")encoder.Encode(email)
And, we can use a web service to provide us with XML to decode (the URL is ...
Read now
Unlock full access