go语言之进阶篇无缓冲channel

时间:2023-06-14 11:10:08

1、无缓冲channel

示例:

package main

import (
"fmt"
"time"
) func main() {
//创建一个无缓存的channel
ch := make(chan int, 0) //len(ch)缓冲区剩余数据个数, cap(ch)缓冲区大小
fmt.Printf("len(ch) = %d, cap(ch)= %d\n", len(ch), cap(ch)) //新建协程
go func() {
for i := 0; i < 3; i++ {
fmt.Printf("子协程:i = %d\n", i)
ch <- i //往chan写内容
}
}() //延时
time.Sleep(2 * time.Second) for i := 0; i < 3; i++ {
num := <-ch //读管道中内容,没有内容前,阻塞
fmt.Println("num = ", num)
} }

执行结果:

len(ch) = 0, cap(ch)= 0
子协程:i = 0
num = 0
子协程:i = 1
子协程:i = 2
num = 1
num = 2