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:
@@ -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),早期的面向對象語言留下的遺産將調用一個方法稱爲“向一個對象發送消息”。
|
||||
|
||||
Reference in New Issue
Block a user