Golang教程-Go通道
            
            Go通道
通道(Channel)充当了一个管道的角色,用于在不同的Goroutine之间发送类型化的值。它保证了同步性,因为任何时刻只有一个Goroutine可以访问数据项。数据的所有权在不同的Goroutine之间传递。因此,通过设计,它避免了共享内存的陷阱,并防止竞态条件的发生。
Go通道示例
package main  
import "fmt"  
import "time"  
func worker(done chan bool) {  
   fmt.Print("working...")  
   time.Sleep(time.Second)  
   fmt.Println("done")  
   done <- true  
}  
func main() {  
   done := make(chan bool, 1)  
   go worker(done)  
   <-done  
}  输出:
working...done