Skip to content

Commit e485b8f

Browse files
committed
Method chaining design pattern in golang
1 parent bb3f14b commit e485b8f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Player_Stat struct {
8+
GamesPlayed float64
9+
GoalScored float64
10+
ShotOnTarget float64
11+
Assist float64
12+
}
13+
14+
func (s *Player_Stat) GoalsRatio(goals, gamesplayed float64) *Player_Stat {
15+
s.GoalScored = goals
16+
s.GamesPlayed = gamesplayed
17+
18+
Goalsratio := s.GoalScored / s.GamesPlayed
19+
fmt.Printf("\nPlayer goal ratio is : %f", Goalsratio)
20+
return s
21+
22+
}
23+
24+
func (s *Player_Stat) SOT_Ratio(Sot float64) *Player_Stat {
25+
s.ShotOnTarget = Sot
26+
sotRatio := float64(s.ShotOnTarget / s.GamesPlayed)
27+
println(s.ShotOnTarget)
28+
29+
fmt.Printf("\nThe player shot on target ratio is : %f", sotRatio)
30+
31+
return s
32+
}
33+
34+
func (s *Player_Stat) AssistRatio(assist float64) {
35+
36+
s.Assist = assist
37+
38+
AssistRatio := s.Assist / s.GamesPlayed
39+
40+
fmt.Printf("\nThe assist ratio is : %f", AssistRatio)
41+
}
42+
43+
func main() {
44+
ps := Player_Stat{}
45+
ps.GoalsRatio(20, 40).SOT_Ratio(55).AssistRatio(15)
46+
}

0 commit comments

Comments
 (0)