Home  >  Article  >  Web Front-end  >  The keyword of Go element is located--chan channel

The keyword of Go element is located--chan channel

坏嘻嘻
坏嘻嘻Original
2018-09-14 10:24:082120browse

HTML validate refers to HTML validation. It is a process of analyzing HTML documents and marking errors and non-standard code by comparing them with standard HTML rules. Web pages are rendered using HTML, and HTML itself adopts HTML specifications as its rules and standards. Validate HTML code across multiple browser standards!

chan

chan is also called a channel, which is similar in form to a pipe. The content is sent in from one end and read out from the other end. The following describes how to define a channel:

var 变量名 chan dataType

When defining a channel, you need to specify the data type, which means that only variables of the specified data type are allowed to pass through the channel.

Initialize channel

In golang, when initializing channel type variables, the channel can be divided into two situations, one is a buffered channel, and the other is Unbuffered channel.
Let’s introduce the initialization methods in the following two situations:

// 初始化不带缓冲的通道,通道中数据类型是intvar ch1 = make(chan int)// 初始化带10个缓冲的通道,通道中数据类型是stringvar ch2 = make(chan string,10)

Another way to write it is to define and initialize the channel,

// 定义通道,并给通道初始化8个缓冲ch3 := make(chan int ,8)// 定义通道,并初始化为不带缓冲通道ch4 := make(chan string)

Channel assignment

Both reading and writing to the channel may enter a blocking state.

  1. Unbuffered channels will block when writing. The blocking will not end until the information in the channel is read.

  2. For a buffered channel, each time information is written to the channel, the channel length will be increased by 1. Each time information is successfully read from the channel, the channel length will be decreased by 1. If the channel length is equal to the channel buffer length, continuing to write information to the channel will cause the program to block; if the channel length is less than the channel buffer length, writing information to the channel will not cause blocking. If the channel length is 5, then writing information to the channel for the sixth time will cause the program to block when the channel has not been read.

The syntax format for channel writing is:

var ch = make(chan string,10)// 将字符串”hello"写入到通道中,通道长度加1ch <- "hello"

Read channel

The channel is empty
1. If the channel is not closed, the program will enter the blocking state and wait until the channel has information written
2. The channel has been closed and will not be blocked. The initial value of the data type in the channel (dirty data) is returned. For example, when the channel is chan int, the return value is 0. When the channel is chan string, the return value is empty.
The channel is not empty
1. The channel is not closed. Read the information from the channel once. After the reading is completed, continue execution
2. The channel has been closed. Read the information from the channel once. After the reading is completed, proceed to the

read channel operation:

val,ok := <-ch

Use assertion to read the channel The value in , checks whether the channel still has content, and determines whether the channel has been closed. When there is no information in the channel and the channel has been closed, the ok value is false. When the channel is not closed, but there is no information in the channel, the program will block. , if there is content in the channel, the ok value is true.

Another way to read a channel without using assertions

val := <-ch

Writing and reading channels

Reading an unbuffered channel example Method:

package mainimport (    "fmt")func main() {    // 定义一个不带缓冲的通道,通道中数据类型是int
    var c = make(chan int)    // 开启一个携程,读取通道中的内容
    go func() {
        fmt.Println("写入信息是:", <-c)
    }()    // 向通道中写入数据
    c <- 1}

Output result:

写入信息是: 1

When reading and writing a buffered channel, as long as the data length in the channel is not greater than the buffer length, there will be no blocking, but when reading the buffered channel, there will be no blocking. For buffered channels, when there is no content in the channel, the program will still enter the blocking state. Therefore, buffered channels only affect writes. Here is an example:

package mainimport (    "fmt")func main() {    var c = make(chan int, 3)
    c <- 1
    c <- 2
    c <- 3
    //c <- 4
    fmt.Println("end")
}

The output information is:

end

When writing content to a channel with 3 buffers, since it is only written 3 times, the length of the channel is exactly equal to The length of the buffer means that the program is not blocked. When the comment in front of c

Coroutine communication

The channel type variable is essentially an address, as shown in the following example code:

package mainimport (    "fmt")func main() {    var c = make(chan int, 3)
    fmt.Println(c)
}

Output result:

0xc042072080

So, when the channel type variable is passed into the function as a parameter, the value in the channel can be directly modified in the function. Although the chan type variable is an address, golang does not allow the use of the value operator (*) to operate the chan type variable. But if you first use the address operator (&) on the chan type variable, and then use the value operator (*), this operation method can still run normally, but it does not mean much unless your purpose is In the function call, redefine a chan type variable to replace the original variable.

These features of chan can effectively realize the synchronization function between coroutines. The unbuffered channel is a zero-tolerance waiting, which can achieve forced synchronization; the buffered channel is a certain amount of tolerance waiting, and can achieve synchronization that allows a certain time difference.

Simple example of inter-coroutine communication:

package mainimport (    "fmt"
    "time")func main() {    var c = make(chan int)    go func() {
        fmt.Println("待命模式:")        // 读取通道时产生阻塞,等待其他协程向通道写入信息
        fmt.Println("命令代码是:", <-c)
    }()    go func() {        // 延时3秒,向通道中写入信息
        time.Sleep(time.Second * 3)
        fmt.Println("发送命令:")
        c <- 8
        close(c)
    }()
    time.Sleep(time.Second * 5)
    fmt.Println("执行完成")
}

The output information is:

待命模式:
发送命令:
命令代码是: 8
执行完成

Related recommendations:

HTML validate HTML validation_HTML /Xhtml_Web page production

HTML skills compilation_CSS/HTML

The above is the detailed content of The keyword of Go element is located--chan channel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn