August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, you will learn how to read an XML file from disk and store it into a Go structure. The name of the program is readXML.go and it will be presented in three parts. The first part of readXML.go is as follows:
package main
import (
"encoding/xml"
"fmt"
"os"
)
type Record struct {
Name string
Surname string
Tel []Telephone
}
type Telephone struct {
Mobile bool
Number string
}
The second part of readXML.go is the following:
func loadFromXML(filename string, key interface{}) error {
in, err := os.Open(filename)
if err != nil {
return err
}
decodeXML := xml.NewDecoder(in)
err = decodeXML.Decode(key)
if err != nil {
return err
}
in.Close()
return nil
}
The presented process is very similar to the way that ...
Read now
Unlock full access