【GoLang】GoLang 遍历 map、slice、array方法
代码示例:
map1 := make(map[string]string) map1["a"] = "AAA" map1["b"] = "BBB" map1["c"] = "CCC" for k, v := range map1 { t.Log(k, v) } for _, v := range map1 { t.Log(v) } array := [...]int64{1, 2, 3, 4} for k, v := range array { t.Log(k, v) } for _, v := range array { t.Log(v) } slice := array[:3:3] for k, v := range slice { t.Log(k, v) } t.Log(cap(slice)) slice[0] = 444 slice = append(slice, 888) slice = append(slice, 999) t.Log(slice) t.Log(array)
结果输出:
app_for_test.go:50: a AAA app_for_test.go:50: b BBB app_for_test.go:50: c CCC app_for_test.go:53: AAA app_for_test.go:53: BBB app_for_test.go:53: CCC app_for_test.go:58: 0 1 app_for_test.go:58: 1 2 app_for_test.go:58: 2 3 app_for_test.go:58: 3 4 app_for_test.go:61: 1 app_for_test.go:61: 2 app_for_test.go:61: 3 app_for_test.go:61: 4 app_for_test.go:66: 0 1 app_for_test.go:66: 1 2 app_for_test.go:66: 2 3 app_for_test.go:68: 3 app_for_test.go:72: [444 2 3 888 999] app_for_test.go:73: [444 2 3 4]
参考资料:
https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/02.3.md#goto