Skip to content

Instantly share code, notes, and snippets.

@tomtsang
Created February 10, 2018 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomtsang/663c8034def3642497b5eed411a50b43 to your computer and use it in GitHub Desktop.
Save tomtsang/663c8034def3642497b5eed411a50b43 to your computer and use it in GitHub Desktop.
struct-anonymous-method.go
package main
import (
"fmt"
"math"
)
type Point struct {
x, y float64
}
func (p *Point) Abs() float64 {
return math.Sqrt(p.x*p.x + p.y*p.y)
}
type NamedPoint struct {
Point
name string
}
func main() {
pa := Point{3, 4}
n := &NamedPoint{pa, "Pythagoras"}
fmt.Println(pa.Abs()) // 打印5
fmt.Println(n.Abs()) // 打印5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment