在使用`println()`之后,我使用`fmt.Println()`。

huangapple go评论92阅读模式
英文:

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

问题

这是我的代码(使用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)
    }
}

这是结果:

0 1
B Banana
1 2
2 3
C Charlie
A Apple
  1. 为什么namesmyMap的顺序混合在一起了?
  2. 为什么myMap的顺序不同?
英文:

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?

答案1

得分: 9

> func println

> func println(args ...Type)

> println内置函数以特定于实现的方式格式化其参数,并将结果写入标准错误。

> func Println

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

> fmt.Println使用其操作数的默认格式进行格式化,并写入标准输出。

fmt.Println写入标准输出(stdout),println写入标准错误(stderr),这是两个不同的、不同步的文件。

> Map types

> map是一组无序的元素,元素类型称为元素类型,由另一种类型的一组唯一键(称为键类型)索引。

> For statements

> "for"语句指定了一个块的重复执行。

> map的迭代顺序未指定,并且不能保证与下一次迭代相同。

map的元素是无序的。迭代顺序未指定。

英文:

> 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.

huangapple
  • 本文由 发表于 2016年3月11日 10:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/35931166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定