methods for struct _ golang
Go supports methods defined on struct types
package main import ( "fmt" ) type rect struct { width, height int } func (r *rect) area() int { return r.width * r.height } func (r rect) perim() int { return 2*r.width + 2*r.height } func main() { r := rect{width: 10, height: 5} fmt.Println("area : ", r.area()) fmt.Println("perim : ", r.perim()) rp := &r fmt.Println("area : ", rp.area()) fmt.Println("perim : ", rp.perim()) }
area : 50 perim : 30 area : 50 perim : 30
总结 :
1 : struct 的指针也能直接引用 struct 的方法