-
Notifications
You must be signed in to change notification settings - Fork 4
/
15_xml_and_json.go
42 lines (35 loc) · 1.03 KB
/
15_xml_and_json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Padesátá sedmá část
// Serializace a deserializace datových struktur v programovacím jazyce Go
// https://www.root.cz/clanky/serializace-a-deserializace-datovych-struktur-v-programovacim-jazyce-go/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z padesáté sedmé části:
// https://github.com/tisnik/go-root/blob/master/article_57/README.md
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
)
type User struct {
XMLName xml.Name `xml:"user" json:"-"`
Id uint32 `xml:"id" json:"user_id"`
Name string `xml:"name>first" json:"user_name"`
Surname string `xml:"name>last" json:"surname"`
}
func main() {
user := User{
Id: 1,
Name: "Pepek",
Surname: "Vyskoč"}
userAsXML, _ := xml.MarshalIndent(user, "", " ")
fmt.Println(string(userAsXML))
fmt.Println()
userAsJSON, _ := json.Marshal(user)
fmt.Println(string(userAsJSON))
}