Functions and Methods
Methods
Methods attach functions to a type.
method receiver
A method receiver appears before the method name and tells Go which type the method belongs to.
Methods
methods_intro.go
Replay: real traced execution (multi-file project)
package main
import "fmt"
type Rectangle struct {
Width int
Height int
}
func (r Rectangle) Area() int {
return r.Width * r.Height
}
func main() {
var width = 4
shape := Rectangle{Width: width, Height: 3}
area := shape.Area()
fmt.Println("width=", shape.Width)
fmt.Println("height=", shape.Height)
fmt.Println("area=", area)
}
package main
import "fmt"
type Rectangle struct {
Width int
Height int
}
func (r Rectangle) Area() int {
return r.Width * r.Height
}
func main() {
var width = 2
shape := Rectangle{Width: width, Height: 3}
area := shape.Area()
fmt.Println("width=", shape.Width)
fmt.Println("height=", shape.Height)
fmt.Println("area=", area)
}
package main
import "fmt"
type Rectangle struct {
Width int
Height int
}
func (r Rectangle) Area() int {
return r.Width * r.Height
}
func main() {
var width = 6
shape := Rectangle{Width: width, Height: 3}
area := shape.Area()
fmt.Println("width=", shape.Width)
fmt.Println("height=", shape.Height)
fmt.Println("area=", area)
}
width ← 4, shape ← main.Rectangle{Width:4, Height:3}
14func main() {15 var width→ 4 = 4 //@width=2, 616 shape→ main.Rectangle{Width:4, Height:3} := Rectangle{Width: width4, Height: 3}17 area := shapemain.Rectangle{Width:4, Height:3}.Area()func (r Rectangle) Area() int
10func (r Rectangle) Area() int {11 return r.Width4 * r.Height312}area ← 12
16 shape := Rectangle{Width: width, Height: 3}17 area→ 12 := shapemain.Rectangle{Width:4, Height:3}.Area()1819 fmt.Println("width=", shape.Width4)20 fmt.Println("height=", shape.Height3)21 fmt.Println("area=", area12)22}outputwidth= 4 height= 3 area= 12
width ← 2, shape ← main.Rectangle{Width:2, Height:3}
14func main() {15 var width→ 2 = 216 shape→ main.Rectangle{Width:2, Height:3} := Rectangle{Width: width2, Height: 3}17 area := shapemain.Rectangle{Width:2, Height:3}.Area()func (r Rectangle) Area() int
10func (r Rectangle) Area() int {11 return r.Width2 * r.Height312}area ← 6
16 shape := Rectangle{Width: width, Height: 3}17 area→ 6 := shapemain.Rectangle{Width:2, Height:3}.Area()1819 fmt.Println("width=", shape.Width2)20 fmt.Println("height=", shape.Height3)21 fmt.Println("area=", area6)22}outputwidth= 2 height= 3 area= 6
width ← 6, shape ← main.Rectangle{Width:6, Height:3}
14func main() {15 var width→ 6 = 616 shape→ main.Rectangle{Width:6, Height:3} := Rectangle{Width: width6, Height: 3}17 area := shapemain.Rectangle{Width:6, Height:3}.Area()func (r Rectangle) Area() int
10func (r Rectangle) Area() int {11 return r.Width6 * r.Height312}area ← 18
16 shape := Rectangle{Width: width, Height: 3}17 area→ 18 := shapemain.Rectangle{Width:6, Height:3}.Area()1819 fmt.Println("width=", shape.Width6)20 fmt.Println("height=", shape.Height3)21 fmt.Println("area=", area18)22}outputwidth= 6 height= 3 area= 18
Follow the Receiver
widthstarts as4.shapestoresWidth: 4andHeight: 3.shape.Area()calls the method withshapeas the receiver.- The method returns
4 * 3. - The program prints
area= 12. | width | height | area | | --- | --- | --- | | 4 | 3 | 12 | | 2 | 3 | 6 | | 6 | 3 | 18 |
Exercise: methods_intro.go
Reproduce width= 4, height= 3, and area= 12, then use the pinned width variants 2 and 6 to predict area= 6 and area= 18.