总体
代码
//demo_18.go
package main
import (
"fmt"
)
func main() {
person := [3] string {"Tom", "Aaron", "John"}
fmt.Printf("len=%d cap=%d array=%v
",len(person),cap(person),person)
fmt.Println("")
//循环
for k, v := range person {
fmt.Printf("person[%d]: %s
", k, v)
}
fmt.Println("")
for i := range person {
fmt.Printf("person[%d]: %s
", i, person[i])
}
fmt.Println("")
for i := 0; i < len(person); i++ {
fmt.Printf("person[%d]: %s
", i, person[i])
}
fmt.Println("")
//使用空白符
for _, name := range person {
fmt.Println("name :", name)
}
}
输出
len=3 cap=3 array=[Tom Aaron John]
person[0]: Tom
person[1]: Aaron
person[2]: John
person[0]: Tom
person[1]: Aaron
person[2]: John
person[0]: Tom
person[1]: Aaron
person[2]: John
name : Tom
name : Aaron
name : John
循环map
//demo_20.go
package main
import (
"fmt"
)
func main() {
person := map[int]string{
1 : "Tom",
2 : "Aaron",
3 : "John",
}
fmt.Printf("len=%d map=%v
", len(person), person)
fmt.Println("")
//循环
for k, v := range person {
fmt.Printf("person[%d]: %s
", k, v)
}
fmt.Println("")
for i := range person {
fmt.Printf("person[%d]: %s
", i, person[i])
}
fmt.Println("")
for i := 1; i <= len(person); i++ {
fmt.Printf("person[%d]: %s
", i, person[i])
}
fmt.Println("")
//使用空白符
for _, name := range person {
fmt.Println("name :", name)
}
}
for
for i := 1; i <= 10; i++ {
if i == 6 {
break//continue
}
fmt.Println("i =", i)
}
func main() {
fmt.Println("begin")
for i := 1; i <= 10; i++ {
if i == 6 {
goto END
}
fmt.Println("i =", i)
}
END :
fmt.Println("end")
}