mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-12-20 12:44:20 +08:00
ch6: fix code format
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user