2022-10-31:以下go语言代码输出什么?A:map[];B:nil;C:Panic;D:编译错误。package mai

2022-11-06 10:30:37 浏览数 (1)

2022-10-31:以下go语言代码输出什么?A:map[];B:nil;C:Panic;D:编译错误。

代码语言:javascript复制
package main

import "fmt"

func main() {
    var m map[string]int
    delete(m, "oh noes!")
    fmt.Println(m)
}

答案选A。在 delete 函数的文档有说明:The delete built-in function deletes the element with the specified key (m[key]) from the map. If m is nil or there is no such element, delete is a no-op.这意思是没有元素的时候,啥都不做。

0 人点赞