Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang i use fmt.Println() after println() but

Tags:

go

this is my code(golang)

func main() {
    names := []string{"1", "2", "3"}

    for index, name := range names {
        println(index, name)
    }

    myMap := map[string]string{
        "A": "Apple",
        "B": "Banana",
        "C": "Charlie",
    }

    for key, val := range myMap {
        fmt.Println(key, val)
    }
}

and this is result

0 1
B Banana
1 2
2 3
C Charlie
A Apple
  1. Why was it names and myMap mixed?
  2. Why different order of myMap?
like image 885
Jihun Lee Avatar asked Mar 11 '16 02:03

Jihun Lee


1 Answers

func println

func println(args ...Type)

The println built-in function formats its arguments in an implementation-specific way and writes the result to standard error.

func Println

func Println(a ...interface{}) (n int, err error)

fmt.Println formats using the default formats for its operands and writes to standard output.

fmt.Println writes to standard output (stdout) and println writes to standard error (stderr), two different, unsynchronized files.

Map types

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.

For statements

A "for" statement specifies repeated execution of a block.

The iteration order over maps is not specified and is not guaranteed to be the same from one iteration to the next.

Maps elements are unordered. The iteration order is not specified.

like image 115
peterSO Avatar answered Oct 08 '22 03:10

peterSO