以下代碼工作正常var requestMap map[string]interface{}for _, value := range requestMap { switch v := value.(type) { case []interface{}: if len(v) == 0 { // if is empty then no need to throw NA return http.StatusOK, nil } case string: if len(v) == 0 { // if is empty then no need to throw NA return http.StatusOK, nil } }}但是下面的代碼給出了invalid argument for len function,我已經閱讀了這個問題var requestMap map[string]interface{}for _, value := range requestMap { switch v := value.(type) { case []interface{}, string: if len(v) == 0 { // if is empty then no need to throw NA return http.StatusOK, nil } }}這個 case 語句不足以識別[]interface{}或string作為值類型嗎?為什么它仍在考慮interface{}作為參數len()
2 回答

holdtom
TA貢獻1805條經驗 獲得超10個贊
這是因為在類型切換的情況下,v
應同時轉換為接口 ( )[]interface
和 astring
的一部分。編譯器無法決定使用哪個,因此它會將值還原為,因為它可以是任何值。interface{}
- 2 回答
- 0 關注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消