Skip to content

Commit c715a7e

Browse files
elibenmmcgrana
authored andcommitted
Complete the example text, add .sh and generate page
1 parent 62bfb15 commit c715a7e

File tree

8 files changed

+361
-5
lines changed

8 files changed

+361
-5
lines changed

examples.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ String Functions
4646
String Formatting
4747
Regular Expressions
4848
JSON
49+
XML
4950
Time
5051
Epoch
5152
Time Formatting / Parsing

examples/xml/xml.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,69 @@
1+
// Go offers built-in support for XML and XML-like
2+
// formats with the `encoding.xml` package.
3+
14
package main
25

36
import (
47
"encoding/xml"
58
"fmt"
69
)
710

11+
// This type will be mapped to XML. Similarly to the
12+
// JSON examples, field tags contain directives for the
13+
// encoder and decoder. Here we use some special features
14+
// of the XML package: the `XMLName` field name dictates
15+
// the name of the XML element representing this struct;
16+
// `id,attr` means that the `Id` field is an XML
17+
// _attribute_ rather than a nested element.
818
type Plant struct {
9-
XMLName xml.Name `xml:"fruit"`
19+
XMLName xml.Name `xml:"plant"`
1020
Id int `xml:"id,attr"`
1121
Name string `xml:"name"`
1222
Origin []string `xml:"origin"`
1323
}
1424

1525
func (p Plant) String() string {
16-
return fmt.Sprintf("Fruit id=%v, name=%v, origin=%v",
26+
return fmt.Sprintf("Plant id=%v, name=%v, origin=%v",
1727
p.Id, p.Name, p.Origin)
1828
}
1929

2030
func main() {
2131
coffee := &Plant{Id: 27, Name: "Coffee"}
2232
coffee.Origin = []string{"Ethiopia", "Brazil"}
2333

34+
// Emit XML representing our plant; using
35+
// `MarshalIndent` to produce a more
36+
// human-readable output.
2437
out, _ := xml.MarshalIndent(coffee, " ", " ")
2538
fmt.Println(string(out))
2639

40+
// To add a generic XML header to the output, append
41+
// it explicitly.
42+
fmt.Println(xml.Header + string(out))
43+
44+
// Use `Unmarhshal` to parse a stream of bytes with XML
45+
// into a data structure. If the XML is malformed or
46+
// cannot be mapped onto Plant, a descriptive error
47+
// will be returned.
2748
var p Plant
2849
if err := xml.Unmarshal(out, &p); err != nil {
2950
panic(err)
3051
}
3152
fmt.Println(p)
53+
54+
tomato := &Plant{Id: 81, Name: "Tomato"}
55+
tomato.Origin = []string{"Mexico", "California"}
56+
57+
// The `parent>child>plant` field tag tells the encoder
58+
// to nest all `plant`s under `<parent><child>...`
59+
type Nesting struct {
60+
XMLName xml.Name `xml:"nesting"`
61+
Plants []*Plant `xml:"parent>child>plant"`
62+
}
63+
64+
nesting := &Nesting{}
65+
nesting.Plants = []*Plant{coffee, tomato}
66+
67+
out, _ = xml.MarshalIndent(nesting, " ", " ")
68+
fmt.Println(string(out))
3269
}

examples/xml/xml.hash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
18ada773098bca38778a58b438d6af70529f18b0
2+
qd9Ii_3AW0s

examples/xml/xml.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
$ go run xml.go
2+
<plant id="27">
3+
<name>Coffee</name>
4+
<origin>Ethiopia</origin>
5+
<origin>Brazil</origin>
6+
</plant>
7+
<?xml version="1.0" encoding="UTF-8"?>
8+
<plant id="27">
9+
<name>Coffee</name>
10+
<origin>Ethiopia</origin>
11+
<origin>Brazil</origin>
12+
</plant>
13+
Plant id=27, name=Coffee, origin=[Ethiopia Brazil]
14+
<nesting>
15+
<parent>
16+
<child>
17+
<plant id="27">
18+
<name>Coffee</name>
19+
<origin>Ethiopia</origin>
20+
<origin>Brazil</origin>
21+
</plant>
22+
<plant id="81">
23+
<name>Tomato</name>
24+
<origin>Mexico</origin>
25+
<origin>California</origin>
26+
</plant>
27+
</child>
28+
</parent>
29+
</nesting>

public/index.html

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/time

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)