From 46859c4faa796d03c43b8cd879f110606ea796f1 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Wed, 1 Jul 2020 09:16:37 +0200 Subject: [PATCH] Example #7: marshaling to XML with indentation --- .../07_xml_marshal_struct_4_indent.go | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lesson7/marshalling/07_xml_marshal_struct_4_indent.go diff --git a/lesson7/marshalling/07_xml_marshal_struct_4_indent.go b/lesson7/marshalling/07_xml_marshal_struct_4_indent.go new file mode 100644 index 0000000..dc6885f --- /dev/null +++ b/lesson7/marshalling/07_xml_marshal_struct_4_indent.go @@ -0,0 +1,50 @@ +package main + +import ( + "encoding/xml" + "fmt" +) + +type User1 struct { + XMLName xml.Name `xml:"user"` + id uint32 `xml:"id"` + name string `xml:"user_name"` + surname string `xml:"surname"` +} + +type User2 struct { + XMLName xml.Name `xml:"user"` + Id uint32 `xml:"id"` + Name string `xml:"user_name"` + Surname string `xml:"surname"` +} + +func main() { + user1 := User1{ + id: 1, + name: "Pepek", + surname: "Vyskoč"} + + user2 := User2{ + Id: 1, + Name: "Pepek", + Surname: "Vyskoč"} + + user1asXML, _ := xml.MarshalIndent(user1, "", " ") + fmt.Println(string(user1asXML)) + + fmt.Println() + + user2asXML, _ := xml.MarshalIndent(user2, "", " ") + fmt.Println(string(user2asXML)) + + fmt.Println() + + user2asXML, _ = xml.MarshalIndent(user2, "", "\t") + fmt.Println(string(user2asXML)) + + fmt.Println() + + user2asXML, _ = xml.MarshalIndent(user2, "\t", "\t") + fmt.Println(string(user2asXML)) +}