From 2b8cee037aecc30cbcd37973314e75741fa10abe Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Wed, 16 Sep 2020 18:38:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E7=AC=94=E8=AE=B0=E5=A4=87?= =?UTF-8?q?=E4=BB=BD=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/go101/go协程延迟函数恐慌恢复.md | 21 +++++++++ src/go101/go注意事项.md | 3 +- src/testCase/defer.go | 21 +++++++++ src/testCase/flib.go | 47 +++++++++++++++++++ src/testCase/greenTest.go | 29 ++++++++++++ src/testCase/recover.go | 21 +++++++++ src/testCase/waitGroup.go | 36 ++++++++++++++ 7 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 src/go101/go协程延迟函数恐慌恢复.md create mode 100644 src/testCase/defer.go create mode 100644 src/testCase/flib.go create mode 100644 src/testCase/greenTest.go create mode 100644 src/testCase/recover.go create mode 100644 src/testCase/waitGroup.go diff --git a/src/go101/go协程延迟函数恐慌恢复.md b/src/go101/go协程延迟函数恐慌恢复.md new file mode 100644 index 0000000..1882d87 --- /dev/null +++ b/src/go101/go协程延迟函数恐慌恢复.md @@ -0,0 +1,21 @@ +# 主协程 🐖 🐑 🐩 🐈 + 每个Go程序启动的时候只有一个对用户可见的协程,称为主协程 + 当一个主协程退出后, 此程序也就退出了,即使还要一些其他协程在运行 + +# 协程返回值 + 一个协程调用的所有返回值(如果存在) 必须被全部舍弃 + +# waitGroup类型有三个方法 + Add(用来注册新的需要完成的任务数), Done(用来通知某个任务完成了), Wait(此方法用来阻塞等待)到所有任务都已经完成后继续执行其后的语句 + +# defer延迟处理函数 + 和协程调用类似, 被延迟函数调用的所有返回值都必须全部舍弃 + + 当一个函数调用被延迟后, 它不会立即被执行, 它将被推入由当前协程维护的一个延迟调用堆栈。 + 但一个函数调用返回并进入他的退出阶段,所有在此函数中已经被推入的延迟调用将按照它们被推入堆栈的逆顺序执行, + 当所有的延迟调用函数执行完毕之后,此函数调用也就真正的退出了 + + + + + diff --git a/src/go101/go注意事项.md b/src/go101/go注意事项.md index ab29ece..c900932 100644 --- a/src/go101/go注意事项.md +++ b/src/go101/go注意事项.md @@ -1,2 +1,3 @@ 1. 一般 print和println这两个内置函数并不推荐使用, 在正式的项目中,我们应该尽量使用fmt标准库包声明的相应函数 -2. Go大括号 {} 来显示一个代码块 但是在Go中 { 不能放到下一行 这限制的好处(加快编译速度, 统一编程风格) \ No newline at end of file +2. Go大括号 {} 来显示一个代码块 但是在Go中 { 不能放到下一行 这限制的好处(加快编译速度, 统一编程风格) +3. log标准库中的打印函数是经过了同步处理的, 而fmt标准库中的打印函数却没有被同步, 如果多协程中使用fmt打印则可能会交织在一起(概率性很低) diff --git a/src/testCase/defer.go b/src/testCase/defer.go new file mode 100644 index 0000000..50913d3 --- /dev/null +++ b/src/testCase/defer.go @@ -0,0 +1,21 @@ +package main + +import ( + "os" + "strconv" + "time" +) + +func test(i int) { + println("IMY***********", i) + defer test(i*100000000000000000/137 + 11 + 253) +} + +func main() { + cnt, _ := strconv.Atoi(os.Args[1]) + + for i := 0; i < cnt; i++ { + go test(1) + } + time.Sleep(120 * time.Second) +} diff --git a/src/testCase/flib.go b/src/testCase/flib.go new file mode 100644 index 0000000..36e47c7 --- /dev/null +++ b/src/testCase/flib.go @@ -0,0 +1,47 @@ +package main + +import ( + "os" + "strconv" +) + +func flib(index, max, l1, l2 int) int { + if index == max { + return l2 + } + return flib(index+1, max, l2, l1+l2) +} + +func f(n int) int { + if n == 0 { + return 0 + } else if n == 1 { + return 1 + } else { + return f(n-1) + f(n-2) + } +} + +func main() { + max, _ := strconv.Atoi(os.Args[1]) + + if max == 1 { + println("index1: \n", max) + println("value1: \n", 1) + return + } + if max == 2 { + println("index1: \n", max) + println("value1: \n", 1) + return + } + + sum := flib(3, max, 1, 2) + println("index1: ", max) + println("value1: ", sum) + + sum = f(max) + println("index2: ", max) + println("value2: ", sum) + +} diff --git a/src/testCase/greenTest.go b/src/testCase/greenTest.go new file mode 100644 index 0000000..66e218f --- /dev/null +++ b/src/testCase/greenTest.go @@ -0,0 +1,29 @@ +package main + +import ( + "log" + "math/rand" + "os" + "strconv" + "time" +) + +func SayGreetings(greeting string, times int) { + for i := 0; i < times; i++ { + log.Println(greeting + " " + strconv.Itoa(i)) + dur := time.Second * time.Duration(rand.Intn(5)) / 2 + time.Sleep(dur) // 睡眠片刻 + } +} + +func main() { + rand.Seed(time.Now().UnixNano()) + log.SetFlags(0) + cnt, _ := strconv.Atoi(os.Args[1]) + + for i := 0; i < cnt; i++ { + go SayGreetings("Hi"+strconv.Itoa(i), 1000) + } + log.Println("the main over!!!") + time.Sleep(time.Second * 1000000) +} diff --git a/src/testCase/recover.go b/src/testCase/recover.go new file mode 100644 index 0000000..30119fd --- /dev/null +++ b/src/testCase/recover.go @@ -0,0 +1,21 @@ +package main + +import "time" + +func main() { + println("hello world") + + go func() { + time.Sleep(time.Second) + //panic(123) + }() + + defer func() { + recover() + }() + println("IMY************") + for { + _ = new(int64) + //time.Sleep(time.Second) + } +} diff --git a/src/testCase/waitGroup.go b/src/testCase/waitGroup.go new file mode 100644 index 0000000..8facf75 --- /dev/null +++ b/src/testCase/waitGroup.go @@ -0,0 +1,36 @@ +package main + +import ( + "log" + "math/rand" + "os" + "strconv" + "sync" + "time" +) + +var wg sync.WaitGroup + +func SayGreetings(greeting string, times int) { + for i := 0; i < times; i++ { + log.Println(greeting + " " + strconv.Itoa(i)) + dur := time.Second * time.Duration(rand.Intn(5)) / 2 + time.Sleep(dur) // 睡眠片刻 + } + wg.Done() + log.Println("green over !!!") +} + +func main() { + rand.Seed(time.Now().UnixNano()) + log.SetFlags(0) + cnt, _ := strconv.Atoi(os.Args[1]) + wg.Add(cnt) + for i := 0; i < cnt; i++ { + go SayGreetings("Hi"+strconv.Itoa(i), 10) + } + + wg.Wait() + wg.Wait() + log.Println("the main over!!!") +}