我是 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
是否返回了錯誤。這不會作為錯誤返回 - 您需要自己檢查。
- 1 回答
- 0 關注
- 260 瀏覽
添加回答
舉報
0/150
提交
取消