Channel

Channel

1. Var pipe name chan data type, eg: var intChan chan intdefine a plastic pipe;
2. The pipe channel is a reference type and needs to be initialized to write data. Make allocation of memory is required, and
memory is allocated: the intChan=make(chan int,3)capacity is 3;

package main
import (
	"fmt"
)
//管道类似于队列
func main(){
    
    
   var intchan chan int   //定义int类型的管道
   intchan=make(chan int,3)   //分配空间,足以说明intchan是引用类型,Intchan指向一个地址
//    fmt.Println(intchan)
//向管道中存入数据,依次存放,超过容量则会出现死锁
   intchan<-10
   var num int =11
   intchan<-num
   intchan<-(-1)
   fmt.Printf("intchan长度=%v,容量=%v\n",len(intchan),cap(intchan))
//向管道中取出数据,去除数据过多就会出现死锁
   var num1,num2,num3 int
   num1=<-intchan
   num2=<-intchan
   num3=<-intchan
   fmt.Printf("%v,%v,%v\n",num1,num2,num3)
   fmt.Printf("intchan长度=%v,容量=%v\n",len(intchan),cap(intchan))
}

In the code, the pipeline enters from the right channel<-数据, and the data goes out from the left.<-channel

Guess you like

Origin blog.csdn.net/yyq1102394156/article/details/114091717