Why are maps built in?
为什么要内置地图?
The same reason strings are: they are such a powerful and important data structure that providing one excellent implementation with syntactic support makes programming more pleasant. We believe that Go's implementation of maps is strong enough that it will serve for the vast majority of uses. If a specific application can benefit from a custom implementation, it's possible to write one but it will not be as convenient syntactically; this seems a reasonable tradeoff.
与string的原因相同:它们是一种非常强大且重要的数据结构,提供具有语法支持的出色实现使编程变得更加愉快。我们相信 Go 的map实现足够强大,足以满足绝大多数用途。如果特定的应用程序可以从自定义实现中受益,则可以编写一个自定义实现,但在语法上不会那么方便;这似乎是一个合理的权衡。
Why don't maps allow slices as keys?
为什么映射不允许切片作为键?
Map lookup requires an equality operator, which slices do not implement. They don't implement equality because equality is not well defined on such types; there are multiple considerations involving shallow vs. deep comparison, pointer vs. value comparison, how to deal with recursive types, and so on. We may revisit this issue—and implementing equality for slices will not invalidate any existing programs—but without a clear idea of what equality of slices should mean, it was simpler to leave it out for now.
map查找需要一个相等运算符,而切片不实现该运算符。它们没有实现相等性,因为此类类型没有很好地定义相等性;有多种考虑因素,涉及浅比较与深比较、指针比较与值比较、如何处理递归类型等等。我们可能会重新讨论这个问题——实现切片相等不会使任何现有程序无效——但如果不清楚切片相等的含义,那么暂时忽略它会更简单。
In Go 1, unlike prior releases, equality is defined for structs and arrays, so such types can be used as map keys. Slices still do not have a definition of equality, though.
在 Go 1 中,与之前的版本不同,为结构体和数组定义了相等性,因此此类类型可以用作映射键。不过,切片仍然没有平等的定义。
Why are maps, slices, and channels references while arrays are values?
为什么映射、切片和通道是引用,而数组是值?
There's a lot of history on that topic. Early on, maps and channels were syntactically pointers and it was impossible to declare or use a non-pointer instance. Also, we struggled with how arrays should work. Eventually we decided that the strict separation of pointers and values made the language harder to use. Changing these types to act as references to the associated, shared data structures resolved these issues. This change added some regrettable complexity to the language but had a large effect on usability: Go became a more productive, comfortable language when it was introduced.
关于这个话题有很多历史。早期,Map和channel在语法上是指针,不可能声明或使用非指针实例。此外,我们还纠结于数组应该如何工作。最终我们认为指针和值的严格分离使得该语言更难使用。更改这些类型以充当对关联的共享数据结构的引用解决了这些问题。这一变化给语言增加了一些令人遗憾的复杂性,但对可用性产生了很大影响:Go 被引入后成为一种更高效、更舒适的语言。






网友评论