channel synchronization _ golang

we can use channels to sychronize execution across goroutines. Here's an example of using a blocking receive to to wait for a goroutine to finsh

package main

import (
    "fmt"
    "time"
)

func worker(done chan bool) {
    fmt.Println("working..")
    time.Sleep(time.Second)
    fmt.Println("done")
    done <- true
}

func main() {

    done := make(chan bool, 1)
    go worker(done)
    fmt.Println("done", <-done)
    fmt.Println(<-done)
}
working..
done
done true

总结 :

   1 : .......

本文来自:博客园

感谢作者:jackkiexu

查看原文:channel synchronization _ golang

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。