Home  >  Article  >  Backend Development  >  Is golang map in order?

Is golang map in order?

(*-*)浩
(*-*)浩Original
2019-12-17 09:45:165295browse

Is golang map in order?

#Map is a built-in type in Go that binds keys and values ​​together. The corresponding value can be obtained by key. (Recommended Learning: Go )

## This is as follows.

is achieved by the structure of the Golang Map in the structure of HASH, so the order is chaotic.

fruits := map[string] int {
    "oranges": 100,
    "apples": 200,
    "banans": 300,
}

If you want it to be in order, you can transfer the key to the slice, sort the slice, and then output:

// Put the keys in a slice and sort it.
var keys []string
for key := range fruits {
	keys = append(keys, key)
}
sort.Strings(keys)

// Display keys according to the sorted slice.
for _, key := range keys {
	fmt.Printf("%s:%v\n", key, fruits[key])
}

The above is the detailed content of Is golang map in order?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn