Logo

Programming-Idioms

History of Idiom 104 > diff from v20 to v21

Edit summary for version 21 by programming-idioms.org:
[Go] ioutil -> io, os

Version 20

2022-04-20, 08:57:11

Version 21

2022-07-05, 22:28:55

Idiom #104 Save object into XML file

Write the contents of the object x into the file data.xml.

Idiom #104 Save object into XML file

Write the contents of the object x into the file data.xml.

Variables
x,data,xml
Variables
x,data,xml
Imports
import "encoding/xml"
import "io/ioutil"
Imports
import "encoding/xml"
import "os"
Code
buffer, err := xml.MarshalIndent(x, "", "  ")
if err != nil {
	return err
}
err = ioutil.WriteFile("data.xml", buffer, 0644)
Code
buffer, err := xml.MarshalIndent(x, "", "  ")
if err != nil {
	return err
}
err = os.WriteFile("data.xml", buffer, 0644)
Comments bubble
xml.MarshalIndent is more human-readable than xml.Marshal.
Comments bubble
xml.MarshalIndent is more human-readable than xml.Marshal.
Doc URL
https://golang.org/pkg/encoding/xml/#Marshal
Doc URL
https://golang.org/pkg/encoding/xml/#Marshal
Demo URL
http://play.golang.org/p/rS8oAtopNQ
Demo URL
https://go.dev/play/p/h5TRelZ3cq2