2021-08-31 18:49:08 +02:00
|
|
|
//go:build ignore
|
2020-05-28 15:12:18 +02:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// This generates countries.go by running "go generate"
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Country struct {
|
|
|
|
Code string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
var countries []Country
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
file, err := os.Open("./resources/countries.txt")
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
parts := strings.Split(scanner.Text(), ":")
|
|
|
|
|
|
|
|
if len(parts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
countries = append(countries, Country{Code: strings.ToLower(parts[0]), Name: strings.ToLower(parts[1])})
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create("countries.go")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
packageTemplate.Execute(f, struct {
|
|
|
|
Countries []Country
|
|
|
|
}{
|
|
|
|
Countries: countries,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:53:55 +02:00
|
|
|
var packageTemplate = template.Must(template.New("").Parse(`
|
2020-05-28 15:12:18 +02:00
|
|
|
package txt
|
|
|
|
|
2022-04-25 09:53:55 +02:00
|
|
|
// Generated code, do not edit.
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
var Countries = map[string]string{
|
|
|
|
{{- range .Countries }}
|
|
|
|
{{ printf "%q" .Name }}: {{ printf "%q" .Code }},
|
|
|
|
{{- end }}
|
|
|
|
}`))
|