如何在Go模板中访问范围内的范围?
模板:
{{range .Resume.Skills}}
{{.Name}}
{{.Level}}
{{range $item, $key := .Keywords}}
{{$key}}
{{$item}}
{{end}}
{{end}}
STRUCT:
type SkillsInfo struct {
Name string
Level string
Keywords []KeywordsInfo
}
type KeywordsInfo struct {
Keyword string
}
我可以看到的结果是{}。如何访问模板中的嵌套对象?
---更新 - :
type ResumeJson struct {
Basics BasicsInfo
Work []WorkInfo
Volunteer []VolunteerInfo
Education []EducationInfo
Awards []AwardsInfo
Publications []PublicationsInfo
Skills []SkillsInfo
Languages []LaunguagesInfo
Interests []InterestsInfo
References []ReferencesInfo
}
现在看到的结果:
Web Development Master {} 0 {} 1 {} 2
ans JSON我解析:
"skills": [{
"name": "Web Development",
"level": "Master",
"keywords": [
"CSS",
"HTML",
"Javascript"
]
}],
答案 0 :(得分:2)
关键字表示为JSON中的字符串数组。更改Go类型以匹配JSON:
type SkillsInfo struct {
Name string
Level string
Keywords []string
}
并使用此模板:
{{range .Resume.Skills}}
{{.Name}}
{{.Level}}
{{range .Keywords}}
{{.}}
{{end}}
{{end}}