转为 mdbook

This commit is contained in:
chai2010
2022-08-04 14:58:52 +08:00
parent 7fa86ea953
commit 06a1bdf735
49 changed files with 3695 additions and 236 deletions

View File

@@ -91,4 +91,69 @@ pptr.Distance(q) // implicit (*pptr)
1. 不管你的method的receiver是指针类型还是非指针类型都是可以通过指针/非指针类型进行调用的,编译器会帮你做类型转换。
2. 在声明一个method的receiver该是指针还是非指针类型时你需要考虑两方面的因素第一方面是这个对象本身是不是特别大如果声明为非指针变量时调用会产生一次拷贝第二方面是如果你用指针类型作为receiver那么你一定要注意这种指针类型指向的始终是一块内存地址就算你对其进行了拷贝。熟悉C或者C++的人这里应该很快能明白。
{% include "./ch6-02-1.md" %}
### 6.2.1. Nil也是一个合法的接收器类型
就像一些函数允许nil指针作为参数一样方法理论上也可以用nil指针作为其接收器尤其当nil对于对象来说是合法的零值时比如map或者slice。在下面的简单int链表的例子里nil代表的是空链表
```go
// 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类型定义的一部分。
<u><i>net/url</i></u>
```go
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]等等),也可以使用这里提供的操作方法,或者两者并用,都是可以的:
<u><i>gopl.io/ch6/urlvalues</i></u>
```go
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语言是差不多的

View File

@@ -1,4 +1,4 @@
# 第章 方法
# 第6章 方法
从90年代早期开始面向对象编程OOP就成为了称霸工程界和教育界的编程范式所以之后几乎所有大规模被应用的语言都包含了对OOP的支持go语言也不例外。