Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang map prints out of order

Tags:

go

map

Why is the map printing out of order, and how do I get it in to order?

package main

import (
    "fmt"
)

type monthsType struct {
    no   int
    text string
}

var months = map[int]string{
    1:"January", 2:"Fabruary", 3:"March", 4:"April", 5:"May", 6:"June",
    7:"July", 8:"August", 9:"September", 10:"October", 11:"Novenber", 12:"December",
}

func main(){
    for no, month := range months {
        fmt.Print(no)
        fmt.Println("-" + month)
    }
}

Prints out:

10-October
7-July
1-January
9-September
4-April
5-May
2-Fabruary
12-December
11-Novenber
6-June
8-August
3-March
like image 362
RoboTamer Avatar asked Aug 24 '12 11:08

RoboTamer


People also ask

How to sort a map by values in Golang?

To sort a map by values, we need to first create a list of keys in that map (slices in Golang). By default Golang prints the map with sorted keys but while iterating over a map, it follows the order of the keys appearing as it is. So, to sort the keys in a map in Golang, we can create a slice of the keys and sort it and in turn sort the slice.

How to print a map variable in Golang?

How to print map in golang? Today, we will create a map variable, add some values, and will try to print it in the console. Here, we are going to use the fmt.Println () method to print map variable values. To create a map type variable we write like m := make (map [string]int). Let’s write code and check the golang console.

How to declare a map in go?

Now we will see how to declare a map in Go. package main import ( "fmt" ) func main() { var names map[int]string // name map has int keys and string values } In the above example, the key is of type int while the values are of string type. Initializing a Map Let’s see how we can initialize a map with values. 1. Using make() function

What is map in Go language?

Maps in GoLang - GoLang Docs Maps are one of the most useful data structures. It can store in key-value pairs and doesn't allow for duplicate keys. Now, we will learn how the Go Maps are one of the most useful data structures. It can store in key-value pairs and doesn't allow for duplicate keys.


1 Answers

Code:

func DemoSortMap() (int, error) {
    fmt.Println("use an array to access items by number:")
    am := [2]string{"jan", "feb"}
    for i, n := range am {
        fmt.Printf("%2d: %s\n", i, n)
    }
    fmt.Println("maps are non-sorted:")
    mm := map[int]string{2: "feb", 1: "jan"}
    for i, n := range mm {
        fmt.Printf("%2d: %s\n", i, n)
    }
    fmt.Println("access items via sorted list of keys::")
    si := make([]int, 0, len(mm))
    for i := range mm {
        si = append(si, i)
    }
    sort.Ints(si)
    for _, i := range si {
        fmt.Printf("%2d: %s\n", i, mm[i])
    }

    return 0, nil
}

(most of it stolen from M. Summerfield's book)

output:

use an array to access items by number:
 0: jan
 1: feb
maps are non-sorted:
 2: feb
 1: jan
access items via sorted list of keys::
 1: jan
 2: feb
like image 100
Ekkehard.Horner Avatar answered Oct 24 '22 07:10

Ekkehard.Horner