method3.go 293 B

123456789101112131415161718192021222324
  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Point struct {
  7. x, y float64
  8. }
  9. func (p *Point) Abs() float64 {
  10. return math.Sqrt(p.x*p.x + p.y*p.y)
  11. }
  12. type NamedPoint struct {
  13. Point
  14. name string
  15. }
  16. func main() {
  17. n := &NamedPoint{Point{3, 4}, "Pythagoras"}
  18. fmt.Println(n.Abs()) // prints 5
  19. }