第五课 golang的流程控制
发布日期:2021-05-14 20:12:12 浏览次数:21 分类:精选文章

本文共 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 := 10
switch {
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 []int
sc = 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"] = 1
m["b"] = 2
m["c"] = 3
m["hello"] = 5
m["world"] = 7
m["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���������

上一篇:go-zero 单体应用框架原理学习—1 路由相关的存储
下一篇:go-zero框架threading包—安全运行goroutine

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月27日 18时59分18秒