Created
August 22, 2017 22:12
-
-
Save hectorgool/3118dd38a5d912db031e943c7d47cc08 to your computer and use it in GitHub Desktop.
Read csv file format from http://download.geonames.org/export/zip/MX.zip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
twitter@hector_gool | |
Yoy can download MX.txt file from the next site: | |
http://download.geonames.org/export/zip/MX.zip | |
*/ | |
package main | |
import ( | |
"encoding/csv" | |
"fmt" | |
"os" | |
) | |
const max = 10 | |
type Document struct { | |
cp, colonia, ciudad, delegacion, lat, lon string | |
} | |
func main() { | |
d := &Document{} | |
file, err := os.Open("./MX.txt") | |
printError(err) | |
defer file.Close() | |
reader := csv.NewReader(file) | |
reader.Comma = '\t' //tab delimited | |
rows, err := reader.ReadAll() | |
printError(err) | |
for n, each := range rows { | |
d.cp = each[1] | |
d.colonia = each[2] | |
d.ciudad = each[3] | |
d.delegacion = each[5] | |
d.lat = each[9] | |
d.lon = each[10] | |
n++ | |
if n <= max { | |
fmt.Printf(" %v) | %v | %v | %v | %v | %v | %v \n", n, d.cp, d.colonia, d.ciudad, d.delegacion, d.lat, d.lon) | |
} | |
} | |
} | |
func printError(err error) { | |
if err != nil { | |
fmt.Printf("\nError: %v \n ", err.Error()) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment