如何在Go中迭代嵌套的结构体?

huangapple go评论96阅读模式
英文:

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:&quot;familyMember&quot; json:&quot;familyMember&quot;`
	Text         string   `xml:&quot;,chardata&quot; json:&quot;,chardata&quot;`
	ResponseCode string   `xml:&quot;responseCode&quot;`
	Family     []struct {
		Text    string `xml:&quot;,chardata&quot; json:&quot;,chardata&quot;`
		Parents []struct {
			Text   string `xml:&quot;,chardata&quot; json:&quot;,chardata&quot;`
			Parent []struct {
				Text      string `xml:&quot;,chardata&quot; json:&quot;,chardata&quot;`
				FirstName string `xml:&quot;firstName&quot; json:&quot;firstName&quot;`
				MidleName string `xml:&quot;midleName&quot; json:&quot;midleName&quot;`
				SurName   string `xml:&quot;surName&quot; json:&quot;surName&quot;`
				Gender    string `xml:&quot;gender&quot; json:&quot;gender&quot;`
				DoB       string `xml:&quot;doB&quot; json:&quot;doB&quot;`
			} `xml:&quot;parent&quot; json:&quot;parent&quot;`
		} `xml:&quot;parents&quot; json:&quot;parents&quot;`

		Children []struct {
			Text  string `xml:&quot;,chardata&quot; json:&quot;,chardata&quot;`
			Child []struct {
				Text      string `xml:&quot;,chardata&quot; json:&quot;,chardata&quot;`
				FirstName string `xml:&quot;firstName&quot; json:&quot;firstName&quot;`
				MidleName string `xml:&quot;midleName&quot; json:&quot;midleName&quot;`
				SurName   string `xml:&quot;surName&quot; json:&quot;surName&quot;`
				Gender    string `xml:&quot;gender&quot; json:&quot;gender&quot;`
				DoB       string `xml:&quot;doB&quot; json:&quot;doB&quot;`
			} `xml:&quot;child&quot; json:&quot;child&quot;`
		} `xml:&quot;children&quot; json:&quot;children&quot;`

	} `xml:&quot;family&quot; json:&quot;family&quot;`
}

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 }}
    &lt;table &gt;
      &lt;thead&gt;
         &lt;tr&gt;
           &lt;th&gt;Names&lt;/th&gt;
           &lt;th&gt;Gender&lt;/th&gt;
           &lt;th&gt;Born&lt;/th&gt;
         &lt;/tr&gt;
       &lt;/thead&gt;
       &lt;tbody&gt;
          {{ range .FamilyMembers }}
          &lt;tr&gt;
             &lt;td&gt;{{ .FirstName }} {{ .MidleName }} {{ .SurName }}&lt;/td&gt;
             &lt;td&gt;{{ .Gender }}&lt;/td&gt;
             &lt;td&gt;{{ .DoB }}&lt;/td&gt;
          &lt;/tr&gt;
          {{ end }}
       &lt;/tbody&gt;
   &lt;/table&gt;
 {{ else }}
    &lt;h5 class=&quot;mt-3 text-center&quot;&gt;No family details&lt;/h5&gt;
{{ 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 }}
            &lt;tr&gt;
                &lt;td&gt;{{ .FirstName }} {{ .MidleName }} {{ .SurName }}&lt;/td&gt;
                &lt;td&gt;{{ .Gender }}&lt;/td&gt;
                &lt;td&gt;{{ .DoB }}&lt;/td&gt;
              &lt;/tr&gt;
           {{ end }}
        {{ end }}
        {{ range $j, $b := .Children }}
             {{ range $k, $c := .Child }}
                  &lt;tr&gt;
                      &lt;td&gt;{{ .FirstName }} {{ .MidleName }} {{ .SurName }}&lt;/td&gt;
                      &lt;td&gt;{{ .Gender }}&lt;/td&gt;
                      &lt;td&gt;{{ .DoB }}&lt;/td&gt;
                  &lt;/tr&gt;
            {{ end }}
      {{ end }}
    {{ end }}

By doing that help to display data in all inner structs (nested structs)

huangapple
  • 本文由 发表于 2023年1月13日 17:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75107031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定