delete map[key] in go?

后端 未结 5 715
星月不相逢
星月不相逢 2021-01-30 04:46

I have a map:

var sessions =  map[string] chan int{}

How do I delete sessions[key]? I tried:

sessions[key] = nil,         


        
5条回答
  •  心在旅途
    2021-01-30 05:15

    From Effective Go:

    To delete a map entry, use the delete built-in function, whose arguments are the map and the key to be deleted. It's safe to do this even if the key is already absent from the map.

    delete(timeZone, "PDT")  // Now on Standard Time
    

提交回复
热议问题