访问嵌套结构体中的字段

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

Accessing fields in nested structs

问题

如何从主要的“Forecast”结构中引用下面的“AllData”结构中的字段?例如,如果我想从“Forecast -> Daily”引用“TemperatureMax”字段,应该怎么做?

  1. type AllData struct {
  2. Time float64 `json:"time"`
  3. Summary string `json:"summary"`
  4. Icon string `json:"icon"`
  5. TemperatureMin float64 `json:"temperatureMin"`
  6. TemperatureMinTime float64 `json:"temperatureMinTime"`
  7. TemperatureMax float64 `json:"temperatureMax"`
  8. }
  9. type HourlyData struct {
  10. Summary string `json:"summary"`
  11. Icon string `json:"icon"`
  12. Data []CurrentData `json:"data"`
  13. }
  14. type DailyData struct {
  15. Summary string `json:"summary"`
  16. Icon string `json:"icon"`
  17. Data []AllData `json:"data"`
  18. }
  19. type Forecast struct {
  20. Latitude float64 `json:"latitude"`
  21. Longitude float64 `json:"longitude"`
  22. Timezone string `json:"timezone"`
  23. Offset int `json:"offset"`
  24. Currently CurrentData `json:"currently"`
  25. Hourly HourlyData `json:"hourly"`
  26. Daily DailyData `json:"daily"`
  27. Flags Flags `json:"flags"`
  28. }

要引用“TemperatureMax”字段,您可以使用以下代码:

  1. temperatureMax := forecast.Daily.Data[0].TemperatureMax

这将从“Forecast”结构的“Daily”字段中的第一个元素的“TemperatureMax”字段中获取值。请注意,这里假设“Data”字段是一个非空的切片,并且至少有一个元素。如果“Data”字段是空的或没有元素,您需要进行适当的错误处理。

英文:

How do I reference a field in the "AllData" struct below from the main "Forecast" struct? e.g if I wanted to reference "TemperatureMax from Forecast -> Daily?

  1. type AllData struct {
  2. Time float64 `json:"time"`
  3. Summary string `json:"summary"`
  4. Icon string `json:"icon"`
  5. TemperatureMin float64 `json:"temperatureMin"`
  6. TemperatureMinTime float64 `json:"temperatureMinTime"`
  7. TemperatureMax float64 `json:"temperatureMax"`
  8. }
  9. type HourlyData struct {
  10. Summary string `json:"summary"`
  11. Icon string `json:"icon"`
  12. Data []CurrentData `json:"data"`
  13. }
  14. type DailyData struct {
  15. Summary string `json:"summary"`
  16. Icon string `json:"icon"`
  17. Data []AllData `json:"data"`
  18. }
  19. type Forecast struct {
  20. Latitude float64 `json:"latitude"`
  21. Longitude float64 `json:"longitude"`
  22. Timezone string `json:"timezone"`
  23. Offset int `json:"offset"`
  24. Currently CurrentData `json:"currently"`
  25. Hourly HourlyData `json:"hourly"`
  26. Daily DailyData `json:"daily"`
  27. Flags Flags `json:"flags"`
  28. }

答案1

得分: 4

你可以通过在DailyDataData切片中提供索引来访问Forecast结构中的AllData字段。考虑一下你问题的简化示例:

  1. package main
  2. import "fmt"
  3. type AllData struct {
  4. Summary string
  5. }
  6. type DailyData struct {
  7. Data []AllData
  8. }
  9. type Forecast struct {
  10. Daily DailyData
  11. }
  12. func main() {
  13. a := AllData{"summary"}
  14. s := []AllData{a}
  15. d := DailyData{s}
  16. f := Forecast{d}
  17. val := f.Daily.Data[0].Summary
  18. fmt.Println(val)
  19. }

main函数中,我们从DailyDataData切片中的索引0处读取AllData结构的Summary字段。这将在控制台打印出summary

可选地,我们可以通过在DailyData的切片上进行迭代来访问多个AllData结构:

  1. func main() {
  2. a1 := AllData{"summary1"}
  3. a2 := AllData{"summary2"}
  4. a3 := AllData{"hello"}
  5. s := []AllData{a1, a2, a3}
  6. d := DailyData{s}
  7. f := Forecast{d}
  8. for _, val := range f.Daily.Data {
  9. fmt.Println(val.Summary)
  10. }
  11. }

上述代码将打印:

  1. summary1
  2. summary2
  3. hello
英文:

You can access an AllData field from a Forecast struct by providing an index into the Data slice in DailyData. Consider this stripped-down example of your question:

  1. package main
  2. import "fmt"
  3. type AllData struct {
  4. Summary string
  5. }
  6. type DailyData struct {
  7. Data []AllData
  8. }
  9. type Forecast struct {
  10. Daily DailyData
  11. }
  12. func main() {
  13. a := AllData{"summary"}
  14. s := []AllData{a}
  15. d := DailyData{s}
  16. f := Forecast{d}
  17. val := f.Daily.Data[0].Summary
  18. fmt.Println(val)
  19. }

In main, we read the Summary field from the AllData struct at index 0 of the DailyData's Data slice. This prints summary to the console.

Optionally, we could access multiple AllData structs by ranging over the slice in DailyData:

  1. func main() {
  2. a1 := AllData{"summary1"}
  3. a2 := AllData{"summary2"}
  4. a3 := AllData{"hello"}
  5. s := []AllData{a1, a2, a3}
  6. d := DailyData{s}
  7. f := Forecast{d}
  8. for _, val := range f.Daily.Data {
  9. fmt.Println(val.Summary)
  10. }
  11. }

The above prints:

  1. summary1
  2. summary2
  3. hello

huangapple
  • 本文由 发表于 2015年9月23日 05:19:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/32727135.html
匿名

发表评论

匿名网友

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

确定