91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

goroutine/channel(1)

發布時間:2020-08-23 19:26:04 來源:網絡 閱讀:230 作者:梁十八 欄目:編程語言

goroutine/channel(1)
goroutine/channel(1)
goroutine/channel(1)
goroutine/channel(1)
goroutine/channel(1)
goroutine/channel(1)
goroutine/channel(1)

goroutine/channel(1)

channel可以是各種數據類型:
goroutine/channel(1)
(map類型)
goroutine/channel(1)
(struct類型)
goroutine/channel(1)
(接口類型)
goroutine/channel(1)
goroutine/channel(1)

package main

import "fmt"

type student struct {
    name string
}

func main() {
    var stuChan chan interface{}
    //空interface類型可以是任意類型
    stuChan = make(chan interface{}, 10)
    stu := student{name:"stu01"}
    stuChan <- &stu

    var stu01 interface{}
    stu01 = <- stuChan

    fmt.Println(stu01)

    var stu02 *student
    //將stu01由interface轉換為結構體類型指針
    stu02, ok := stu01.(*student)
    if !ok {
        fmt.Println("不可以轉換")
        return
    }
    fmt.Println(stu02)

}

輸出:
&{stu01}
&{stu01}

Process finished with exit code 0

goroutine/channel(1)
goroutine/channel(1)

goroutine/channel(1)

channel關閉,判斷:

package main

import "fmt"

func main() {
    var ch chan int
    ch = make(chan int, 10)

    for i := 0; i < 10; i++ {
        ch <- i
    }

    close(ch)

    for {
        var b int
        //從chan取數據的時候第二個參數用來判斷是否成功
        b, ok := <- ch
        if ok == false {
            fmt.Println("chan is closed")
            return
        }
        fmt.Println(b)
    }

}

輸出:
0
1
2
3
4
5
6
7
8
9
chan is closed
range遍歷chan:

package main

import "fmt"

func main() {
    var ch chan int
    ch = make(chan int, 1000)

    for i := 0; i < 1000; i++ {
        ch <- i
    }

    close(ch)
    for v := range ch {
        fmt.Println(v)
    }
}

關閉chan,goroutine同步案例:

package main

import (
    "fmt"
)

func calc(taskChan chan int, resChan chan int,  exitChan chan bool) {
    for v := range taskChan {
        flag := true
        for i := 2; i < v; i++ {
            if v%i == 0 {
                flag = false
                break
            }
        }

        if flag {
            resChan <- v
        }
    }

    fmt.Println("exit")
    //每個calc的協程(goroutine)結束后寫入true到名為exitChan的chan里。代表該goroutine結束了
    exitChan <- true
}

func main() {
    intChan := make(chan int, 1000)
    resultChan := make(chan int, 1000)
    exitChan := make(chan bool, 8)

    go func() {
        for i := 0; i < 10000; i++ {
            intChan <- i
        }

        close(intChan)
    }()

    for i := 0; i < 8; i++ {
        go calc(intChan, resultChan, exitChan)
    }

    //等待所有計算的goroutine全部退出
    go func() {
        for i := 0; i < 8; i++ {
            <-exitChan
            fmt.Println("wait goroute ", i, " exited")
        }
        //8個goroutine結束后關閉resultChan管道
        close(resultChan)
    }()

    //resultChan管道關閉后,range才能在遍歷完后結束
    for v := range resultChan {
        fmt.Println(v)
    }
}

再來個同步channel的例子:

package main

import (
    "fmt"
)

func send(ch chan int, exitChan chan bool) {
    for i := 0; i < 10; i++ {
        ch <- i
    }
    close(ch)
    exitChan <- true
}

func recv(ch chan int, exitChan chan bool) {
    for {
        v, ok := <- ch
        if !ok {
            break
        }
        fmt.Println(v)
    }
    exitChan <- true
}

func main() {
    ch := make(chan int, 10)
    exitChan := make(chan bool, 2)

    go send(ch, exitChan)
    go recv(ch, exitChan)

    var total = 0
    for _ = range exitChan {
        total++
        if total == 2 {
            break
        }
    }

}

輸出:
0
1
2
3
4
5
6
7
8
9

Process finished with exit code 0
channel的只讀,只寫(讀函數里只準讀,寫函數里只準寫。來進行規范,防止出錯):

package main

import "fmt"

func send(ch chan<- int, exitChan chan struct{}) {

    for i := 0; i < 10; i++ {
        ch <- i
    }

    close(ch)
    var a struct{}
    exitChan <- a
}

func recv(ch <-chan int, exitChan chan struct{}) {
    for {
        v, ok := <-ch
        if !ok {
            break
        }

        fmt.Println(v)
    }

    var a struct{}
    exitChan <- a
}

func main() {
    var ch chan int
    ch = make(chan int, 10)
    exitChan := make(chan struct{}, 2)

    go send(ch, exitChan)
    go recv(ch, exitChan)

    var total = 0
    for _ = range exitChan {
        total++
        if total == 2 {
            break
        }
    }
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阿荣旗| 徐汇区| 甘南县| 朝阳区| 常州市| 辰溪县| 抚顺县| 石柱| 彭山县| 双桥区| 镇赉县| 土默特右旗| 峨山| 青阳县| 遂平县| 嘉鱼县| 漯河市| 璧山县| 红安县| 双牌县| 曲水县| 尖扎县| 刚察县| 曲靖市| 理塘县| 榕江县| 昌图县| 清镇市| 龙里县| 双峰县| 上虞市| 阿坝| 利川市| 玉田县| 格尔木市| 平南县| 苏尼特右旗| 库尔勒市| 蕉岭县| 舒兰市| 九龙县|