-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_controller.go
More file actions
102 lines (88 loc) · 1.89 KB
/
process_controller.go
File metadata and controls
102 lines (88 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"time"
)
func processControllerCase() {
fmt.Println("=====流程控制=====")
//switch
exp := "1"
switch exp {
case "1":
fmt.Println("1")
case "2":
fmt.Println("2")
case "4", "5", "6":
fmt.Println("4,5,6匹配")
default:
fmt.Println("默认操作")
}
//type switch
var x interface{}
switch i := x.(type) {
case nil:
fmt.Println("x的类型为: ", i)
case int:
fmt.Println("x是 int 类型")
case string:
fmt.Println("x是 string 类型")
default:
fmt.Println("x是未知类型")
}
c1 := make(chan int) //无缓冲chan
c2 := make(chan int)
c3 := make(chan int)
var i1, i2 int
// 添加 goroutine 从 c2 接收数据
go func() {
fmt.Println("从c2接收数据", <-c2)
}()
go func() {
c1 <- 1
fmt.Println("向c1发送数据")
}()
/**
select 用于处理异步IO操作,case语句必须是一个channel操作,select会监听case语句中channel的读写操作
有default分支时: 仅当所有其它case都阻塞时执行,否则随机执行一个非阻塞的case语句
没有default分支时: 随机执行一个非阻塞的case语句,若都不满足条件,则一直阻塞
*/
select {
case i1 = <-c1:
fmt.Println("received", i1, "from c1")
case c2 <- i2:
fmt.Println("sent", i2, "to c2")
case i3, ok := (<-c3):
if ok {
fmt.Println("received", i3, "from c3")
}
default:
fmt.Printf("no communication\n")
}
//select实现超时处理
var resp = make(chan int)
select {
case data := <-resp:
fmt.Println("received", data, "from resp")
case <-time.After(time.Second * 3):
fmt.Println("timeout")
}
//for
s := "abc"
for i, n := 0, len(s); i < n; i++ { // 常见的 for 循环,支持初始化语句。
fmt.Println(s[i])
}
n := len(s)
for n > 0 {
fmt.Println(n)
n--
}
//for {
// println("while")
//}
//for true {
// println("无限循环")
//}
for i, v := range s {
fmt.Println(i, v)
}
}