Logo

Programming-Idioms

History of Idiom 103 > diff from v10 to v11

Edit summary for version 11 by programming-idioms.org:
[Go] +imports

Version 10

2019-10-07, 22:54:44

Version 11

2019-12-17, 15:57:13

Idiom #103 Load XML file into struct

Read from file data.xml and write its content into object x.
Assume the XML data is suitable for the type of x.

Idiom #103 Load XML file into struct

Read from file data.xml and write its content into object x.
Assume the XML data is suitable for the type of x.

Imports
import "encoding/xml"
Imports
import "encoding/xml"
import "io/ioutil"
Code
buffer, err := ioutil.ReadFile("data.xml")
if err != nil {
	return err
}
err = xml.Unmarshal(buffer, &x)
if err != nil {
	return err
}
Code
buffer, err := ioutil.ReadFile("data.xml")
if err != nil {
	return err
}
err = xml.Unmarshal(buffer, &x)
if err != nil {
	return err
}
Comments bubble
buffer is a []byte.
&x is the address of x.
You must check errors after each step.
Comments bubble
buffer is a []byte.
&x is the address of x.
You must check errors after each step.
Doc URL
https://golang.org/pkg/encoding/xml/#Unmarshal
Doc URL
https://golang.org/pkg/encoding/xml/#Unmarshal
Demo URL
http://play.golang.org/p/Ee6nItesnR
Demo URL
http://play.golang.org/p/Ee6nItesnR