import "electionday"
- func DecrementVotesOfCandidate(results map[string]int, candidate string)
- func DisplayResult(result *ElectionResult) string
- func IncrementVoteCount(counter *int, increment int)
- func NewVoteCounter(initialVotes int) *int
- func VoteCount(counter *int) int
- type ElectionResult
func DecrementVotesOfCandidate(results map[string]int, candidate string)
DecrementVotesOfCandidate decrements by one the vote count of a candidate in a map
Example
{
var finalResults = map[string]int{
"Mary": 10,
"John": 51,
}
DecrementVotesOfCandidate(finalResults, "Mary")
fmt.Println(finalResults["Mary"])
}
9
func DisplayResult
func DisplayResult(result *ElectionResult) string
DisplayResult creates a message with the result to be displayed
Example
{
var result *ElectionResult
result = &ElectionResult{
Name: "John",
Votes: 32,
}
message := DisplayResult(result)
fmt.Println(message)
}
John (32)
func IncrementVoteCount
func IncrementVoteCount(counter *int, increment int)
IncrementVoteCount increments the value in a vote counter
Example
{
var votes int
var voteCounter *int
votes = 3
voteCounter = &votes
IncrementVoteCount(voteCounter, 2)
fmt.Println(votes == 5)
fmt.Println(*voteCounter == 5)
}
true
true
func NewVoteCounter
func NewVoteCounter(initialVotes int) *int
NewVoteCounter returns a new vote counter with a given number of initial votes.
Example
{
var initialVotes int
var counter *int
initialVotes = 2
counter = NewVoteCounter(initialVotes)
fmt.Println(*counter == initialVotes)
}
true
func VoteCount
func VoteCount(counter *int) int
VoteCount extracts the number of votes from a counter.
Example
{
var votes int
votes = 3
var voteCounter *int
voteCounter = &votes
var nilVoteCounter *int
fmt.Println(VoteCount(voteCounter))
fmt.Println(VoteCount(nilVoteCounter))
}
3
0
type ElectionResult
ElectionResult represents an election result.
type ElectionResult struct {
// Name is the name of the candidate.
Name string
// Number is the total number of votes the candidate had.
Votes int
}
func NewElectionResult
func NewElectionResult(candidateName string, votes int) *ElectionResult
NewElectionResult creates a new election result
Example
{
var result *ElectionResult
result = NewElectionResult("Peter", 3)
fmt.Println(result.Name == "Peter")
fmt.Println(result.Votes == 3)
}
true
true
Generated by gomarkdoc