gopl-zh.github.com/ch6/ch6-02-1.md

3.1 KiB
Raw Blame History

6.2.1. Nil也是一個合法的接收器類型

就像一些函數允許nil指針作爲參數一樣方法理論上也可以用nil指針作爲其接收器尤其當nil對於對象來説是合法的零值時比如map或者slice。在下面的簡單int鏈表的例子里nil代表的是空鏈表

// An IntList is a linked list of integers.
// A nil *IntList represents the empty list.
type IntList struct {
    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()
}

當你定義一個允許nil作爲接收器值的方法的類型時在類型前面的註釋中指出nil變量代表的意義是很有必要的就像我們上面例子里做的這樣。

下面是net/url包里Values類型定義的一部分。

net/url
package url

// Values maps a string key to a list of values.
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 ""
}
// 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)
}

這個定義向外部暴露了一個map的類型的變量併且提供了一些能夠簡單操作這個map的方法。這個map的value字段是一個string的slice所以這個Values是一個多維map。客戶端使用這個變量的時候可以使用map固有的一些操作(make切片m[key]等等),也可以使用這里提供的操作方法,或者兩者併用,都是可以的:

gopl.io/ch6/urlvalues
m := url.Values{"lang": {"en"}} // direct construction
m.Add("item", "1")
m.Add("item", "2")

fmt.Println(m.Get("lang")) // "en"
fmt.Println(m.Get("q"))    // ""
fmt.Println(m.Get("item")) // "1"      (first value)
fmt.Println(m["item"])     // "[1 2]"  (direct map access)

m = nil
fmt.Println(m.Get("item")) // ""
m.Add("item", "3")         // panic: assignment to entry in nil map

對Get的最後一次調用中nil接收器的行爲卽是一個空map的行爲。我們可以等價地將這個操作寫成Value(nil).Get("item")但是如果你直接寫nil.Get("item")的話是無法通過編譯的因爲nil的字面量編譯器無法判斷其準備類型。所以相比之下最後的那行m.Add的調用就會産生一個panic因爲他嚐試更新一個空map。

由於url.Values是一個map類型併且間接引用了其key/value對因此url.Values.Add對這個map里的元素做任何的更新、刪除操作對調用方都是可見的。實際上就像在普通函數中一樣雖然可以通過引用來操作內部值但在方法想要脩改引用本身是不會影響原始值的比如把他置爲nil或者讓這個引用指向了其它的對象調用方都不會受影響。譯註因爲傳入的是存儲了內存地址的變量你改變這個變量是影響不了原始的變量的想想C語言是差不多的