吐出来:
{40.68433 74.39967}
map [贝尔实验室:{40.68433 74.39967} test:{12 100}] 我更改测试顶点声明的一小部分,如下所示:
m [test] =顶点{ 12.0,100,}输出如下: {40.68433 74.39967}
map [测试:{12 100}贝尔实验室:{40.68433 74.39967}]
改变??我搬进了右边的}4个空间。为什么这会影响我的地图的顺序?
感谢任何答案/想法
解决方案映射顺序取决于使用的哈希函数。散列函数被随机化以防止使用散列冲突的拒绝服务攻击。有关详情,请参阅问题跟踪器:
code.google/p/go/issues/detail?id=2630
地图订单无法保证根据规范。尽管在当前的实现中还没有完成,但未来的实现可能会在GC或其他操作期间进行一些压缩,这会改变地图的顺序,而不会影响您的代码修改地图。假设一个未在规范中定义的属性是不明智的。
映射是一个无序元素组一种叫做元素类型,由另一种类型的唯一键索引,称为键类型。
In a Go example if I put in the following:
package main import "fmt" type Vertex struct { Lat, Long float64 } var m map[string]Vertex func main() { m = make(map[string]Vertex) m["Bell Labs"] = Vertex{ 40.68433, 74.39967, } m["test"] = Vertex{ 12.0, 100, } fmt.Println(m["Bell Labs"]) fmt.Println(m) }It spits out:
{40.68433 74.39967}
map[Bell Labs:{40.68433 74.39967} test:{12 100}]
However if I Change ONE minor part of the test Vertex declaration as follows:
m["test"] = Vertex{ 12.0, 100, }it outputs the following: {40.68433 74.39967}
map[test:{12 100} Bell Labs:{40.68433 74.39967}]
The change?? I moved in the right "}" 4 spaces. Why the heck does that affect the order of my map?
Thanks for any answers / ideas
解决方案Map "order" depends on the hash function used. The hash function is randomized to prevent denial of service attacks that use hash collisions. See the issue tracker for details:
code.google/p/go/issues/detail?id=2630
Map order is not guaranteed according to the specification. Although not done in current go implementations, a future implementation could do some compacting during GC or other operation that changes the order of a map without the map being modified by your code. It is unwise to assume a property not defined in the specification.
A map is an unordered group of elements of one type, called the element type, indexed by a set of unique keys of another type, called the key type.
更多推荐
Google Go Lang分配顺序
发布评论