-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
61 lines (44 loc) · 1.07 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package country
import (
"io/ioutil"
"os"
"strings"
"log"
)
const (
countryFileName = "countries.dat"
newlineSeparator = "\n"
commaSeparator = ","
)
func Init() {
f, err := os.Open(countryFileName)
defer f.Close()
if err != nil {
log.Panicf("Country.init() Error! -> Unable to open countries.dat : %v", err)
}
parseDataFile(f.Name())
}
func addCountry(n string, iso string, code int, codeStr string) (country Country) {
return Country{Name: n, CountryISO:iso, CountryCode:code, CountryCodeSTR:codeStr}
}
func parseDataFile(filename string) (err error){
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Panicf("Country.parseDataFile() Error! -> %v", err)
}
populateRows(b)
return
}
func populateRows(b []byte) {
data := strings.Split(string(b), newlineSeparator)
for _, row := range data {
populateFields(row)
}
}
func populateFields(row string){
field := strings.Split(row, commaSeparator)
countries[strings.ToLower(field[0])] = addCountry(field[0],
field[1],
ConvertStringToInt(field[2]),
field[2])
}