Google Go Lang分配顺序

编程入门 行业动态 更新时间:2024-10-14 14:16:45
本文介绍了Google Go Lang分配顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在一个Go示例中,如果我输入以下内容:

package main import fmt 类型顶点结构{ Lat,Long float64 } var m map [string]顶点 func main(){m = make(map [string] Vertex)m [Bell Labs] = Vertex { 40.68433,74.39967,} m [测试] =顶点{ 12.0,100,} fmt.Println(m [贝尔实验室)) fmt.Println(m)}

吐出来:

{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分配顺序

本文发布于:2023-11-30 02:14:28,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   分配   Google   Lang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!