Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Golang Print structure always report undefined

Here is the structure I defined – generated from a json to Go struct tool:

type NYTimesNews struct {
    Data struct {
        LegacyCollection struct {
            CollectionsPage struct {
                Stream struct {
                    Edges []struct {
                        Node struct {
                            FirstPublished string `json:"firstPublished"`
                            Headline       struct {
                                Default string `json:"default"`
                            } `json:"headline"`
                            Summary string `json:"summary"`
                            URL     string `json:"url"`
                        } `json:"node"`
                    } `json:"edges"`
                } `json:"stream"`
            } `json:"collectionsPage"`
        } `json:"legacyCollection"`
    } `json:"data"`
}

When I iterate response from my request at the layer of Nodes ,everything works right and can be print out, below are the code of print out all Nodes which is in Edges array

  for i, Node:= range data.Data.LegacyCollection.CollectionsPage.Stream.Edges{
      // fmt.Printf("[%d] \n %s \n", i, Node)
      fmt.Printf("[%d] \n %s \n %s\n", i, reflect.TypeOf(Node),Node)
  } 

Output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[0] 
 struct { Node struct { FirstPublished string "json:\"firstPublished\""; Headline struct { Default string "json:\"default\"" } "json:\"headline\""; Summary string "json:\"summary\""; URL string "json:\"url\"" } "json:\"node\"" } 
 {{2022-05-14T21:17:13.000Z {Turkey Offers to Evacuate Mariupol Fighters Despite Disagreements} Turkey has had a ship waiting for weeks in Istanbul to evacuate those remaining in the Azovstal steel plant, but Ukraine and Russia have not agreed to a plan, a Turkish official said. https://www.nytimes.com/2022/05/14/world/europe/azovstal-evacuation-turkey.html}}
[1] 
 struct { Node struct { FirstPublished string "json:\"firstPublished\""; Headline struct { Default string "json:\"default\"" } "json:\"headline\""; Summary string "json:\"summary\""; URL string "json:\"url\"" } "json:\"node\"" } 
 {{2022-05-14T16:26:03.000Z {Catalan Pop? Corsican Rock? It’s Europe’s Other Song Contest.} The Liet International, a competition for minority and regional languages, lacks the glitz of Eurovision. But its organizers say it helps keep endangered tongues alive. https://www.nytimes.com/2022/05/14/arts/music/minority-languages-song-contest.html}}

But when I tried to access single fields in Node structure error occured

Code after modify:

  for i, Node:= range data.Data.LegacyCollection.CollectionsPage.Stream.Edges{
    fmt.Printf("[%d] \n %s \n %s -------- \n\n", i, reflect.TypeOf(Node), Node.FirstPublished)
    break
  }

Output

./getdata-url-nytimes.go:92:80: Node.FirstPublished undefined (type struct{Node struct{FirstPublished string "json:"firstPublished""; Headline struct{Default string "json:"default""} "json:"headline""; Summary string "json:"summary""; URL string "json:"url""} "json:"node""} has no field or method FirstPublished)

Anything goes wrong when I use ‘doc fieldname’ to print a field in a Go structure ?

>Solution :

You named loop variable Node but what it really contains is a struct of type Edge. .Node is property of each Edge in the collection you iterate over. You need to access it through edge’s .Node field like this instead:

  for i, edge:= range data.Data.LegacyCollection.CollectionsPage.Stream.Edges{
    fmt.Printf("[%d] \n %s \n %s -------- \n\n", i, reflect.TypeOf(edge.Node), edge.Node.FirstPublished)
    break
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading