我正在嘗試從 golang 中的嵌套結構訪問值。我的最終結構如下StudentDetails = [{"rollno":3,"name":"John","score":{"sub1":50,"sub2":48,"sub3":45}} , {"rollno":4,"name":"James","score":{"sub1":38,"sub2":35,"sub3":40}}]我想單獨打印這些值。為此,我嘗試添加一個循環for i, details := range StudentDetails { glog.Info("increment ", i) glog.Info("name of student ", details.name) glog.Info("mark of sub 1", details.score.sub1)}但我總是得到錯誤"(type map[string]interface {} has no field or method name"" 我的結構如下type scoreCard struct { subject1 int `json:"id"` subject2 int `json:"id"` subject3 int `json:"id"`}type StudDetails struct { roll int name string score scoreCard}
1 回答

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
您創建的“StudentDetails”變量屬于 map 類型。我想你打算做的是這個
var StudentDetails = []StudDetails{ {"roll":3,"name":"John","score":{"subject1":50,"subject2":48,"subject3":45}}, {"roll":4,"name":"James","score":{"subject1":38,"subject2":35,"subject3":40}}, }
此外,您的結構字段名稱也存在一些不一致;創建實例時也應使用定義期間使用的字段名稱,否則您將收到錯誤消息。
- 1 回答
- 0 關注
- 112 瀏覽
添加回答
舉報
0/150
提交
取消