
本文共 3643 字,大约阅读时间需要 12 分钟。
Golang������������������������������
1. for ������
���������for������
���Golang������for������������������������������������������������������������������������������������������������������������������������������������������������������������for���������������
Loop: for i := 0; i < 10; i++ { // ������������ for j := 0; j < 10; j++ { if i == 5 { continue Loop // ��������������������������� } fmt.Printf("i: %d; j: %d\n", i, j) } }
������������������������������������for������������������continue Loop
������������������������������������������������������������i
���0���9������i
������5������������������������������������������������������������������������������������������
switch������
switch���������Golang������������������������������������������������������������������case������������������������������
i := 10switch { case i == 4: fmt.Println("4444") case i == 5: fmt.Println("5555") default: fmt.Println("default:", i)}
������������������������i
���������������������������������������i
������4���5���������������������������������������������������default
���������������i
���������������������������������Golang���������������default
���������������fallthrough
���������������
���������������fallthrough
������������case������������
switch 9 { case 9: fmt.Println("999") fallthrough // ���������case���������������������������������case case 10: fmt.Println("ok") fallthrough // ��������������������������������������� default: fmt.Println("default")}
������������������������������fallthrough
���������������������������������������case
���
2. ���������������
Golang���������������������������������������������������������������������������������������������
���������������������
str := "abcdefg"for i, k := range str { fmt.Printf("%v-%v\n", i, string(k))}// ���������������// 0-a// 1-b// 2-c// 3-d// 4-e// 5-f// 6-g
���������range str
������������������������i
���������������k
���
���������������������������������������
str1 := "���������������"for i, k := range str1 { fmt.Printf("%v-%v\n", i, string(k))}// ���������������// 0-���// 1-���// 2-���// 3-���// 4-���
������������������������Golang������range
������������������������������������������i
��������������������������� Gothmultibyte������������������������
���
3. ���������Slice���
���Golang������������������������������������������������������������������������������������������������������������
var sc []intsc = make([]int, 0, 10) // ������������������������10������������10���������sc = append(sc, 1, 2, 3) // ������������������������sc = append(sc, []int{4, 5, 6}...) // ���������������������������for k, v := range sc { fmt.Printf("key: %v; val: %v\n", k, v)}
������������������������������������������sc
������������������������������������������������������������������
4. ���������Map���
���������Golang������������������������������������������������������������������������������������������������������������������
m := make(map[string]int)m["a"] = 1m["b"] = 2m["c"] = 3m["hello"] = 5m["world"] = 7m["ni"] = 6// ������������for k, v := range m { fmt.Printf("%v: %v\n", k, v)}// ���������������������// ni: 6// a: 1// b: 2// c: 3// hello:5// world:7
���������������������������������������������������������������������������������������������������������������������������������������������������
���������������������Golang������for���������switch���������������������������������������������������������������������Golang���������
发表评论
最新留言
关于作者
