Compare commits
2 Commits
bba7ffe503
...
69e53c2d4b
Author | SHA1 | Date | |
---|---|---|---|
69e53c2d4b | |||
4105f63f1f |
20
bilibili/aceld/goroutine/channel/test1/main.go
Normal file
20
bilibili/aceld/goroutine/channel/test1/main.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// 定义一个channel
|
||||||
|
c := make(chan int)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer fmt.Println("goroutine 结束")
|
||||||
|
fmt.Println("goroutine 正在运行...")
|
||||||
|
// go程中:将值发送给c
|
||||||
|
c <- 666
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 主线程中:读c中的数据并赋值给num
|
||||||
|
num := <-c
|
||||||
|
fmt.Println("num = ", num)
|
||||||
|
fmt.Println("main goroutine 结束 ...")
|
||||||
|
}
|
35
bilibili/aceld/goroutine/channel/test2/main.go
Normal file
35
bilibili/aceld/goroutine/channel/test2/main.go
Normal file
@ -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结束")
|
||||||
|
}
|
27
bilibili/aceld/goroutine/channel/test3/main.go
Normal file
27
bilibili/aceld/goroutine/channel/test3/main.go
Normal file
@ -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 ...")
|
||||||
|
}
|
20
bilibili/aceld/goroutine/channel/test4/main.go
Normal file
20
bilibili/aceld/goroutine/channel/test4/main.go
Normal file
@ -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 ...")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user