英文:
How to iterate nested struct in Go?
问题
我收到一个错误消息,内容为“无法迭代范围...在花括号中的数据列表”。
我有以下结构体:
type FamilyMembers struct {
XMLName xml.Name `xml:"familyMember" json:"familyMember"`
Text string `xml:",chardata" json:",chardata"`
ResponseCode string `xml:"responseCode"`
Family []struct {
Text string `xml:",chardata" json:",chardata"`
Parents []struct {
Text string `xml:",chardata" json:",chardata"`
Parent []struct {
Text string `xml:",chardata" json:",chardata"`
FirstName string `xml:"firstName" json:"firstName"`
MidleName string `xml:"midleName" json:"midleName"`
SurName string `xml:"surName" json:"surName"`
Gender string `xml:"gender" json:"gender"`
DoB string `xml:"doB" json:"doB"`
} `xml:"parent" json:"parent"`
} `xml:"parents" json:"parents"`
Children []struct {
Text string `xml:",chardata" json:",chardata"`
Child []struct {
Text string `xml:",chardata" json:",chardata"`
FirstName string `xml:"firstName" json:"firstName"`
MidleName string `xml:"midleName" json:"midleName"`
SurName string `xml:"surName" json:"surName"`
Gender string `xml:"gender" json:"gender"`
DoB string `xml:"doB" json:"doB"`
} `xml:"child" json:"child"`
} `xml:"children" json:"children"`
} `xml:"family" json:"family"`
}
我想将所有数据列在一个表格中,但我总是收到错误消息,指示在执行“body"时无法迭代范围......然后在控制台窗口显示所有数据和错误,如下所示:
{{ if .FamilyMembers }}
<table>
<thead>
<tr>
<th>Names</th>
<th>Gender</th>
<th>Born</th>
</tr>
</thead>
<tbody>
{{ range .FamilyMembers }}
<tr>
<td>{{ .FirstName }} {{ .MidleName }} {{ .SurName }}</td>
<td>{{ .Gender }}</td>
<td>{{ .DoB }}</td>
</tr>
{{ end }}
</tbody>
</table>
{{ else }}
<h5 class="mt-3 text-center">No family details</h5>
{{ end }}
我无法获得预期的结果,因为我尝试迭代时遇到问题。
英文:
I get erro message that "Range Cann't iterate over....list of data in Curl brackets"
I have the following struct
type FamilyMembers struct {
XMLName xml.Name `xml:"familyMember" json:"familyMember"`
Text string `xml:",chardata" json:",chardata"`
ResponseCode string `xml:"responseCode"`
Family []struct {
Text string `xml:",chardata" json:",chardata"`
Parents []struct {
Text string `xml:",chardata" json:",chardata"`
Parent []struct {
Text string `xml:",chardata" json:",chardata"`
FirstName string `xml:"firstName" json:"firstName"`
MidleName string `xml:"midleName" json:"midleName"`
SurName string `xml:"surName" json:"surName"`
Gender string `xml:"gender" json:"gender"`
DoB string `xml:"doB" json:"doB"`
} `xml:"parent" json:"parent"`
} `xml:"parents" json:"parents"`
Children []struct {
Text string `xml:",chardata" json:",chardata"`
Child []struct {
Text string `xml:",chardata" json:",chardata"`
FirstName string `xml:"firstName" json:"firstName"`
MidleName string `xml:"midleName" json:"midleName"`
SurName string `xml:"surName" json:"surName"`
Gender string `xml:"gender" json:"gender"`
DoB string `xml:"doB" json:"doB"`
} `xml:"child" json:"child"`
} `xml:"children" json:"children"`
} `xml:"family" json:"family"`
}
I want to list all data in one table but i always get error message that excuting "body" ay <.FamilyMembers >: range cann't iterat over......then it dislay all data in console window with that error as following
{{ if .FamilyMembers }}
<table >
<thead>
<tr>
<th>Names</th>
<th>Gender</th>
<th>Born</th>
</tr>
</thead>
<tbody>
{{ range .FamilyMembers }}
<tr>
<td>{{ .FirstName }} {{ .MidleName }} {{ .SurName }}</td>
<td>{{ .Gender }}</td>
<td>{{ .DoB }}</td>
</tr>
{{ end }}
</tbody>
</table>
{{ else }}
<h5 class="mt-3 text-center">No family details</h5>
{{ end }}
i can not get the expected results as i i tried to iterate
答案1
得分: 2
最后,通过在迭代中执行以下操作,我得到了我想要显示的内容:
{{ range $i, $a := .FamilyMembers.Family}}
{{ range $j, $b := .Parents }}
{{ range $k, $c := .Parent }}
<tr>
<td>{{ .FirstName }} {{ .MidleName }} {{ .SurName }}</td>
<td>{{ .Gender }}</td>
<td>{{ .DoB }}</td>
</tr>
{{ end }}
{{ end }}
{{ range $j, $b := .Children }}
{{ range $k, $c := .Child }}
<tr>
<td>{{ .FirstName }} {{ .MidleName }} {{ .SurName }}</td>
<td>{{ .Gender }}</td>
<td>{{ .DoB }}</td>
</tr>
{{ end }}
{{ end }}
{{ end }}
通过这样做,可以帮助显示所有内部结构(嵌套结构)中的数据。
英文:
Finally, I got what i wanted to display by doing the following on iteration,
{{ range $i, $a := .FamilyMembers.Family}}
{{ range $j, $b := .Parents }}
{{ range $k, $c := .Parent }}
<tr>
<td>{{ .FirstName }} {{ .MidleName }} {{ .SurName }}</td>
<td>{{ .Gender }}</td>
<td>{{ .DoB }}</td>
</tr>
{{ end }}
{{ end }}
{{ range $j, $b := .Children }}
{{ range $k, $c := .Child }}
<tr>
<td>{{ .FirstName }} {{ .MidleName }} {{ .SurName }}</td>
<td>{{ .Gender }}</td>
<td>{{ .DoB }}</td>
</tr>
{{ end }}
{{ end }}
{{ end }}
By doing that help to display data in all inner structs (nested structs)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论