package main import ( "encoding/csv" "fmt" "os" "strconv" "strings" ) type State struct { Name string Votes int Party2000 string Party2004 string Party2008 string Party2012 string Party2016 string Party2020 string } func readStates() ([]State, error) { var states map[string]State = make(map[string]State) 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) 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 return nil, fmt.Errorf("error reading votes header: %v", err) } for { record, err := votesReader.Read() if err != nil { break // EOF or an error } votes, err := strconv.Atoi(record[1]) if err != nil { continue // Error in converting votes, skip this record } state := record[0] if _, exists := states[state]; !exists { states[state] = State{Name: state, Votes: votes} } } /* Election results */ for _, year := range years { resultsFilename := fmt.Sprintf("data/results/%s.csv", year) resultsFile, err := os.Open(resultsFilename) if err != nil { return nil, fmt.Errorf("error opening the results file for %s: %v", year, err) } defer resultsFile.Close() resultsReader := csv.NewReader(resultsFile) if _, err := resultsReader.Read(); err != nil { // Skip header return nil, fmt.Errorf("error reading results header for %s: %v", year, err) } for { record, err := resultsReader.Read() if err != nil { break // EOF or an error } state, party := record[0], record[1] data, exists := states[state] if !exists { continue // State not found in votes map, skip } // Update the party winning in the specific year switch year { case "2000": data.Party2000 = party case "2004": data.Party2004 = party case "2008": data.Party2008 = party case "2012": data.Party2012 = party case "2016": data.Party2016 = party case "2020": data.Party2020 = party } states[state] = data } } // Convert statesData map to a slice for returning var dataSlice []State for _, data := range states { dataSlice = append(dataSlice, data) } return dataSlice, nil } type VotesForEachParty struct { Democrats int Republicans int } func sampleFromState(state State) VotesForEachParty { switch state.Name { case "Nebraska": return VotesForEachParty{Democrats: 1, Republicans: 0} case "Maine": return VotesForEachParty{Democrats: 1, Republicans: 0} default: return VotesForEachParty{Democrats: 1, Republicans: 0} } } func main() { states, err := readStates() if err != nil { fmt.Println("Error:", err) return } // Example states to query example_state_names := []string{"California", "Texas", "Florida"} for _, states := range states { for _, example_state_name := range example_state_names { if strings.EqualFold(states.Name, example_state_name) { fmt.Printf("%s: Votes: %d, Winners: 2000 - %s, 2004 - %s, 2008 - %s, 2012 - %s, 2016 - %s, 2020 - %s\n", states.Name, states.Votes, states.Party2000, states.Party2004, states.Party2008, states.Party2012, states.Party2016, states.Party2020) break } } } }