Created
February 10, 2018 08:07
-
-
Save tomtsang/663c8034def3642497b5eed411a50b43 to your computer and use it in GitHub Desktop.
struct-anonymous-method.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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