面试题答案
一键面试package main
import (
"fmt"
)
func main() {
// 初始化嵌套Map
nestedMap := make(map[string]map[int]string)
// 调用函数添加键值对并打印
addAndPrint(nestedMap, "key1", 1, "value1")
}
func addAndPrint(nestedMap map[string]map[int]string, outerKey string, innerKey int, value string) {
// 检查外层Map中是否存在该键
if _, exists := nestedMap[outerKey];!exists {
nestedMap[outerKey] = make(map[int]string)
}
// 添加新的键值对
nestedMap[outerKey][innerKey] = value
// 打印整个嵌套Map
fmt.Println(nestedMap)
}