ch6: fix code format

This commit is contained in:
chai2010
2016-01-21 10:08:07 +08:00
parent 20a8cf71b9
commit 2420954025
7 changed files with 90 additions and 88 deletions

View File

@@ -6,15 +6,15 @@
// An IntList is a linked list of integers.
// A nil *IntList represents the empty list.
type IntList struct {
Value int
Tail *IntList
Value int
Tail *IntList
}
// Sum returns the sum of the list elements.
func (list *IntList) Sum() int {
if list == nil {
return 0
}
return list.Value + list.Tail.Sum()
if list == nil {
return 0
}
return list.Value + list.Tail.Sum()
}
```
@@ -22,8 +22,8 @@ func (list *IntList) Sum() int {
下面是net/url包里Values類型定義的一部分。
<u><i>net/url</i></u>
```go
net/url
package url
// Values maps a string key to a list of values.
@@ -31,22 +31,22 @@ type Values map[string][]string
// Get returns the first value associated with the given key,
// or "" if there are none.
func (v Values) Get(key string) string {
if vs := v[key]; len(vs) > 0 {
return vs[0]
}
return ""
if vs := v[key]; len(vs) > 0 {
return vs[0]
}
return ""
}
// Add adds the value to key.
// It appends to any existing values associated with key.
func (v Values) Add(key, value string) {
v[key] = append(v[key], value)
v[key] = append(v[key], value)
}
```
這個定義向外部暴露了一個map的類型的變量併且提供了一些能夠簡單操作這個map的方法。這個map的value字段是一個string的slice所以這個Values是一個多維map。客戶端使用這個變量的時候可以使用map固有的一些操作(make切片m[key]等等),也可以使用這里提供的操作方法,或者兩者併用,都是可以的:
<u><i>gopl.io/ch6/urlvalues</i></u>
```go
gopl.io/ch6/urlvalues
m := url.Values{"lang": {"en"}} // direct construction
m.Add("item", "1")
m.Add("item", "2")