Home  >  Article  >  Backend Development  >  Go's concurrency without it is like an iPhone without network

Go's concurrency without it is like an iPhone without network

Go语言进阶学习
Go语言进阶学习forward
2023-07-21 14:13:52718browse
The concurrency attribute of Golang is a big killer of the language. When talking about concurrency, we cannot but mention Channel, you can think of it as a pipe, through which the concurrent core unit can send or receive data for communication. This article takes a deeper look at channel.

#The design of channel is based on the CSP model. CSP is the abbreviation of Communicating Sequential Process. It can be called Communicating Sequential Process in Chinese. It is a concurrent programming model proposed by Tony Hoare in 1977. Simply put, the CSP model consists of concurrently executing entities (threads or processes). Entities communicate by sending messages. The channel used when sending messages is a channel, or channel. The key to the CSP model is to focus on the channel, not the entity sending the message. The Go language implements part of the CSP theory. Goroutine corresponds to the concurrently executed entity in CSP, and channel corresponds to the channel in CSP.

Create Channel

  • Channel in Go uses chan as the keyword.

  • Without buffering chan, sending and receiving will be blocked until the other party is ready. This method can be used for synchronization in gororutines - without using locks or condition variables.

  • With buffer chan, you can try to avoid blocking and improve application performance, typically exchanging time for space.

aChan := make(chan int)  // 创建无缓冲chan
bChan := make(chan int, N) // 创建缓冲为N的chan

Assignment and value acquisition

You can’t see its huge role from the following code, which is normal, because the two statements are usually not together, for example : Coroutine A sends data, and coroutine B receives data.

mchan <- value  // 发送值v到Channel ch中
value := <-mchan // 从Channel ch中接收数据,并将数据赋值给v

Select

Selsect is the most common way to obtain data in Channel.

select 一定程度上可以类比于 linux 中的 IO 多路复用中的 select。后者相当于提供了对多个 IO 事件的统一管理,而 Golang 中的 select 相当于提供了对多个 channel 的统一管理。当然这只是 select 在 channel 上的一种使用方法。

func main(){
    ch1 := make(chan int, 1)
    ch2 := make(chan int, 1)

    select {
        case e1 := <-ch1:
        //如果ch1通道成功读取数据,则执行该case处理语句
            fmt.Printf("1th case is selected. e1=%v",e1)
        case e2 := <-ch2:
        //如果ch2通道成功读取数据,则执行该case处理语句
            fmt.Printf("2th case is selected. e2=%v",e2)
        default:
        //如果上面case都没有成功,则进入default处理流程
            fmt.Println("default!.")
    }
}

for…range

    for …… range语句可以处理Channel。

    go func() {
        time.Sleep(1 * time.Hour)
    }()
    c := make(chan int)
    go func() {
        for i := 0; i < 10; i = i + 1 {
            c <- i
        }
        close(c)
    }()
    for i := range c {
        fmt.Println(i)
    }
    fmt.Println("Finished")

timeout

    Select很重要的一个应用就是超时处理。因为上面提供的demo,select语句就会一直阻塞着。这时候我们可能就需要一个超时操作,用来处理超时的情况。下面这个例子我们会在2秒后往channel c1中发送一个数据,但是Select设置为1秒超时,因此我们会打印出timeout 1,而不是result 1。

    c1 := make(chan string, 1)
    go func() {
        time.Sleep(time.Second * 2)
        c1 <- "result 1"
    }()
    select {
    case res := <-c1:
        fmt.Println(res)
    case <-time.After(time.Second * 1):
        fmt.Println("timeout 1")
    }

close

    Go内建的close方法就可以用来关闭channel。但如果channel 已经被关闭,继续往它发送数据会导致panic: send on closed channel:

 close(mChan)

The above is the detailed content of Go's concurrency without it is like an iPhone without network. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete