61 Star 659 Fork 311

ShirDon-廖显东/Go Web编程实战派源码

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
channel4.go 1021 Bytes
Copy Edit Raw Blame History
ShirDon-廖显东 authored 2021-01-19 21:02 +08:00 . commit
//++++++++++++++++++++++++++++++++++++++++
// 《Go Web编程实战派从入门到精通》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goWebActualCombat
// 仓库地址:https://github.com/shirdonl/goWebActualCombat
//++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var wg sync.WaitGroup
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
ch := make(chan int)
wg.Add(2)
go playTableTennis("Jim", ch)
go playTableTennis("Jack", ch)
ch <- 1
wg.Wait()
}
func playTableTennis(player string, ch chan int) {
defer wg.Done()
for {
ball, ok := <-ch
if !ok {
fmt.Printf("%s has won!\n", player)
return
}
n := rand.Intn(100)
if n % 13 == 0 {
fmt.Printf("player %s missed!\n", player)
close(ch)
return
}
ball++
fmt.Printf("play %s has hit the ball, his score is %d\n", player, ball)
ch <- ball
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shirdonl/goWebActualCombat.git
git@gitee.com:shirdonl/goWebActualCombat.git
shirdonl
goWebActualCombat
Go Web编程实战派源码
11434bb9048a

Search