From 69e53c2d4bb0395e63698b0bc41241d1bebfa911 Mon Sep 17 00:00:00 2001 From: wandoubaba Date: Wed, 20 Nov 2024 11:41:06 +0800 Subject: [PATCH] =?UTF-8?q?channel/test2=E6=9C=89=E7=BC=93=E5=86=B2?= =?UTF-8?q?=E7=9A=84channel=EF=BC=8Cchannel/test3=E6=98=BE=E5=BC=8F?= =?UTF-8?q?=E5=85=B3=E9=97=ADchannel=EF=BC=8Cchannel/test4=E4=B8=BB?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E7=94=A8for..range=E4=BB=8Echannel=E8=AF=BB?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aceld/goroutine/channel/test1/main.go | 4 +-- .../aceld/goroutine/channel/test2/main.go | 35 +++++++++++++++++++ .../aceld/goroutine/channel/test3/main.go | 27 ++++++++++++++ .../aceld/goroutine/channel/test4/main.go | 20 +++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 bilibili/aceld/goroutine/channel/test2/main.go create mode 100644 bilibili/aceld/goroutine/channel/test3/main.go create mode 100644 bilibili/aceld/goroutine/channel/test4/main.go diff --git a/bilibili/aceld/goroutine/channel/test1/main.go b/bilibili/aceld/goroutine/channel/test1/main.go index 33677de..9676d77 100644 --- a/bilibili/aceld/goroutine/channel/test1/main.go +++ b/bilibili/aceld/goroutine/channel/test1/main.go @@ -9,11 +9,11 @@ func main() { go func() { defer fmt.Println("goroutine 结束") fmt.Println("goroutine 正在运行...") - // 将值发送给c + // go程中:将值发送给c c <- 666 }() - // 读c中的数据并赋值给num + // 主线程中:读c中的数据并赋值给num num := <-c fmt.Println("num = ", num) fmt.Println("main goroutine 结束 ...") diff --git a/bilibili/aceld/goroutine/channel/test2/main.go b/bilibili/aceld/goroutine/channel/test2/main.go new file mode 100644 index 0000000..77b5f40 --- /dev/null +++ b/bilibili/aceld/goroutine/channel/test2/main.go @@ -0,0 +1,35 @@ +// 有缓冲的channel +package main + +import ( + "fmt" + "time" +) + +func main() { + c := make(chan int, 3) // 带有缓冲的channel + + fmt.Println("len(c) = ", len(c), ", cap(c) = ", cap(c)) + + // 做一个go程 + go func() { + defer fmt.Println("子go程结束") + // 如果循环3将不会阻塞,大于3就会阻塞 + for i := 0; i < 4; i++ { + // 把i传递给channel + c <- i + fmt.Println("子go程正在运行, 发送元素: ", i, ", len(c) = ", len(c), ", cap(c) = ", cap(c)) + } + }() + + time.Sleep(2 * time.Second) + + for i := 0; i < 4; i++ { + num := <-c + fmt.Println("num = ", num) + // 如果这里不阻塞的话,那么子go程在第2轮之后有可能不会打印 + time.Sleep(1 * time.Second) + } + + fmt.Println("主进程main结束") +} diff --git a/bilibili/aceld/goroutine/channel/test3/main.go b/bilibili/aceld/goroutine/channel/test3/main.go new file mode 100644 index 0000000..d7b3c56 --- /dev/null +++ b/bilibili/aceld/goroutine/channel/test3/main.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +func main() { + c := make(chan int) + + go func() { + for i := 0; i < 5; i++ { + c <- i + } + // close可以关闭一个channel + close(c) + }() + + for { + // 如果ok为true表示channel没有关闭,如果ok为false表示channel已经关闭 + if data, ok := <-c; ok { + fmt.Println(data) + } else { + fmt.Println("channel c 已被关闭") + break + } + } + + fmt.Println("Main finished ...") +} diff --git a/bilibili/aceld/goroutine/channel/test4/main.go b/bilibili/aceld/goroutine/channel/test4/main.go new file mode 100644 index 0000000..bc7bd0d --- /dev/null +++ b/bilibili/aceld/goroutine/channel/test4/main.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func main() { + c := make(chan int) + + go func() { + for i := 0; i < 5; i++ { + c <- i + } + close(c) + }() + + for data := range c { + fmt.Println(data) + } + + fmt.Println("Main finished ...") +}