package main import "encoding/csv" import "fmt" import "io" import "log" import "os" import "strconv" type StateData struct { id int name string abbreviation string country string type_state string sort int status string occupied string notes string fips_state string assoc_press string standard_federal_region string census_region int census_region_name string census_division int census_division_name string circuit_court int } func readCsvHeader(line []string) (map[string]int, error) { columns := make(map[string]int) for i, col := range line { columns[col] = i } return columns, nil } func readCsvLine(line []string, columns map[string]int) (*StateData, error) { var err error data := new(StateData) for i, col := range line { switch i { case columns["id"]: data.id, err = strconv.Atoi(col) if err != nil { return data, err } case columns["name"]: data.name = col case columns["abbreviation"]: data.abbreviation = col case columns["country"]: data.country = col case columns["type"]: data.type_state = col case columns["sort"]: data.sort, err = strconv.Atoi(col) if err != nil { return data, err } case columns["status"]: data.status = col case columns["occupied"]: data.occupied = col case columns["notes"]: data.notes = col case columns["fips_state"]: data.fips_state = col case columns["assoc_press"]: data.assoc_press = col case columns["standard_federal_region"]: data.standard_federal_region = col case columns["census_region"]: data.census_region, err = strconv.Atoi(col) if err != nil { return data, err } case columns["census_region_name"]: data.census_region_name = col case columns["census_division"]: data.census_division, err = strconv.Atoi(col) if err != nil { return data, err } case columns["census_division_name"]: data.census_division_name = col case columns["circuit_court"]: data.circuit_court, err = strconv.Atoi(col) if err != nil { return data, err } } } return data, nil } func readCsv(in io.Reader) map[string]*StateData { data := make(map[string]*StateData) csvReader := csv.NewReader(in) var header map[string]int for { line, err := csvReader.Read() if err == io.EOF { break } else if err != nil { log.Fatal(err) } if len(header) == 0 { header, err = readCsvHeader(line) if err != nil { log.Fatal(err) } } else { d, err := readCsvLine(line, header) if err != nil { log.Println("Invalid line", err) // skip } else { data[d.abbreviation] = d } } } return data } func printStateHtml(state *StateData) { fmt.Println("
Abbrev. | Name | `) } func printFooter() { fmt.Println(`
---|