溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用channel實現(xiàn)goroutine

發(fā)布時間:2020-07-05 14:30:00 來源:網(wǎng)絡(luò) 閱讀:277 作者:PowerMichael 欄目:編程語言
使用channel實現(xiàn)goroutine

1.無緩沖channel



    package main

    import (
        "fmt"
        "time"
    )

    var message = make(chan string)

    //往channel中輸入信息
    func sample1()  {
        message <- "hello gorotine."
    }

    //消費channel中的信息
    func sample2()  {
        str := <- message
        str = str + " run fast,run the world."
        message <- str

    }

    func main()  {
        go sample1()
        go sample2()
        time.Sleep(time.Second)
        fmt.Println(<-message)
        fmt.Println("message")

    }

2.buffer channel(緩沖channel)



    package main

    import (
        "fmt"
        "time"
    )

    /*
        1.buffer channel
        2.在函數(shù)中傳遞channel,而不是申明一個全局變量channel
        3.FIFO:first input first output
     */

    //往channel中輸入信息
    func sample1(message chan string)  {
        message <- "hello gorotine.1"
        message <- "hello gorotine.2"
        message <- "hello gorotine.3"
        message <- "hello gorotine.4"
    }

    //消費channel中的信息
    func sample2(message chan  string)  {
        str := <- message
        str = str + " run fast,run the world."
        message <- str

    }

    func main()  {
        var message = make(chan string, 4)
        go sample1(message)
        go sample2(message)
        time.Sleep(time.Second)
        fmt.Println(<-message)
        fmt.Println(<-message)
        fmt.Println(<-message)
        //fmt.Println(<-message)

        fmt.Println("message")

    }
   // 2.1 output1
    hello gorotine.2
    hello gorotine.3
    hello gorotine.4
    message

取消這行代碼的注釋//fmt.Println(<-message),將會得到如下的輸出,這正好說明了先進(jìn)先出的概念

  //2.2 output2
    hello gorotine.2
    hello gorotine.3
    hello gorotine.4
    hello gorotine.1 run fast,run the world.
    message

3.遍歷channel及關(guān)閉遍歷channel

package main

import (
    "fmt"
    "time"
)

/*
    1.buffer channel
    2.在函數(shù)中傳遞channel,而不是申明一個全局變量channel
    3.FIFO:first input first output
 */

//往channel中輸入信息
func sample1(message chan string)  {
    message <- "hello gorotine.1"
    message <- "hello gorotine.2"
    message <- "hello gorotine.3"
    message <- "hello gorotine.4"
}

//消費channel中的信息
func sample2(message chan  string)  {
    str := <- message
    str = str + " run fast,run the world."
    message <- str

}

func main()  {
    var message = make(chan string, 4)
    go sample1(message)
    go sample2(message)
    time.Sleep(time.Second)
    //使用range遍歷channel
    for str := range message {
        fmt.Println(str)
    }

    fmt.Println("message")

}

不關(guān)閉channel,那么channel默認(rèn)為空;那此時如果遍歷,則會拋出異常

//3.1output1
hello gorotine.2
hello gorotine.3
hello gorotine.4
hello gorotine.1 run fast,run the world.
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
    D:/run/pms/src/test.go:37 +0x126

下面演示關(guān)閉channel的遍歷

package main

import (
    "fmt"
    "time"
)

/*
    1.buffer channel
    2.在函數(shù)中傳遞channel,而不是申明一個全局變量channel
    3.FIFO:first input first output
 */

//往channel中輸入信息
func sample1(message chan string)  {
    message <- "hello gorotine.1"
    message <- "hello gorotine.2"
    message <- "hello gorotine.3"
    message <- "hello gorotine.4"
}

//消費channel中的信息
func sample2(message chan  string)  {
    str := <- message
    str = str + " run fast,run the world."
    message <- str
    close(message)

}

func main()  {
    var message = make(chan string, 4)
    go sample1(message)
    go sample2(message)
    time.Sleep(time.Second)
    //使用range遍歷channel
    for str := range message {
        fmt.Println(str)
    }

    fmt.Println("message")

}
//3.2output2
hello gorotine.2
hello gorotine.3
hello gorotine.4
hello gorotine.1 run fast,run the world.
message
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI