go_playground/5-go-by-example/19-methods.go

33 lines
662 B
Go
Raw Normal View History

2022-01-01 18:47:24 +00:00
// methods defined on struct types
package main
import "fmt"
type rect struct {
width, height int
}
// area method has a receiver type of *rect
func (r *rect) area() int {
return r.width * r.height
}
// Methods can be defined for either pointer or value receiver types
func (r rect) perim() int {
return 2*r.width + 2*r.height
}
func main() {
r := rect{width: 10, height: 5}
// call 2 methods defined for the struct
fmt.Println("area: ", r.area())
fmt.Println("perim:", r.perim())
// Go automatically handles conversion between values and pointers for method calls
rp := &r
fmt.Println("area: ", rp.area())
fmt.Println("perim:", rp.perim())
}