Four code to get started Go process and channel!

Write a simple program, so that a co program is used to write data to the channel, and a program is used to read data.

import (
    "fmt"
    "strconv"
    "testing"
)
/**
In this example, we have
  - A message as a channel
  - A protocol to send messages
  - The master process is used to receive messages
*/
func Test_message(t *testing.T)  {
    /**
    Declare a channel of length 1
    */
    messages := make(chan string)

    /**
    The protocol is responsible for sending data to the channel five times in total
    */
    go func() {
        for i := 0; i < 5; i++ {
            messages <- "Hello, This is the first. " + strconv.Itoa(i) + " Secondary delivery message"
        }
    }()

    /**
    Read the data in the channel. This is a blocking operation
    */
    for i := 0; i < 5; i++ {
        fmt.Println(<- messages)
    }
}

Code sample

Then here are three pieces of code. Before you see it, you can think about the running results. You'd better test it yourself.

If a delay of 1 second is set when writing data to the channel, will the protocol reading the channel data block?

import (
    "fmt"
    "strconv"
    "testing"
    "time"
)

/**
In this example, our sub process has a one second delay when writing data to the channel

And then we found that the console printed the content off and on

It shows that it is a blocking behavior for us to read messages from the channel
 */
func Test_message1(t *testing.T)  {
    /**
    Declare a channel of length 1
     */
    messages := make(chan string)

    /**
    The protocol sends data to the channel every second
     */
    go func() {
        for i := 0; i < 5; i++ {
            time.Sleep(time.Second * 1)
            messages <- "Hello, This is the first. " + strconv.Itoa(i) + " Secondary delivery message"
        }
    }()

    /**
    Read the data in the channel. This is a blocking operation
     */
    for i := 0; i < 5; i++ {
        fmt.Println(<- messages)
    }
}

Of course, the writing process is blocked, and the reading process can only wait. This problem seems to be a bit (๑ ᴗ ๑)

If one second delay is set for the read process, will the write process block?

import (
    "fmt"
    "strconv"
    "testing"
    "time"
)

/**
In this example, we set a one second delay when reading data to the channel

And then we found that the console printed the content off and on

It shows that it is also a blocking behavior for us to write messages to the channel
 It is limited by the channel capacity. If the channel capacity is full, the write operation will block the cooperation
*/
func Test_message2(t *testing.T)  {
    /**
    Declare a channel of one length
    */
    messages := make(chan int)

    /**
    The protocol sends data to the channel every second
    */
    go func() {
        for i := 0; i < 5; i++ {
            messages <- i
            fmt.Println("Hello, This is the first. " + strconv.Itoa(i) + " Secondary delivery message")
        }
    }()

    /**
    Read the data in the channel. This is a blocking operation
    */
    for i := 0; i < 5; i++ {
        <- messages
        time.Sleep(time.Second * 1)
    }
}

The answer is yes, because the capacity of channel is limited. Without consumers, producers cannot continue to produce.

How to set a volume of 5 for the channel? What will be the running result of the following program?

import (
    "fmt"
    "strconv"
    "testing"
    "time"
)

/**
In this example, we create a cached channel

You can do the following by yourself. You can see that the first six times of printing are very fast, and the last three times are intermittent

This is a buffered channel. Even without consumers, we can still write data to the channel,
Until the capacity reaches the upper limit
*/
func Test_message3(t *testing.T)  {
    /**
    Declare a channel with capacity of 5
    */
    messages := make(chan int, 5)

    /**
    This process continuously tries to send a data to the channel
    */
    go func() {
        for i := 0; i < 10; i++ {
            messages <- i
            fmt.Println("Hello, This is the first. " + strconv.Itoa(i) + " Secondary delivery message")
        }
    }()

    /**
    Read the data in the channel every second, which causes the elements in the channel to accumulate gradually
    */
    for i := 0; i < 5; i++ {
        <- messages
        time.Sleep(time.Second * 1)
    }
}

The answer is that the first six prints are very fast, and the last three are intermittent

Do you have a basic understanding of the coordination process and channel

https://github.com/fish-bugs/...

Tags: Go github

Posted on Fri, 08 Nov 2019 11:17:11 -0800 by GM