refactor snake_case

This commit is contained in:
NunoSempere 2024-04-13 14:55:57 -04:00
parent 87c48c5f61
commit 08571ee9b6

24
main.go
View File

@ -24,26 +24,26 @@ func readStates() ([]State, error) {
var years = []string{"2000", "2004", "2008", "2012", "2016", "2020"}
/* Electoral college votes for the 2024 election*/
votesFilename := "data/electoral-college-votes.csv"
votesFile, err := os.Open(votesFilename)
votes_file, err := os.Open("data/electoral-college-votes.csv")
if err != nil {
return nil, fmt.Errorf("error opening the votes file: %v", err)
}
defer votesFile.Close()
votesReader := csv.NewReader(votesFile)
if _, err := votesReader.Read(); err != nil { // Skip header
defer votes_file.Close()
votes_reader := csv.NewReader(votes_file)
if _, err := votes_reader.Read(); err != nil { // Skip header
return nil, fmt.Errorf("error reading votes header: %v", err)
}
for {
record, err := votesReader.Read()
csv_record, err := votes_reader.Read()
if err != nil {
break // EOF or an error
}
votes, err := strconv.Atoi(record[1])
votes, err := strconv.Atoi(csv_record[1])
if err != nil {
continue // Error in converting votes, skip this record
}
state := record[0]
state := csv_record[0]
if _, exists := states[state]; !exists {
states[state] = State{Name: state, Votes: votes}
}
@ -51,13 +51,13 @@ func readStates() ([]State, error) {
/* Election results */
for _, year := range years {
resultsFilename := fmt.Sprintf("data/results/%s.csv", year)
resultsFile, err := os.Open(resultsFilename)
results_filename := fmt.Sprintf("data/results/%s.csv", year)
results_file, err := os.Open(results_filename)
if err != nil {
return nil, fmt.Errorf("error opening the results file for %s: %v", year, err)
}
defer resultsFile.Close()
resultsReader := csv.NewReader(resultsFile)
defer results_file.Close()
resultsReader := csv.NewReader(results_file)
if _, err := resultsReader.Read(); err != nil { // Skip header
return nil, fmt.Errorf("error reading results header for %s: %v", year, err)
}