1 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊
interface{} 類型中的值可以是任何類型的值。使用類型斷言確保值類型在訪問之前對操作有效:
package main
import "fmt"
func main() {
Problem := map[string]interface{}{
"Alan": map[string]interface{}{
"Particulars": map[string]interface{}{
"Age": "28",
},
},
"Sophia": map[string]interface{}{
"Particulars": map[string]interface{}{
"Age": "18",
},
},
}
Ages := []string{}
for key, value := range Problem {
fmt.Println(key)
fmt.Println(value)
a, ok := value.(map[string]interface{})["Particulars"].(map[string]interface{})["Age"].(string)
if ok {
Ages = append(Ages, a)
}
}
fmt.Println(Ages)
}
- 1 回答
- 0 關(guān)注
- 106 瀏覽
添加回答
舉報(bào)