3

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"`
}
3
  • 2
    DailyData.Data is an array, what exactly are you trying to do? Commented Sep 22, 2015 at 21:25
  • hi - this mirrors the way the json feed is structured when you pull from forecast.io ( weather forecast). the goal is to eventually access any of the available variables in a fronted visualization. I can't print out any of the values in AllData at this point e.g when using fmt.Printf Commented Sep 22, 2015 at 21:48
  • Can you include the JSON sample and how you are trying to use it with these data structures? That will help us provide more specific guidance. Commented Sep 22, 2015 at 22:07

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Carl - this is great. the first option is closer to what I am looking to do - i.e access the variables in AllData individually ( vs. spitting it all out in a range like in the second option). However, this implies I have to setup variables like this for EACH item in AllData - and there are quite a few - I abbreviated it here for clarity.
I'm sorry then I don't really understand what you're trying to do, could you edit your question to specify what you mean beyond accessing the struct members?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.