-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodschallenge03.go
More file actions
62 lines (46 loc) · 1.37 KB
/
methodschallenge03.go
File metadata and controls
62 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Alta3 Research | RZFeeser@alta3.com
Understanding Go Receiver Functions (i.e. methods)
func(receiver_name Type) method_name(parameter_list)(return_type){
// Code
}
*/
package main
import "fmt"
type Player struct {
Lives int
Stage int
Inventory []string
}
// recv function to add a life
func (p *Player) Greenmushroom() {
p.Lives++
}
// recv function to add an inventory item
func (p *Player) Pickup(powerup string) {
p.Inventory = append(p.Inventory, powerup)
}
// rec function to check on the current stasge
func (p Player) CanWhistle() bool {
return p.Stage >= 5
}
func main() {
mario := Player{3, 1, []string{"mushroom"}}
// display mario's current lives
fmt.Println(mario.Lives)
// mario just touched a greenmushroom!
mario.Greenmushroom()
// display mario's current lives have changed
fmt.Println(mario.Lives)
// display mario's current inventory
fmt.Println(mario.Inventory)
// mario just picked up a flower!
mario.Pickup("flower")
// display mario's current inventory has changed
fmt.Println(mario.Inventory)
// below stage 5 mario cannot use the warp whistle
fmt.Println(mario.CanWhistle())
// move to a stage where mario can use the warp whistle
mario.Stage = 7
// mario can only use the warp whistle on or above stage 5
fmt.Println(mario.CanWhistle())
}