go已经越来越被重视了,特别适合大型互联网公司基础服务的编写,高效,高并发,可以同时允许多个明星出轨,多个明星结婚 都不在话下,下面介绍下GO协程通讯的过程
直接上代码
package mainimport ( "fmt" "time")func main() { //关于channel和routine的理解,一个描述挖矿的程序。它包含三个函数,分别负责执行寻矿、挖矿和练矿任务。 //在本例中,我们用一组字符串表示 rock(矿山) 和 ore(矿石),每个函数都以它们作为输入,并返回一组 “处理过的” 字符串。 theMine := [5]string{"rock", "ore", "rock", "ore", "ore"} //建立两个管道 oreChannel := make(chan string) mineChannel := make(chan string) //Finder 找到石头后发送到管道oreChannel go func(mine [5]string) { for _, item := range mine { if item == "rock" { oreChannel <- item //send item on oreChannel } } }(theMine) //Ore Breaker //从管道oreChannel 中读取 ore原石 /*go func() { for i := 0; i < 3; i++ { foundOre := <-oreChannel //read from oreChannel fmt.Println("From Finder", foundOre) mineChannel <- "mineOre" //send to mineOreChan } }() */ go func() { for foundOre := range oreChannel { fmt.Println("Miner: Received " + foundOre + " from finder") mineChannel <- foundOre } }() //Smelter //接收 go func() { for i := 0; i < 3; i++ { mineOre := <-mineChannel //read from mineChannel fmt.Println("From Miner:", mineOre) fmt.Println("From Smelter:Ore is smelter") } }() <-time.After(time.Second * 5) //}
最近学习的内容都记录下方便以后复习查看