Home Web Front-end HTML Tutorial The keyword of Go element is located--chan channel

The keyword of Go element is located--chan channel

Sep 14, 2018 am 10:24 AM

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
Copy after login

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)
Copy after login

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

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

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"
Copy after login

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
Copy after login

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
Copy after login

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}
Copy after login

Output result:

写入信息是: 1
Copy after login

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")
}
Copy after login

The output information is:

end
Copy after login

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 <- 4 is removed, since there is no program to read this channel, the main program enters a deadlock state and causes an exception.

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)
}
Copy after login

Output result:

0xc042072080
Copy after login

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("执行完成")
}
Copy after login

The output information is:

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

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles