Google Go Lang Assignment Order

前端 未结 2 1790
死守一世寂寞
死守一世寂寞 2021-01-03 10:28

Let\'s look at the following Go code:

package main

import \"fmt\"

type Vertex struct {
    Lat, Long float64
}

var m map[string]Vertex

func main() {
             


        
2条回答
  •  太阳男子
    2021-01-03 11:03

    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.

提交回复
热议问题