Skip to content

Commit

Permalink
Example #13: marshaling array of structs into XML, node names
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Jul 2, 2020
1 parent dc6fea5 commit 9363e1c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lesson7/marshalling/13_xml_marshal_array_of_struct_3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"encoding/xml"
"fmt"
)

type User struct {
XMLName xml.Name `xml:"user"`
Id uint32 `xml:"id"`
Name string `xml:"user_name"`
Surname string `xml:"surname"`
}

type Users struct {
XMLName xml.Name `xml:"users"`
List []User
}

func main() {
var users Users = Users{
List: []User{
User{
Id: 1,
Name: "Pepek",
Surname: "Vyskoč"},
User{
Id: 2,
Name: "Pepek",
Surname: "Vyskoč"},
User{
Id: 3,
Name: "Josef",
Surname: "Vyskočil"},
},
}

usersAsXML, _ := xml.MarshalIndent(users, "", " ")
fmt.Println(string(usersAsXML))
}

0 comments on commit 9363e1c

Please sign in to comment.