Google Go Lang分配顺序 [英] Google Go Lang Assignment Order

查看:84
本文介绍了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个空间。为什么这会影响我的地图的顺序?



感谢任何答案/想法

解决方案

映射顺序取决于使用的哈希函数。散列函数被随机化以防止使用散列冲突的拒绝服务攻击。有关详情,请参阅问题跟踪器:



http://code.google.com/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:

http://code.google.com/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分配顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆