Add en/0.7.x.md syntax highlighting

This commit is contained in:
vCaesar
2017-06-06 21:44:08 +08:00
parent f9dfd25650
commit ceb9768171
6 changed files with 126 additions and 124 deletions

View File

@@ -5,7 +5,7 @@ XML is a commonly used data communication format in web services. Today, it's as
I will not make any attempts to teach XML's syntax or conventions. For that, please read more documentation about XML itself. We will only focus on how to encode and decode XML files in Go.
Suppose you work in IT, and you have to deal with the following XML configuration file:
```xml
<?xml version="1.0" encoding="utf-8"?>
<servers version="1">
<server>
@@ -17,7 +17,7 @@ Suppose you work in IT, and you have to deal with the following XML configuratio
<serverIP>127.0.0.2</serverIP>
</server>
</servers>
```
The above XML document contains two kinds of information about your server: the server name and IP. We will use this document in our following examples.
## Parse XML
@@ -29,7 +29,7 @@ How do we parse this XML document? We can use the `Unmarshal` function in Go's `
the `data` parameter receives a data stream from an XML source, and `v` is the structure you want to output the parsed XML to. It is an interface, which means you can convert XML to any structure you desire. Here, we'll only talk about how to convert from XML to the `struct` type since they share similar tree structures.
Sample code:
```Go
package main
import (
@@ -73,9 +73,9 @@ Sample code:
fmt.Println(v)
}
```
XML is actually a tree data structure, and we can define a very similar structure using structs in Go, then use `xml.Unmarshal` to convert from XML to our struct object. The sample code will print the following content:
```xml
{{ servers} 1 [{{ server} Shanghai_VPN 127.0.0.1} {{ server} Beijing_VPN 127.0.0.2}]
<server>
<serverName>Shanghai_VPN</serverName>
@@ -86,11 +86,11 @@ XML is actually a tree data structure, and we can define a very similar structur
<serverIP>127.0.0.2</serverIP>
</server>
}
```
We use `xml.Unmarshal` to parse the XML document to the corresponding struct object. You should see that we have something like `xml:"serverName"` in our struct. This is a feature of structs called `struct tags` for helping with reflection. Let's see the definition of `Unmarshal` again:
```Go
func Unmarshal(data []byte, v interface{}) error
```
The first argument is an XML data stream. The second argument is storage type and supports the struct, slice and string types. Go's XML package uses reflection for data mapping, so all fields in v should be exported. However, this causes a problem: how does it know which XML field corresponds to the mapped struct field? The answer is that the XML parser parses data in a certain order. The library will try to find the matching struct tag first. If a match cannot be found then it searches through the struct field names. Be aware that all tags, field names and XML elements are case sensitive, so you have to make sure that there is a one-to-one correspondence for the mapping to succeed.
Go's reflection mechanism allows you to use this tag information to reflect XML data to a struct object. If you want to know more about reflection in Go, please read the package documentation on struct tags and reflection.
@@ -116,14 +116,14 @@ Note that all fields in structs should be exported (capitalized) in order to par
## Produce XML
What if we want to produce an XML document instead of parsing one. How do we do this in Go? Unsurprisingly, the `xml` package provides two functions which are `Marshal` and `MarshalIndent`, where the second function automatically indents the marshalled XML document. Their definition as follows:
```Go
func Marshal(v interface{}) ([]byte, error)
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
```
The first argument in both of these functions is for storing a marshalled XML data stream.
Let's look at an example to see how this works:
```Go
package main
import (
@@ -155,9 +155,9 @@ Let's look at an example to see how this works:
os.Stdout.Write(output)
}
```
The above example prints the following information:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<servers version="1">
<server>
@@ -169,7 +169,7 @@ The above example prints the following information:
<serverIP>127.0.0.2</serverIP>
</server>
</servers>
```
As we've previously defined, the reason we have `os.Stdout.Write([]byte(xml.Header))` is because both `xml.MarshalIndent` and `xml.Marshal` do not output XML headers on their own, so we have to explicitly print them in order to produce XML documents correctly.
Here we can see that `Marshal` also receives a v parameter of type `interface{}`. So what are the rules when marshalling to an XML document?
@@ -198,7 +198,7 @@ Then we need to figure out how to set tags in order to produce the final XML doc
- If a tag contains `",comment"`, it prints it as a comment without escaping, so you cannot have "--" in its value.
- If a tag contains `"omitempty"`, it omits this field if its value is zero-value, including false, 0, nil pointer or nil interface, zero length of array, slice, map and string.
- If a tag contains `"a>b>c"`, it prints three elements where a contains b and b contains c, like in the following code:
```xml
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
@@ -206,7 +206,7 @@ Then we need to figure out how to set tags in order to produce the final XML doc
<first>Asta</first>
<last>Xie</last>
</name>
```
You may have noticed that struct tags are very useful for dealing with XML, and the same goes for the other data formats we'll be discussing in the following sections. If you still find that you have problems with working with struct tags, you should probably read more documentation about them before diving into the next section.
## Links