-
Notifications
You must be signed in to change notification settings - Fork 0
/
goPointersEx.go
93 lines (75 loc) · 2.04 KB
/
goPointersEx.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import "fmt"
type ElectionResult struct {
Name string
Votes int
}
// NewVoteCounter returns a new vote counter with
// a given number of initial votes.
func NewVoteCounter(initialVotes int) *int {
return &initialVotes
}
// VoteCount extracts the number of votes from a counter.
func VoteCount(counter *int) int {
if counter == nil {
return 0
}
return *counter
}
// IncrementVoteCount increments the value in a vote counter.
func IncrementVoteCount(counter *int, increment int) {
*counter += increment
}
// NewElectionResult creates a new election result.
func NewElectionResult(candidateName string, votes int) *ElectionResult {
return &ElectionResult{
candidateName,
votes,
}
}
// DisplayResult creates a message with the result to be displayed.
func DisplayResult(result *ElectionResult) string {
return fmt.Sprintf ("%s (%d)", result.Name, result.Votes)
// Remember that the dot operator works the same
// with a pointer to a struct as it does with the
// actual struct. You don't have to explicitly
// write (*result).
}
// DecrementVotesOfCandidate decrements by one the vote count of a candidate in a map.
func DecrementVotesOfCandidate(results map[string]int, candidate string) {
results[candidate]--
}
func main() {
// task 1:
counter := NewVoteCounter(2)
fmt.Println(*counter)
// task 2:
votes := 4
voteCounter := &votes
fmt.Println(VoteCount(voteCounter))
var nilVoteCounter *int
fmt.Println(VoteCount(nilVoteCounter))
// task 3:
votes = 3
IncrementVoteCount(voteCounter, 2)
fmt.Println(votes, voteCounter, *voteCounter)
// task 4:
fmt.Println(*NewElectionResult("Peter", 3))
fmt.Println(NewElectionResult("Peter", 3))
fmt.Println(NewElectionResult("Peter", 3).Name)
fmt.Println(NewElectionResult("Peter", 3).Votes)
// task 5:
result := &ElectionResult{
Name: "John",
Votes: 32,
}
fmt.Println(DisplayResult(result))
// task 6:
var finalResults = map[string]int{
"Mary": 10,
"John": 51,
}
DecrementVotesOfCandidate(finalResults, "Mary")
fmt.Println(finalResults["Mary"])
}
// Election Day