我如何获得地图内部和其他地图内部的值?
How i can get a value that is inside a map, inside other map?
我是 golang 的新手,我遇到了这个问题。
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)
Ages = value["Age"]
}
}
我想用“年龄”的勇气做点什么,我该怎么做?
interface{} 类型中的值可以是任何类型的值。在访问之前使用 type assertion 确保值类型对操作有效:
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)
}
我是 golang 的新手,我遇到了这个问题。
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)
Ages = value["Age"]
}
}
我想用“年龄”的勇气做点什么,我该怎么做?
interface{} 类型中的值可以是任何类型的值。在访问之前使用 type assertion 确保值类型对操作有效:
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)
}