Golang打印结构总是报undefined
Golang Print structure always report undefined
这是我定义的结构 - 从 json 到 Go struct 工具生成:
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"`
}
当我在节点层迭代我的请求响应时,一切正常并且可以打印出来,下面是打印出边缘数组中所有节点的代码
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)
}
输出
[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}}
但是当我尝试访问节点结构中的单个字段时发生错误
修改后的代码:
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
}
输出
./getdata-url-nytimes.go:92:80: Node.FirstPublished 未定义(类型 struct{Node struct{FirstPublished string "json:"firstPublished" "; Headline struct{默认字符串 "json:"default""} "json:"headline""; 摘要字符串 "json:"summary""; URL string "json:"url""} "json:"node""} 没有字段或方法 FirstPublished)
当我使用 'doc fieldname' 打印 Go 结构中的字段时出现什么问题?
您将循环变量命名为 Node
,但它真正包含的是类型 Edge
的结构。 .Node
是您迭代的集合中每个 Edge
的 属性。您需要像这样通过边缘的 .Node
字段访问它:
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
}
这是我定义的结构 - 从 json 到 Go struct 工具生成:
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"`
}
当我在节点层迭代我的请求响应时,一切正常并且可以打印出来,下面是打印出边缘数组中所有节点的代码
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)
}
输出
[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}}
但是当我尝试访问节点结构中的单个字段时发生错误
修改后的代码:
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
}
输出
./getdata-url-nytimes.go:92:80: Node.FirstPublished 未定义(类型 struct{Node struct{FirstPublished string "json:"firstPublished" "; Headline struct{默认字符串 "json:"default""} "json:"headline""; 摘要字符串 "json:"summary""; URL string "json:"url""} "json:"node""} 没有字段或方法 FirstPublished)
当我使用 'doc fieldname' 打印 Go 结构中的字段时出现什么问题?
您将循环变量命名为 Node
,但它真正包含的是类型 Edge
的结构。 .Node
是您迭代的集合中每个 Edge
的 属性。您需要像这样通过边缘的 .Node
字段访问它:
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
}