Home > Blockchain >  How to iterate nested struct in Go?
How to iterate nested struct in Go?

Time:01-16

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 >No family details</h5>
{{ end }}

i can not get the expected results as i i tried to iterate

CodePudding user response:

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)

  • Related