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

@@ -4,8 +4,8 @@
下面來寫我們第一個方法的例子這個例子在package geometry下
<u><i>gopl.io/ch6/geometry</i></u>
```go
gopl.io/ch6/geometry
package geometry
import "math"
@@ -14,15 +14,13 @@ type Point struct{ X, Y float64 }
// traditional function
func Distance(p, q Point) float64 {
return math.Hypot(q.X-p.X, q.Y-p.Y)
return math.Hypot(q.X-p.X, q.Y-p.Y)
}
// same thing, but as a method of the Point type
func (p Point) Distance(q Point) float64 {
return math.Hypot(q.X-p.X, q.Y-p.Y)
return math.Hypot(q.X-p.X, q.Y-p.Y)
}
```
上面的代碼里那個附加的參數p叫做方法的接收器(receiver),早期的面向對象語言留下的遺産將調用一個方法稱爲“向一個對象發送消息”。