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语言是差不多的