英文:
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
你可以通过在DailyData
的Data
切片中提供索引来访问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
函数中,我们从DailyData
的Data
切片中的索引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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论