Skip to content

Commit

Permalink
Example #7: marshaling to XML with indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Jul 1, 2020
1 parent fdf23a1 commit 46859c4
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lesson7/marshalling/07_xml_marshal_struct_4_indent.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 46859c4

Please sign in to comment.