前端之家收集整理的这篇文章主要介绍了
golang channel的长度,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
packagemain
import(
"fmt"
"time"
)
funcmain(){
test_chan2()
}
functest_chan2(){
c:=make(chan[]int,10)
fmt.Printf("len1%v\n",len(c))
//fmt.Printf("chanel%v\n",<-c)
//close(c)
c<-[]int{1,2,10}
c<-[]int{3,4,5}
c<-[]int{5,6,7}
gofunc(){
for{
select{
casexx:=<-c:
fmt.Printf("value%v,len%v\n",xx,len(c))
}
}
}()
time.Sleep(5*time.Second)
fmt.Printf("chanel%v\n",len(c))
}
//输出结果为:
len10
value[1210],len2
value[345],len1
value[567],len0
len20