這是我定義的結構——從 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"`}當我在 Nodes 層迭代我的請求響應時,一切正常并且可以打印出來,下面是打印出 Edges 數組中所有 Nodes 的代碼 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) } 但是當我嘗試訪問節點結構中的單個字段時發生錯誤修改后的代碼: 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 undefined (type struct{Node struct{FirstPublished string "json:"firstPublished""; Headline struct{Default string "json:"default""} "json :"headline""; 摘要字符串 "json:"summary""; URL 字符串 "json:"url""} "json:"node""} 沒有字段或方法 FirstPublished)當我使用'doc fieldname'打印 Go 結構中的字段時出現什么問題?
Golang打印結構總是報undefined
12345678_0001
2022-12-13 16:18:24