GO循环切片(slice)、数据(array)、集合(map)
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2019-11-09 15:43:48
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
循环数组
//demo_18.go package main import ( "fmt" ) func main() { person := [3] string {"Tom", "Aaron", "John"} fmt.Printf("len=%d cap=%d array=%v\n", len(person), cap(person), person) fmt.Println("") //循环 for k, v := range person { fmt.Printf("person[%d]: %s\n", k, v) } fmt.Println("") for i := range person { fmt.Printf("person[%d]: %s\n", i, person[i]) } fmt.Println("") for i := 0; i < len(person); i++ { fmt.Printf("person[%d]: %s\n", i, person[i]) } fmt.Println("") //使用空白符 for _, name := range person { fmt.Println("name :", name) } }
循环切片
//demo_19.go package main import ( "fmt" ) func main() { person := [] string {"Tom", "Aaron", "John"} fmt.Printf("len=%d cap=%d slice=%v\n", len(person), cap(person), person) fmt.Println("") //循环 for k, v := range person { fmt.Printf("person[%d]: %s\n", k, v) } fmt.Println("") for i := range person { fmt.Printf("person[%d]: %s\n", i, person[i]) } fmt.Println("") for i := 0; i < len(person); i++ { fmt.Printf("person[%d]: %s\n", i, person[i]) } fmt.Println("") //使用空白符 for _, name := range person { fmt.Println("name :", name) } }
循环集合
//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\n", len(person), person) fmt.Println("") //循环 for k, v := range person { fmt.Printf("person[%d]: %s\n", k, v) } fmt.Println("") for i := range person { fmt.Printf("person[%d]: %s\n", i, person[i]) } fmt.Println("") for i := 1; i <= len(person); i++ { fmt.Printf("person[%d]: %s\n", i, person[i]) } fmt.Println("") //使用空白符 for _, name := range person { fmt.Println("name :", name) } }