访问嵌套结构体中的字段

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

Accessing fields in nested structs

问题

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

type AllData struct {
    Time               float64 `json:"time"`
    Summary            string  `json:"summary"`
    Icon               string  `json:"icon"`
    TemperatureMin     float64 `json:"temperatureMin"`
    TemperatureMinTime float64 `json:"temperatureMinTime"`
    TemperatureMax     float64 `json:"temperatureMax"`
}

type HourlyData struct {
    Summary string        `json:"summary"`
    Icon    string        `json:"icon"`
    Data    []CurrentData `json:"data"`
}

type DailyData struct {
    Summary string    `json:"summary"`
    Icon    string    `json:"icon"`
    Data    []AllData `json:"data"`
}

type Forecast struct {
    Latitude  float64     `json:"latitude"`
    Longitude float64     `json:"longitude"`
    Timezone  string      `json:"timezone"`
    Offset    int         `json:"offset"`
    Currently CurrentData `json:"currently"`
    Hourly    HourlyData  `json:"hourly"`
    Daily     DailyData   `json:"daily"`
    Flags     Flags       `json:"flags"`
}

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

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?

type AllData struct {
	Time                       float64 `json:"time"`
	Summary                    string  `json:"summary"`
	Icon                       string  `json:"icon"`
	TemperatureMin             float64 `json:"temperatureMin"`
	TemperatureMinTime         float64 `json:"temperatureMinTime"`
	TemperatureMax             float64 `json:"temperatureMax"`
}

type HourlyData struct {
	Summary string        `json:"summary"`
	Icon    string        `json:"icon"`
	Data    []CurrentData `json:"data"`
}

type DailyData struct {
	Summary string    `json:"summary"`
	Icon    string    `json:"icon"`
	Data    []AllData `json:"data"`
}

type Forecast struct {
	Latitude  float64     `json:"latitude"`
	Longitude float64     `json:"longitude"`
	Timezone  string      `json:"timezone"`
	Offset    int         `json:"offset"`
	Currently CurrentData `json:"currently"`
	Hourly    HourlyData  `json:"hourly"`
	Daily     DailyData   `json:"daily"`
	Flags     Flags       `json:"flags"`
}

答案1

得分: 4

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

package main

import "fmt"

type AllData struct {
	Summary string
}

type DailyData struct {
	Data []AllData
}

type Forecast struct {
	Daily DailyData
}

func main() {
	a := AllData{"summary"}
	s := []AllData{a}
	d := DailyData{s}
	f := Forecast{d}

	val := f.Daily.Data[0].Summary

	fmt.Println(val)
}

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

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

func main() {
	a1 := AllData{"summary1"}
	a2 := AllData{"summary2"}
	a3 := AllData{"hello"}
	s := []AllData{a1, a2, a3}
	d := DailyData{s}
	f := Forecast{d}

	for _, val := range f.Daily.Data {
		fmt.Println(val.Summary)
	}
}

上述代码将打印:

summary1
summary2
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:

package main

import "fmt"

type AllData struct {
	Summary string
}

type DailyData struct {
	Data []AllData
}

type Forecast struct {
	Daily DailyData
}

func main() {
	a := AllData{"summary"}
	s := []AllData{a}
	d := DailyData{s}
	f := Forecast{d}

	val := f.Daily.Data[0].Summary

	fmt.Println(val)
}

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:

func main() {
	a1 := AllData{"summary1"}
	a2 := AllData{"summary2"}
	a3 := AllData{"hello"}
	s := []AllData{a1, a2, a3}
	d := DailyData{s}
	f := Forecast{d}

	for _, val := range f.Daily.Data {
		fmt.Println(val.Summary)
	}
}

The above prints:

summary1
summary2
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:

确定