golang ------------ channel channel (a basic operation)

1. Channel is the interaction between goroutines, not in a single goroutine

2. If the data sent to the channel has not been received by the goroutine at that time, it will be deadlocked (someone must fetch the data)

func work(){
	//var c chan int //declaration
	c := make(chan int) //create chan
	//a :=<- c does not work, because channel is the interaction between goroutines and cannot be in a single goroutine
	go func(){
		for{
			a := <-c
			fmt.Println(a)
		}
	}()
	c <- 1 //If this sentence is placed on go func(), it will be deadLock, and the data sent to the channel will be deadLock if it has not been received by goroutine at that time
}

func main(){
	work()
	time.Sleep(time.Minute)
}

3, channel as a parameter

func work(id int,c chan int) {
	for{
		a := <-c
		fmt.Printf("work %d received %d\n",id,a)
	}
}

func chanMake(){
	var channels [9]chan int
	for i:=0;i<9;i++ {
		channels[i] = make(chan int)
		go work(i,channels[i])
	}

	for i:=0;i<9;i++ {
		channels[i] <- i
	}
}

func main(){
	chanMake()
	time.Sleep(time.Minute)
}

4, channel as the return value

func work(id int) chan int {//chan as the return value
	c := make(chan int)
	go func(){
		for{
			fmt.Printf("work %d received %d\n",id,<-c)
		}
	}()
	return c
}

func chanMake(){
	var channels [9]chan int
	for i:=0;i<9;i++ {
		channels[i] = work(i)
	}

	for i:=0;i<9;i++ {
		channels[i] <- i
	}
}

func main(){
	chanMake()
	time.Sleep(time.Millisecond)
}

4. One-way channel

func work(id int) chan<- int { //Only data can be sent outside
	c := make(chan int)
	go func(){
		for{
			fmt.Printf("work %d received %d\n",id,<-c) //It can only receive data
		}
	}()
	return c
}

func chanMake(){
	var channels [9]chan<- int //One-way chan
	for i:=0;i<9;i++ {
		channels[i] = work(i)
	}

	for i:=0;i<9;i++ {
		channels[i] <- i
	}
}

5. Buffered channel ------------- These values ​​can be sent to the channel without corresponding concurrent reception.

func work(id int,c chan int) {
	for{
		a := <-c
		fmt.Printf("work %d received %d\n",id,a)
	}
}

func bufferedChannel(){
	c := make(chan int ,2)//buffer channel accepts a limited number of values
	go work(0,c)
	c <- 1
	c <- 2
	c <- 3
	c <- 4
	time.Sleep(time.Millisecond)
}

func main(){
	bufferedChannel()
	time.Sleep(time.Millisecond)
}

6. Close the channel

    The data to be sent can still be read from it after close. After the data is read, the zero value will be read , which can be read multiple times. If there is no close, you can only read the data that has been sent in

func work0(id int,c chan int) {
/*	for{
		a,ok := <-c
		if !ok {
			break
		}
		fmt.Printf("work %d received %d\n",id,a)
	}*/
	for a := range c{ //The comment code has the same effect as this code, range, when there is no data in the channel, it will not be read
		fmt.Printf("work %d received %d\n",id,a)
	}
}

func channelClose(){
	c := make(chan int)
	go work0(0,c)
	c <- 1
	c <- 2
	c <- 3
	c <- 4
	close(c) //The data to be sent can still be read from it after close. After reading the data, the zero value will be read, which can be read multiple times
	time.Sleep(time.Millisecond)
}

func main(){
	channelClose()
	time.Sleep(time.Millisecond)
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324733892&siteId=291194637