go by example part 2
This commit is contained in:
32
5-go-by-example/19-methods.go
Normal file
32
5-go-by-example/19-methods.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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())
|
||||
}
|
||||
Reference in New Issue
Block a user