英文:
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
- 为什么
names
和myMap
的顺序混合在一起了? - 为什么
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
- Why was it names and myMap mixed?
- Why different order of myMap?
答案1
得分: 9
> func println(args ...Type)
> println内置函数以特定于实现的方式格式化其参数,并将结果写入标准错误。
> func Println(a ...interface{}) (n int, err error)
> fmt.Println使用其操作数的默认格式进行格式化,并写入标准输出。
fmt.Println写入标准输出(stdout),println写入标准错误(stderr),这是两个不同的、不同步的文件。
> map是一组无序的元素,元素类型称为元素类型,由另一种类型的一组唯一键(称为键类型)索引。
> "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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论