forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1.go
46 lines (37 loc) · 1.12 KB
/
exercise1.go
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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Declare a struct that represents a baseball player. Include name, atBats and hits.
// Declare a method that calculates a players batting average. The formula is Hits / AtBats.
// Declare a slice of this type and initialize the slice with several players. Iterate over
// the slice displaying the players name and batting average.
package main
import "fmt"
// player represents a person in the game.
type player struct {
name string
atBats int
hits int
}
// average calculates the batting average for a player.
func (p *player) average() float64 {
if p.atBats == 0 {
return 0.0
}
return float64(p.hits) / float64(p.atBats)
}
func main() {
// Create a few players.
ps := []player{
{"bill", 10, 7},
{"jim", 12, 6},
{"ed", 6, 4},
}
// Display the batting average for each player.
for i := range ps {
fmt.Printf("%s: AVG[.%.f]\n", ps[i].name, ps[i].average()*1000)
}
// Why did I not choose this form?
for _, p := range ps {
fmt.Printf("%s: AVG[.%.f]\n", p.name, p.average()*1000)
}
}