-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen.go
70 lines (57 loc) · 1.15 KB
/
gen.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
61
62
63
64
65
66
67
68
69
70
// +build ignore
// This program generates indonesian_pos.go
// It can be invoked by running go generate
package main
import (
"encoding/json"
"io/ioutil"
"os"
"text/template"
)
const FileTemplate = `// This file is generated by go generate, please do not change manually.
package tek
var {{ .VariableName }} = []*Vocab{
{{ range .Data }}{
Id: {{ .Id }},
Word: "{{ .Word }}",
Type: "{{ .Type }}",
},{{ end }}
}
`
type TemplateFiller struct {
VariableName string
Data []*Vocab
}
type Vocab struct {
Id int `json:"id"`
Word string `json:"word"`
Type string `json:"type"`
}
func main() {
pos := []*Vocab{}
fb, err := ioutil.ReadFile("./pos_id.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(fb, &pos)
if err != nil {
panic(err)
}
tmpl, err := template.New("indonesian_pos.go").Parse(FileTemplate)
if err != nil {
panic(err)
}
targetFile, err := os.Create("./indonesian_pos.go")
if err != nil {
panic(err)
}
defer targetFile.Close()
templateFiller := &TemplateFiller{
VariableName: "indonesianPos",
Data: pos,
}
err = tmpl.Execute(targetFile, templateFiller)
if err != nil {
panic(err)
}
}