英文:
Delete key in map
问题
我有一个地图:
var sessions = map[string] chan int{}
如何删除 sessions[key]
?我尝试过:
sessions[key] = nil,false;
但是没有起作用。
更新(2011年11月):
在Go版本1中,删除地图条目的特殊语法被移除:
> Go 1将删除特殊的地图赋值,并引入一个新的内置函数delete
:delete(m, x)
将删除由表达式m[x]
检索到的地图条目。...
英文:
I have a map:
var sessions = map[string] chan int{}
How do I delete sessions[key]
? I tried:
sessions[key] = nil,false;
That didn't work.
Update (November 2011):
The special syntax for deleting map entries is removed in Go version 1:
> Go 1 will remove the special map assignment and introduce a new built-in function, delete
: delete(m, x)
will delete the map entry retrieved by the expression m[x]
. ...
答案1
得分: 329
Go引入了一个delete(map, key)
函数:
package main
func main () {
var sessions = map[string] chan int{};
delete(sessions, "moo");
}
英文:
Go introduced a delete(map, key)
function:
package main
func main () {
var sessions = map[string] chan int{};
delete(sessions, "moo");
}
答案2
得分: 116
从Go 1的发布说明中复制而来
在旧语言中,要从由m
表示的映射中删除键为k
的条目,可以使用以下语句:
m[k] = value, false
这种语法是一种特殊情况,是唯一的两对一赋值。它需要传递一个值(通常被忽略),该值会被计算但被丢弃,以及一个几乎总是常量false的布尔值。它可以完成工作,但是很奇怪并且引起争议。
在Go 1中,这种语法已经被删除了;取而代之的是一个新的内置函数delete
。调用
delete(m, k)
将删除由表达式m[k]
检索到的映射条目。没有返回值。删除不存在的条目不会产生任何效果。
更新: 运行go fix
将会将形式为m[k] = value, false
的表达式转换为delete(m, k)
,当清楚地知道被忽略的值可以安全地从程序中丢弃,并且false
指的是预定义的布尔常量时。修复工具将会标记其他使用该语法的地方,以供程序员检查。
英文:
Copied from Go 1 release notes
In the old language, to delete the entry with key k
from the map represented by m
, one wrote the statement,
m[k] = value, false
This syntax was a peculiar special case, the only two-to-one assignment. It required passing a value (usually ignored) that is evaluated but discarded, plus a boolean that was nearly always the constant false. It did the job but was odd and a point of contention.
In Go 1, that syntax has gone; instead there is a new built-in function, delete
. The call
delete(m, k)
will delete the map entry retrieved by the expression m[k]
. There is no return value. Deleting a non-existent entry is a no-op.
Updating: Running go fix
will convert expressions of the form m[k] = value, false
into delete(m, k)
when it is clear that the ignored value can be safely discarded from the program and false
refers to the predefined boolean constant. The fix tool will flag other uses of the syntax for inspection by the programmer.
答案3
得分: 102
从Effective Go中:
> 要删除一个映射条目,请使用delete内置函数,其参数是映射和要删除的键。即使键已经不存在于映射中,这样做也是安全的。
>
> delete(timeZone, "PDT") // 现在是标准时间
英文:
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
答案4
得分: 45
这些天,没有什么会崩溃。
英文:
delete(sessions, "anykey")
These days, nothing will crash.
答案5
得分: 1
使用make (chan int)
而不是nil
。第一个值必须与您的映射所持有的类型相同。
package main
import "fmt"
func main() {
var sessions = map[string] chan int{}
sessions["somekey"] = make(chan int)
fmt.Printf ("%d\n", len(sessions)) // 1
// 从sessions中删除somekey的值
delete(sessions, "somekey")
fmt.Printf ("%d\n", len(sessions)) // 0
}
**更新:**更正了我的答案。
英文:
Use make (chan int)
instead of nil
. The first value has to be the same type that your map holds.
package main
import "fmt"
func main() {
var sessions = map[string] chan int{}
sessions["somekey"] = make(chan int)
fmt.Printf ("%d\n", len(sessions)) // 1
// Remove somekey's value from sessions
delete(sessions, "somekey")
fmt.Printf ("%d\n", len(sessions)) // 0
}
UPDATE: Corrected my answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论