package main import ( "fmt" "math" ) // 1) 定义结构体 type Point struct { X, Y float64 } // 2. 给 Point 添加 Distance 方法 // p.Distance(q) 算 p 到 q 的欧氏距离 func (p Point) Distance(q Point) float64 { dx := p.X - q.X dy := p.Y - q.Y return math.Sqrt(dx*dx + dy*dy) } func main() { // 3) 用字面量创建两个点 a := Point{X: 3, Y: 4} b := Point{X: 0, Y: 0} // 4) 调用方法 fmt.Printf("a=%v, b=%v, 距离=%.2f\n", a, b, a.Distance(b)) // 期望 5.00 }