directory

  • preface
  • The body of the
  • At the end

preface

The concept of channels in Golang should give you a basic understanding of channels, at least for data transmission.

Review of the previous period

The body of the

Channels are used to transmit data, so the data must have its own type, yes, that’s right. The pipe also has its own type, but it has a chan identifier in front of the data type, such as chan int, chan String, etc.

Since the channel is used to transmit data, will the channel have a difference in direction? Ha ha, yes. Channels do have a direction, but we tend to have a common two-way channel. Note that the directions are written and read.

At this point, some people may ask: are there some channels, only to read data? Some channels, you can only write data, right? The answer is yes.

So how do you declare and define read-only and write-only channels?

Next, take a look at some code:

ch1 := make(chan int)   // In general, we define a two-way channel
ch2 := make(chan<- int) // This is a write-only channel that can only write int data
ch3 := make(< -chan int) // This is a read-only channel that can only read int data
Copy the code

Okay, you get the idea.

Are there any other types of channels?

Yes, there is another type of channel ———— buffer channel.

Normally, a channel is created with a buffer, but the buffer size is 0. Heh heh, just not!

How are buffer channels declared and defined? Let’s look at another piece of code:

ch1 := make(chan int)     In general, we define a channel with a buffer of 0
ch2 := make(chan int.10) // We define a channel with buffer size of 10, which means we can write 11 data.
Copy the code

So what’s special about buffer channels? Non-buffered channels, which block every read and write, can create efficiency problems. Buffered channels can avoid this problem to some extent.

Let’s take a look at how the buffering channel works with an example.

Example code:

package main

import (
	"fmt"
)

func main(a) {
	// Define a channel with buffer size 5
	ch1 := make(chan int.5)

	// Enable subcoroutine goroutine to write data
	go func(a) {
		for i := 0; i < 11; i++ {
			ch1 <- i
			fmt.Println("Subcoroutine writes data:", i)
		}
		close(ch1) // Close the channel} ()// The main coroutine reads data
	for {
		v, ok := <-ch1
		if! ok { fmt.Println("Finish reading", ok)
			break
		}
		fmt.Println("The main coroutine reads:", v)
	}

	fmt.Println("End of main coroutine")}Copy the code

Code execution process:

Subcoroutines write data: 0

Subcoroutines write data: 1

Subcoroutines write data: 2

Subcoroutines write data: 3

Subcoroutines write data: 4

Subcoroutines write data: 5

The main coroutine reads: 0

The data read by the main coroutine is: 1

The data read by the main coroutine is: 2

The data read by the main coroutine is: 3

The data read by the main coroutine is: 4

The data read by the main coroutine is: 5

The data read by the main coroutine is: 6

Subcoroutines write data: 6

Subcoroutines write data: 7

Subcoroutines write data: 8

Subcoroutines write data: 9

Subcoroutines write data: 10

The data read by the main coroutine is: 7

The data read by the main coroutine is: 8

Data read by the main coroutine is: 9

The main coroutine reads: 10

Read end false

The main coroutine ends

As we can see, the buffer channel can write data continuously, even if it is not read, it is not blocked. In this way, you can avoid the rigid work style of a non-buffered channel, which depends on the specific usage scenario.

At the end

Well, today’s introduction to channels is all about these, mainly to understand the channel is a “direction” concept, the other is that channels can have buffers.

PS: Nuggets editor theme template is very easy to use, like a 👍