Does Go Map Iteration Order Depend on Key Insertion Order?
Assignment Order in Go Maps
Considering the following Go code:
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 outputs:
{40.68433 74.39967} map[Bell Labs:{40.68433 74.39967} test:{12 100}]
Modifying the test vertex declaration by moving the right "}" 4 spaces, as follows:
m["test"] = Vertex{ 12.0, 100, }
Changes the output to:
{40.68433 74.39967} map[test:{12 100} Bell Labs:{40.68433 74.39967}]
Explanation:
Map "order" in Go depends on the randomized hash function used to prevent denial of service attacks. As per the Go issue tracker (http://code.google.com/p/go/issues/detail?id=2630), map order is not guaranteed according to the specification.
According to the specification, a map is an unordered group of elements with unique keys. A future implementation could change the order of a map without modifying it in your code. Therefore, relying on specific map order is not a recommended practice in Go.
The above is the detailed content of Does Go Map Iteration Order Depend on Key Insertion Order?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework?

How to convert MySQL query result List into a custom structure slice in Go language?

How can I define custom type constraints for generics in Go?

How do I write mock objects and stubs for testing in Go?

How to write files in Go language conveniently?

How can I use tracing tools to understand the execution flow of my Go applications?
