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

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

Go中結構數組的索引超出范圍

Go中結構數組的索引超出范圍

Go
慕容708150 2021-07-30 22:13:26
我是 Go 的新手,所以希望我能弄清楚我遇到的這個問題。我的問題是我試圖迭代一個結構數組,但我一直遇到index out of range問題。出于這個問題的目的,我已經驗證我的數組不是空的,但它實際上確實包含至少一個Services結構,并且file_content是包含我的有效字符串的字符串JSON這是代表我遇到的問題的代碼片段:type service_config struct {    Services []struct {        Name    string        Command string        Request map[string]interface{}    }}var ServiceConf = service_config{}err_json := json.Unmarshal(file_content, &ServiceConf)for _, s := range ServiceConf.Services {  log.Println(s)}所以每次我運行我的代碼時,我都會得到:2014/03/14 18:19:53 http: panic serving [::1]:65448: runtime error: index out of range{  "services" : [    {      "name": "translation",      "command": "to german",      "request": {        "key": "XXX",        "url": "https://www.googleapis.com/language/translate/v2?"      }    }  ]}
查看完整描述

1 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

錯誤來自這一行

fmt.Fprintf(w, "{ \"text\": \"Translated to German you said: '%s'\" }",
    f.Data.Translations[0].TranslatedText)

所以你沒有得到任何Translations回報 - 該數組是空的。

您可能想要檢查resp.Status是否返回了錯誤。這不會作為錯誤返回 - 您需要自己檢查。


查看完整回答
反對 回復 2021-08-02
  • 1 回答
  • 0 關注
  • 260 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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