亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從嵌套結構中檢索值

從嵌套結構中檢索值

Go
侃侃無極 2022-07-04 16:54:51
我正在嘗試從 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}}, }

此外,您的結構字段名稱也存在一些不一致;創建實例時也應使用定義期間使用的字段名稱,否則您將收到錯誤消息。


查看完整回答
反對 回復 2022-07-04
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號